TypeError: Cannot convert value <tensorflow.python.keras.losses.CategoricalCrossentropy object ...> to a TensorFlow DType

TypeError: Cannot convert value <tensorflow.python.keras.losses.CategoricalCrossentropy object ...> to a TensorFlow DType

我想使用纯 TensorFlow 2 的负采样来实现 Word2Vec。当我想计算梯度时,我在最后一行遇到了这个错误。我正在努力寻找问题。

代码相当简单:

import tensorflow as tf
import numpy as np

x, y = (('self', 'the'), ('self', 'violent'), ('self', 'any')), (1, 0, 0)
y = tf.convert_to_tensor(y, dtype='float32')

embeding_tensor = tf.keras.layers.Embedding(len(words_lst), embeding_size)
context_tensor = tf.keras.layers.Embedding(len(words_lst), embeding_size)


with tf.GradientTape() as t:    
      middle = embeding_tensor(word2index[x[0][0]])
      neighbor_choices = context_tensor(np.asarray([[word2index[i[1]] for i in x]]))

      scores = tf.tensordot(neighbor_choices, middle, 1)
      prediction = tf.nn.sigmoid(scores)
      loss = tf.keras.losses.CategoricalCrossentropy(y, prediction)

      g_embed, g_context = t.gradient(loss, [middle, neighbor_choices])

TypeError                                 Traceback (most recent call last)
<ipython-input-40-fba4cda17cff> in <module>()
     17       loss = tf.keras.losses.CategoricalCrossentropy(y, prediction)
     18 
---> 19       g_embed, g_context = t.gradient(loss, [middle, neighbor_choices])

2 frames
/usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/dtypes.py in as_dtype(type_value)
    648 
    649   raise TypeError("Cannot convert value %r to a TensorFlow DType." %
--> 650                   (type_value,))

TypeError: Cannot convert value <tensorflow.python.keras.losses.CategoricalCrossentropy object at 0x7f3ec9be28d0> to a TensorFlow DType.

tf.keras.losses.CategoricalCrossentropy调用前需要实例化:

loss = tf.keras.losses.CategoricalCrossentropy()(y, prediction)

您也可以只使用 tf.keras.losses.categorical_crossentropy:

loss = tf.keras.losses.categorical_crossentropy(y, prediction)