我正在尝试使用tensorflow-recommenders库开发一个非常基本的检索模型。我的数据集包含userid,itemid,genre和value(我没有在检索模型中使用value特征)。流派特征是RaggedTensor,这意味着一个项目可以属于单个或多个长度可变的流派。这就是我的数据集的样子:
x1c 0d1x的数据
对于建模,我有以下结构:
用户型号:
class UserModel(tf.keras.Model):
def __init__(self,vocab_user_ids,embedding_dimension):
super().__init__()
self.user_embeddings = tf.keras.Sequential([
tf.keras.layers.StringLookup(vocabulary=vocab_user_ids, mask_token=None),
tf.keras.layers.Embedding(len(vocab_user_ids) + 1, embedding_dimension),
])
def call(self, userid):
return self.user_embeddings(userid)
字符串
项目型号:
class ItemModel(tf.keras.Model):
def __init__(self, vocab_item_ids, vocab_genres, embedding_dimension):
super().__init__()
self.item_embeddings = tf.keras.Sequential([
tf.keras.layers.StringLookup(vocabulary=vocab_item_ids, mask_token=None),
tf.keras.layers.Embedding(len(vocab_item_ids) + 1, embedding_dimension),
])
self.genre_embedding = tf.keras.Sequential([
tf.keras.layers.StringLookup(vocabulary=vocab_genres, mask_token=None),
tf.keras.layers.Embedding(len(vocab_genres) + 1, embedding_dimension),
])
def call(self, inputs):
item_embedding = self.item_embeddings(inputs['itemid'])
genre_embedding = self.genre_embedding(inputs['genre'])
if isinstance(genre_embedding, tf.RaggedTensor):
genre_embedding=tf.reduce_mean(genre_embedding,axis=1)
else:
genre_embedding=tf.reduce_mean(genre_embedding,axis=0)
combined_embedding = tf.keras.layers.Multiply()([item_embedding*0.4, genre_embedding*0.6])
return combined_embedding
型
推荐型号:
#Recommender Model
class RecommenderModel(tf.keras.Model):
def __init__(self,user_model,item_model, candidates):
super().__init__()
self.user_model=user_model
self.item_model=item_model
self.task = tfrs.tasks.Retrieval(
metrics=tfrs.metrics.FactorizedTopK(
candidates=candidates.batch(batch_size).map(self.item_model),
ks=[1,5,10,50,100]
),
)
def call(self, inputs: Dict[Text, tf.Tensor],training=False):
user_id_embedding = self.user_model(inputs['userid'])
item_embedding = self.item_model(inputs)
return self.task(user_id_embedding,item_embedding,compute_metrics= training)
def compute_loss(self, features: Dict[Text, tf.Tensor], training=False) -> tf.Tensor:
# generate embedding for the user ids
user_id_embedding = self.user_model(features['userid'])
# generate embedding for the item ids
item_embedding = self.item_model(features)
return self.task(user_id_embedding,item_embedding)
型
模型编译和训练代码:
model = RecommenderModel(UserModel(vocab_user_ids,32),ItemModel(vocab_item_ids,unique_genres,32),tf_data)
model.compile(optimizer=tf.keras.optimizers.Adagrad(learning_rate=0.1),loss=loss)
# shuffle and split data: train, valid, test
# set seed
tf.random.set_seed(42)
# total data points
N = data.shape[0]
# total train data points
N_train = int(0.8 * N)
# total valid data points
#N_valid = int(0.2 * N)
# total test data points
N_test = N - (N_train)
#N_test = N - (N_train + N_valid)
# shuffle data
shuffled = tf_data.shuffle(N, seed=42, reshuffle_each_iteration=False)
# # split data
num_epochs = 50
train = shuffled.take(N_train)
test = shuffled.skip((N_train)).take(N_test)
cached_train = train.batch(batch_size).cache()
cached_valid = test.batch(batch_size).cache()
model.fit(cached_train,epochs=10,batch_size=batch_size)
型
但我得到以下错误:
TypeError Traceback (most recent call last)
File <command-2894041669182423>:1
----> 1 model.fit(cached_train,epochs=10,batch_size=batch_size)
File /databricks/python/lib/python3.9/site-packages/mlflow/utils/autologging_utils/safety.py:435, in safe_patch.<locals>.safe_patch_function(*args, **kwargs)
420 if (
421 active_session_failed
422 or autologging_is_disabled(autologging_integration)
(...)
429 # warning behavior during original function execution, since autologging is being
430 # skipped
431 with set_non_mlflow_warnings_behavior_for_current_thread(
432 disable_warnings=False,
433 reroute_warnings=False,
434 ):
--> 435 return original(*args, **kwargs)
437 # Whether or not the original / underlying function has been called during the
438 # execution of patched code
439 original_has_been_called = False
File /local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/keras/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
67 filtered_tb = _process_traceback_frames(e.__traceback__)
68 # To get the full stack trace, call:
69 # `tf.debugging.disable_traceback_filtering()`
---> 70 raise e.with_traceback(filtered_tb) from None
71 finally:
72 del filtered_tb
File /tmp/__autograph_generated_filex0pt7t50.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
13 try:
14 do_return = True
---> 15 retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
16 except:
17 do_return = False
TypeError: in user code:
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/keras/engine/training.py", line 1284, in train_function *
return step_function(self, iterator)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/keras/engine/training.py", line 1268, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/keras/engine/training.py", line 1249, in run_step **
outputs = model.train_step(data)
File "/local_disk0/.ephemeral_nfs/cluster_libraries/python/lib/python3.9/site-packages/keras/engine/training.py", line 1051, in train_step
loss = self.compute_loss(x, y, y_pred, sample_weight)
TypeError: compute_loss() takes from 2 to 3 positional arguments but 5 were given
型
我试图找出错误,但我不能。到目前为止,我可以看到,我准确地覆盖了compute_loss()函数。有人能指出我在这里遗漏了什么或者做错了什么导致了这个错误吗?
1条答案
按热度按时间tzdcorbm1#
我从
tf.keras.Model
继承了recommender类,而它应该被tfrs.Model
继承。字符串