问题描述
我在 Tensor Flow 中实现了一个词袋,并得到了一个
I was working on a Bag of Words implementation in Tensor Flow and got a
TypeError: Input 'y' of 'Mul' Op 的类型 float32 与参数 'x' 的类型 int32 不匹配.
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.
在 tf.nn.nce_loss 中.我尝试查看进入 tf.nn.nce_loss 的类型并尝试强制它们无济于事.任何帮助,将不胜感激.我在 jupyter 笔记本中使用 python 工作.
in the tf.nn.nce_loss. I tried looking at the types going into tf.nn.nce_loss and tried coercing them to no avail. Any help would be appreciated. I was working in python in a jupyter notebook.
完整代码包括数据拉取,请访问 https://gist.github.com/gammaguy/683a1357fdb044d0abbd897a7179d525
The full code is including data pull is available at https://gist.github.com/gammaguy/683a1357fdb044d0abbd897a7179d525
graph = tf.Graph()
with graph.as_default():
# Input data.
train_inputs = tf.placeholder(tf.int32,shape=[batch_size, skip_window * 2],name="train_inputs")
train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1],name="train_labels")
valid_dataset = tf.constant(valid_examples, dtype=tf.int32,name="valid_dataset")
# train_inputs = tf.placeholder(tf.int32,shape=[batch_size, skip_window * 2],name="train_inputs")
# train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1],name="train_labels")
# valid_dataset = tf.constant(valid_examples, dtype=tf.int32,name="valid_dataset")
with tf.device('/cpu:0'):
# Look up embeddings for inputs.
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0),name="embeddings")
# Embedding size is calculated as shape(train_inputs) + shape(embeddings)[1:]
embed = tf.nn.embedding_lookup(embeddings, train_inputs,name="embed")
reduced_embed = tf.div(tf.reduce_sum(embed, 1), skip_window*2,name="reduced_embed")
# Construct the variables for the NCE loss
nce_weights = tf.Variable(
tf.truncated_normal([vocabulary_size, embedding_size],
stddev=1.0 / math.sqrt(embedding_size)),name="nce_weights")
nce_biases = tf.Variable(tf.zeros([vocabulary_size]),name="nce_biases")
print(train_inputs)
print(train_labels)
print(valid_dataset)
print(embeddings)
print(embed)
print(reduced_embed)
print(nce_weights)
print(nce_biases)
print(num_sampled)
print(vocabulary_size)
# Compute the average NCE loss for the batch.
# tf.nce_loss automatically draws a new sample of the negative labels each
# time we evaluate the loss.
loss = tf.reduce_mean(
tf.nn.nce_loss(nce_weights, nce_biases, reduced_embed, train_labels,
num_sampled, vocabulary_size))
输出
Tensor("train_inputs:0", shape=(128, 2), dtype=int32)
Tensor("train_labels:0", shape=(128, 1), dtype=int32)
Tensor("valid_dataset:0", shape=(16,), dtype=int32)
<tf.Variable 'embeddings:0' shape=(82297, 128) dtype=float32_ref>
Tensor("embed:0", shape=(128, 2, 128), dtype=float32, device=/device:CPU:0)
Tensor("reduced_embed:0", shape=(128, 128), dtype=float32, device=/device:CPU:0)
<tf.Variable 'nce_weights:0' shape=(82297, 128) dtype=float32_ref>
<tf.Variable 'nce_biases:0' shape=(82297,) dtype=float32_ref>
64
82297
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
490 as_ref=input_arg.is_ref,
--> 491 preferred_dtype=default_dtype)
492 except TypeError as err:
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype)
703 if ret is None:
--> 704 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
705
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in _TensorTensorConversionFunction(t, dtype, name, as_ref)
576 "Tensor conversion requested dtype %s for Tensor with dtype %s: %r"
--> 577 % (dtype.name, t.dtype.name, str(t)))
578 return t
ValueError: Tensor conversion requested dtype int32 for Tensor with dtype float32: 'Tensor("nce_loss/Reshape_1:0", shape=(?, 1, ?), dtype=float32)'
During handling of the above exception, another exception occurred:
TypeError Traceback (most recent call last)
<ipython-input-7-55d5813d0e24> in <module>()
42 loss = tf.reduce_mean(
43 tf.nn.nce_loss(nce_weights, nce_biases, reduced_embed, train_labels,
---> 44 num_sampled, vocabulary_size))
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_impl.py in nce_loss(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, remove_accidental_hits, partition_strategy, name)
1164 remove_accidental_hits=remove_accidental_hits,
1165 partition_strategy=partition_strategy,
-> 1166 name=name)
1167 sampled_losses = sigmoid_cross_entropy_with_logits(
1168 labels=labels, logits=logits, name="sampled_losses")
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/nn_impl.py in _compute_sampled_logits(weights, biases, labels, inputs, num_sampled, num_classes, num_true, sampled_values, subtract_log_q, remove_accidental_hits, partition_strategy, name)
999 row_wise_dots = math_ops.multiply(
1000 array_ops.expand_dims(inputs, 1),
-> 1001 array_ops.reshape(true_w, new_true_w_shape))
1002 # We want the row-wise dot plus biases which yields a
1003 # [batch_size, num_true] tensor of true_logits.
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/math_ops.py in multiply(x, y, name)
276
277 def multiply(x, y, name=None):
--> 278 return gen_math_ops._mul(x, y, name)
279
280
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/ops/gen_math_ops.py in _mul(x, y, name)
1432 A `Tensor`. Has the same type as `x`.
1433 """
-> 1434 result = _op_def_lib.apply_op("Mul", x=x, y=y, name=name)
1435 return result
1436
/home/paul/.local/lib/python3.5/site-packages/tensorflow/python/framework/op_def_library.py in apply_op(self, op_type_name, name, **keywords)
525 "%s type %s of argument '%s'." %
526 (prefix, dtypes.as_dtype(attrs[input_arg.type_attr]).name,
--> 527 inferred_from[input_arg.type_attr]))
528
529 types = [values.dtype]
TypeError: Input 'y' of 'Mul' Op has type float32 that does not match type int32 of argument 'x'.
推荐答案
nce_loss(
weights,
biases,
labels,
inputs,
num_sampled,
num_classes,
num_true=1,
sampled_values=None,
remove_accidental_hits=False,
partition_strategy='mod',
name='nce_loss'
)
将 reduced_embed
与 train_labels
交换.
这篇关于在 Tensorflow tf.nn.nce_loss 中得到 TypeError: Input 'y' of 'Mul' Op 的类型 float32 与参数 'x' 的类型 int32 不匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!