【问题标题】:TypeError: Cannot convert value <tensorflow.python.keras.losses.CategoricalCrossentropy object ...> to a TensorFlow DTypeTypeError:无法将值 <tensorflow.python.keras.losses.CategoricalCrossentropy object ...> 转换为 TensorFlow DType
【发布时间】:2020-11-12 17:40:04
【问题描述】:

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

【问题讨论】:

    标签: python tensorflow keras tensorflow2.0 gradient


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 2018-06-11
      • 2021-09-13
      • 2021-07-12
      • 2021-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-15
      • 2017-01-03
      相关资源
      最近更新 更多