tensorflow 在批处理训练循环中,GradientTape中的无梯度

8xiog9wr  于 2023-05-07  发布在  其他
关注(0)|答案(1)|浏览(153)

我是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]))

感谢您的任何帮助!

agxfikkp

agxfikkp1#

这对我很有效:只要在梯度带内做tf.nn.embedding_lookup

with tf.GradientTape() as tape:
    user_batch = tf.nn.embedding_lookup(user_embedding, user_ids) # shape = batch_size x embedding_dims
    item_batch = tf.nn.embedding_lookup(item_embedding, item_ids) # shape = batch_size x embedding_dims

    ## Compute the predicted ratings
    true_ratings = tf.cast(train_batch_st.values, tf.float32)
    predicted_ratings = tf.reduce_sum(user_batch * item_batch, axis=1)

    ## Compute loss
    # Using MSE here
    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])

# Apply gradients (update user and item embeddings)
optimizer.apply_gradients(zip(grads, [user_embedding, item_embedding]))

相关问题