Open italodamato opened 3 years ago
It's really hard to say anything without seeing the errors. Would you mind putting together a Colab that reproduces the issue?
In general:
index
is exactly the same as what you pass to the query model during training.Worked! Thanks a lot! I had to:
Is there a way to avoid getting back the same candidate multiple times? I know I can filter it out afterward but wondering why is this even happening? Am I doing something wrong?
Worked! Thanks a lot! I had to:
- Make sure every value of the dict was a tensor (np arrays didn't work).
- Add one dimension to each tensor to emulate the batch size (some layers like GlobalAveragePooling1D fail if the input is not 3D)
Actually, I'm trying to use it as input for scann but it gives me an error saying it doesn't accept dicts, just tensors. Any solution?
# get query from dataset in the apporpiate format for brute_force
def get_test_query(df, df_row):
return dict(df.loc[df_row,['last_100_product_views',
'last_100_purchases',
'last_100_searches',
'user_gender',
'user_country',
]].map(lambda x: tf.expand_dims(x, axis=0)))
EDIT: Solved it with:
_, titles = scann(model.query_model(get_test_query(train_df, row_n)), n_of_recommendations)
print(f"Top recommendations: {titles[0]}")
Not sure it's the proper way.
Edit2: Actually, I think the proper way is to pass the query model in the scann layer itself.
I'm not sure why I'm not getting any speed benefits compared to brute force though:
Edit3: I got Scann to work faster after I adapted my model to be successfully saved and re-loaded.
To avoid duplicates you should pass a deduplicated dataset into index
, containing each candidate you can recommend only once.
It looks like you're passing in the train
dataset, which presumably contains many, many instances of the same candidate.
Thanks, it makes sense. Wondering also if there's a way to pass the k elements to retrieve after the model has been loaded. I get an error if I try to change it.
Without k, it works:
With k, I get an error:
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-171-ebc22dbbb800> in <module>()
6 ], 1)
7
----> 8 _, titles = loaded(query, 10)
9 print(query)
10 print('\n')
14 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/function_deserialization.py in restored_function_body(*args, **kwargs)
255 .format(_pretty_format_positional(args), kwargs,
256 len(saved_function.concrete_functions),
--> 257 "\n\n".join(signature_descriptions)))
258
259 concrete_function_objects = []
ValueError: Could not find matching function to call loaded from the SavedModel. Got:
Positional arguments (3 total):
* {'last_100_product_views': <tf.Tensor 'queries:0' shape=(1,) dtype=string>, 'last_100_purchases': <tf.Tensor 'queries_1:0' shape=(1,) dtype=string>, 'last_100_searches': <tf.Tensor 'queries_2:0' shape=(1,) dtype=string>, 'user_gender': <tf.Tensor 'queries_4:0' shape=(1,) dtype=string>, 'user_country': <tf.Tensor 'queries_3:0' shape=(1,) dtype=string>}
* 10
* False
Keyword arguments: {}
Expected these arguments to match one of the following 4 option(s):
Option 1:
Positional arguments (3 total):
* {'last_100_purchases': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_purchases'), 'last_100_product_views': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_product_views'), 'user_gender': TensorSpec(shape=(None,), dtype=tf.string, name='user_gender'), 'last_100_searches': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_searches'), 'user_country': TensorSpec(shape=(None,), dtype=tf.string, name='user_country')}
* None
* False
Keyword arguments: {}
Option 2:
Positional arguments (3 total):
* {'last_100_purchases': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_purchases'), 'last_100_product_views': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_product_views'), 'user_gender': TensorSpec(shape=(None,), dtype=tf.string, name='queries/user_gender'), 'last_100_searches': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_searches'), 'user_country': TensorSpec(shape=(None,), dtype=tf.string, name='queries/user_country')}
* None
* False
Keyword arguments: {}
Option 3:
Positional arguments (3 total):
* {'last_100_purchases': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_purchases'), 'last_100_product_views': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_product_views'), 'user_gender': TensorSpec(shape=(None,), dtype=tf.string, name='user_gender'), 'last_100_searches': TensorSpec(shape=(None,), dtype=tf.string, name='last_100_searches'), 'user_country': TensorSpec(shape=(None,), dtype=tf.string, name='user_country')}
* None
* True
Keyword arguments: {}
Option 4:
Positional arguments (3 total):
* {'last_100_purchases': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_purchases'), 'last_100_product_views': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_product_views'), 'user_gender': TensorSpec(shape=(None,), dtype=tf.string, name='queries/user_gender'), 'last_100_searches': TensorSpec(shape=(None,), dtype=tf.string, name='queries/last_100_searches'), 'user_country': TensorSpec(shape=(None,), dtype=tf.string, name='queries/user_country')}
* None
* True
Keyword arguments: {}
I think this is just a function of how SavedModel
s work; you'll need multiple signatures for this to work as you expect.
Have a look at the SavedModel
guide for details.
I'm trying to use the index on my model which requires a dict as inputs (similar to [https://www.tensorflow.org/recommenders/examples/deep_recommenders#combined_model](this tutorial)). How do I input a query correctly when I call the index? I'm getting multiple errors.
This is my last attempt: