TypeError:无法将值<tensorflow.python.keras.losses.CategoricalCrossentropy对象...>转换为TensorFlow DType

时间:2020-11-12 17:40:04

标签: python tensorflow keras tensorflow2.0 gradient

我想使用带有纯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.

1 个答案:

答案 0 :(得分:1)

tf.keras.losses.CategoricalCrossentropy需要先实例化,然后才能被调用:

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

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

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