I’m making a recommendation system of the H&M Personalized Fashion Recommendations dataset so that it gives a user who bought a dress some recommendations on 2-3 other dresses he might be interested in, however I’m getting this error and I can’t move forward, could someone give me a hand? Thanks in advance
ratings = transactions[["customer_id", "article_id", "price"]]
ratings["customer_id"] = ratings["customer_id"].astype(str)
ratings["article_id"] = ratings["article_id"].astype(str)
user_ids = tf.constant(ratings["customer_id"].values, dtype=tf.string)
article_ids = tf.constant(ratings["article_id"].values, dtype=tf.string)
class HMRecommendationModel(tfrs.Model):
def __init__(self):
super().__init__()
# Definisci gli embedding per gli utenti e gli articoli
self.user_embeddings = tf.keras.layers.StringLookup(mask_token=None)
self.article_embeddings = tf.keras.layers.StringLookup(mask_token=None)
self.task = tfrs.tasks.Retrieval(metrics=tfrs.metrics.FactorizedTopK(articles["article_id"].unique()))
def compute_loss(self, features, training=False):
user_embeddings = self.user_embeddings(features["customer_id"])
article_embeddings = self.article_embeddings(features["article_id"])
return self.task(user_embeddings, article_embeddings)
# Addestra il modello
model = HMRecommendationModel()
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1))
model.fit({"customer_id": user_ids, "article_id": article_ids}, epochs=3)
user_id = "user_id_da_considerare"
_, recommended_articles = model({"customer_id": tf.constant([user_id], dtype=tf.string)})
print(f"Top 5 raccomandazioni per l'utente {user_id}: {recommended_articles[0, :5]}")
error:
ValueError: in user code:
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1401, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1384, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.10/dist-packages/keras/src/engine/training.py", line 1373, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.10/dist-packages/tensorflow_recommenders/models/base.py", line 68, in train_step
loss = self.compute_loss(inputs, training=True)
File "<ipython-input-8-3e90eeed72fc>", line 16, in compute_loss
return self.task(user_embeddings, article_embeddings)
File "/usr/local/lib/python3.10/dist-packages/keras/src/utils/traceback_utils.py", line 70, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/tmp/__autograph_generated_file91f6wgvn.py", line 42, in tf__call
scores = ag__.converted_call(ag__.ld(tf).linalg.matmul, (ag__.ld(query_embeddings), ag__.ld(candidate_embeddings)), dict(transpose_b=True), fscope)
ValueError: Exception encountered when calling layer 'retrieval' (type Retrieval).
in user code:
File "/usr/local/lib/python3.10/dist-packages/tensorflow_recommenders/tasks/retrieval.py", line 160, in call *
scores = tf.linalg.matmul(
ValueError: Shape must be rank 2 but is rank 1 for '{{node retrieval/MatMul}} = MatMul[T=DT_INT64, transpose_a=false, transpose_b=true](string_lookup/Identity, string_lookup_1/Identity)' with input shapes: [?], [?].
Call arguments received by layer 'retrieval' (type Retrieval):
• query_embeddings=tf.Tensor(shape=(None,), dtype=int64)
• candidate_embeddings=tf.Tensor(shape=(None,), dtype=int64)
• sample_weight=None
• candidate_sampling_probability=None
• candidate_ids=None
• compute_metrics=True
• compute_batch_metrics=True