我是TensorFlow的新手,我正在尝试在v2中实现简单的协同过滤。我一次在整个训练集上训练没有麻烦,但是当我尝试批量训练时就有问题了。具体地,当计算grads
时,输出梯度为[None, None]
。包含完整尝试的colab文件可以在这里找到。
with tf.GradientTape() as tape:
tape.watch(user_embedding)
tape.watch(item_embedding)
## Compute the predicted ratings
predicted_ratings = tf.reduce_sum(user_batch * item_batch, axis=1)
## Compute loss
true_ratings = tf.cast(train_batch_st.values, tf.float32)
loss = tf.losses.mean_squared_error(true_ratings, predicted_ratings) # batch loss
# Cumulative epoch loss (across all batches)
epoch_loss += loss
## Compute gradients of loss with respect to user and item embeddings
grads = tape.gradient(loss, [user_embedding, item_embedding])
print(grads) # grads None, None thus causing error below
# Apply gradients
optimizer.apply_gradients(zip(grads, [user_embedding, item_embedding]))
感谢您的任何帮助!
1条答案
按热度按时间agxfikkp1#
这对我很有效:只要在梯度带内做
tf.nn.embedding_lookup
。