“Mul”操作的输入“y”的类型为 float32,与参数“x”的 int32 类型不匹配

Han*_*Luo 9 python-3.x

当我在 Linux 上使用此代码时。有用。但在 Windows 上则不然。顺便说一下,我的 Windows 上的 python 版本是 3.5

with graph.as_default():

 train_inputs = tf.placeholder(tf.int32, shape=[batch_size])
 train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
 valid_dataset = tf.constant(valid_examples, dtype=tf.int32)


with tf.device('/cpu:0'):

 embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
 embed = tf.nn.embedding_lookup(embeddings, train_inputs)


 nce_weights = tf.Variable(
    tf.truncated_normal([vocabulary_size, embedding_size],
                        stddev=1.0 / math.sqrt(embedding_size)))
 nce_biases = tf.Variable(tf.zeros([vocabulary_size]))


loss = tf.reduce_mean(
  tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,num_sampled, vocabulary_size))
Run Code Online (Sandbox Code Playgroud)

Sri*_*ddy 12

您需要将 train_labels 类型转换为float32. [您已经提到过 train_labels 是类型int32,而 embed 是类型float32。]

这就是将 int32 类型转换为 float32 的方法

tf.cast(train_labels, tf.float32)
Run Code Online (Sandbox Code Playgroud)

然后计算损失。


小智 0

我也遇到了这个错误。代码与您的非常相似。当我使用 env=tensorflow (这意味着 Python3 上的 Tensorflow 1.1.0 + Keras 2.0.4)在 Floydhub 上运行它时,它抛出了上述错误。

但是,在我更改环境以使用tensorflow-1.0(Python3上的Tensorflow 1.0.0 + Keras 1.2.2)后,它运行良好。