python – TypeError:’MatMul’Op的输入’b’的类型为float32,与参数’a’的int32类型不匹配
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – TypeError:’MatMul’Op的输入’b’的类型为float32,与参数’a’的int32类型不匹配,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2653字,纯文字阅读大概需要4分钟。
内容图文
我正在尝试关注word2vec示例,但我收到此错误:
TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.
在这一行
相似性= tf.matmul(
tf.cast(valid_embeddings,tf.int32),tf.cast(normalized_embeddings,tf.int32),transpose_b = True)
这是整个代码:
graph = tf.Graph()
with graph.as_default():
# Input data.
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)
# Ops and variables pinned to the CPU because of missing GPU implementation
with tf.device('/cpu:0'):
# Look up embeddings for inputs.
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, train_inputs)
# 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)))
nce_biases = tf.Variable(tf.zeros([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, embed, train_labels,
num_sampled, vocabulary_size))
# Construct the SGD optimizer using a learning rate of 1.0.
optimizer = tf.train.GradientDescentOptimizer(1.0).minimize(loss)
# Compute the cosine similarity between minibatch examples and all embeddings.
norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keep_dims=True))
normalized_embeddings = embeddings / norm
valid_embeddings = tf.nn.embedding_lookup(
normalized_embeddings, valid_dataset)
similarity = tf.matmul(
tf.cast(valid_embeddings,tf.int32), tf.cast(normalized_embeddings,tf.int32), transpose_b=True)
# Add variable initializer.
init = tf.initialize_all_variables()
我怎样才能解决这个问题?
解决方法:
我使用Tensorflow r1.4和Python 3.4遇到了同样的问题.
实际上,我认为你需要改变代码
tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,
num_sampled, vocabulary_size))
成
tf.nn.nce_loss(nce_weights, nce_biases, train_labels, embed,
num_sampled, vocabulary_size))
要么
loss = tf.reduce_mean(tf.nn.nce_loss(
weights = softmax_weights,
biases = softmax_biases,
inputs = embed,
labels = train_labels,
num_sampled = num_sampled,
num_classes = vocabulary_size))
同时,您需要将代码更改回
similarity = tf.matmul(valid_embeddings, tf.transpose(normalized_embeddings))
使用tf.cast(…,tf.int32)是错误的,实际上,没有必要使用tf.cast(…,tf.float32),因为它已经是tf.float32.
附:
当您在使用tf.nn.sampled_softmax_loss()时遇到问题时,该解决方案也很有用,因为sampled_softmax_loss()的使用与nce_loss()非常相似.
内容总结
以上是互联网集市为您收集整理的python – TypeError:’MatMul’Op的输入’b’的类型为float32,与参数’a’的int32类型不匹配全部内容,希望文章能够帮你解决python – TypeError:’MatMul’Op的输入’b’的类型为float32,与参数’a’的int32类型不匹配所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。