recommenders
recommenders copied to clipboard
Candidate tower multiple features
Hi Im trying to add a new feature to the candidate tower (product title) as well as the ID of the product. I followed the same process as the user tower however i get a error from this line
self.task = tfrs.tasks.Retrieval(
metrics=tfrs.metrics.FactorizedTopK(
candidates=products.batch(2048*4).map(self.candidate_model),
),
)
I think its because I have multiple features in the cadidate model but I dont know how to solve
If you're trying to get help you should include more information about your error - it's really hard to say what's going wrong without knowing any details!
Archive.zip sorry, I have atached some data and the notebook file for what Im trying to do. It failed when I try to compile the model and the error points to the product model (seems to me something about how im importing the data to it but dont know how to solve) thanks
A couple of suggestions to get help more quickly:
- Directly paste your error message here (with appropriate code formatting).
- When sharing code, put it on Github, or provide a link to a Colab.
It's very unlikely the anyone will ever download a zip file you attach!
Sorry, never asked a question like this before. The git hub link is https://github.com/AndrewJGroves/Recomendation
The error it gives out is shown below (hope this is the formating you meant)
TypeError Traceback (most recent call last)
in ----> 1 model = RecsModel() 2 model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.1)) 15 frames /tmp/__autograph_generated_file76h_4ngn.py in tf__call(self, inputs) 10 try: 11 do_return = True ---> 12 retval_ = ag__.converted_call(ag__.ld(tf).concat, ([ag__.converted_call(ag__.ld(self).title_embedding, (ag__.ld(inputs)['titles'],), None, fscope), ag__.converted_call(ag__.ld(self).title_text_embedding, (ag__.ld(inputs)['titles'],), None, fscope), ag__.converted_call(ag__.ld(self).int_embedding, (ag__.ld(inputs)['int'],), None, fscope)],0;3... 13 except: 14 do_return = False TypeError: Exception encountered when calling layer "product_model" (type ProductModel). in user code:
File "<ipython-input-7-496d97c89354>", line 77, in call *
self.int_embedding(inputs["int"])
TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got 'titles'
Call arguments received by layer "product_model" (type ProductModel): • inputs=tf.Tensor(shape=(None,), dtype=string)
Thanks!
It looks like you expect inputs to be a dictionary, but it's actually a tensor. Have you tried tracing the data flow through your model, and figuring out where your expectations diverge from what's actually happening?
Your products need to have the same structure as inputs to the candidate_model, i.e.
{
"title":features["LINE_DESC"],
"int":features["int"],
}
I have exactly the same problem. In most examples, the product class accepts only one field of input, but in my case, it accepts multiple fields, so in the call method, I have to specify Inputs['field'] as you did. But somehow,
products.batch(2048*4).map(self.candidate_model)
generate error.
@AndrewJGroves Do you find a solution now?
I never coded it up as the project changed, but understood the error. When you make your input tensors, start of the code just after downloading the data, you make one with the interactions and you make another with a list of products. In the tutorials this only has a list of product titles as that's all they use, if you want to add more product features they need adding to this tensor
I just came across this link. https://github.com/tensorflow/recommenders/issues/356
Basically, the same problem. They suggested that items, as now have multiple fields, should be a dictionary.
I followed their suggestion and model.compile() passed. However, it leads to another error when I run mode.fit() haha.
@AndrewJGroves Just FYI
Hi @jackgurae have you resolved adding extra feature to candidate model? For me, model.compile() giving error even after I put candidate model feature into a dictionary. Can you please help me on that?
The model becomes compilable if you fix the error with products as mentioned above, e.g.
products = product.map(lambda x: {"titles": x["LINE_DESC"], "int": x["LINE_NUMBER"]},num_parallel_calls=tf.data.AUTOTUNE)
And then you need to fix the error with self.title_vectorizer.adapt(products) (or comment the line out to compile).