MatMul Op类型float32与int32类型不匹配

时间:2017-08-22 21:29:11

标签: python tensorflow

我正在尝试运行这段代码:

batch_size = 128
embedding_size = 128
skip_window = 1
num_skips = 2
valid_size = 16
valid_window = 100
valid_examples = np.array(random.sample(range(valid_window), valid_size))
num_sampled = 64
graph = tf.Graph()
with graph.as_default(), tf.device('/cpu:0'):
    train_dataset = 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)
    embeddings = tf.Variable(
        tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0))
    softmax_weights = tf.Variable(
        tf.truncated_normal([vocabulary_size, embedding_size],
        stddev=1.0 / math.sqrt(embedding_size)))
    softmax_biases = tf.Variable(tf.zeros([vocabulary_size]))
    embed = tf.nn.embedding_lookup(embeddings, train_dataset)
    loss = tf.reduce_mean(
        tf.nn.sampled_softmax_loss(softmax_weights, softmax_biases, embed,
            train_labels, num_sampled, vocabulary_size))
    optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss)
    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(valid_embeddings,tf.transpose(normalized_embeddings))

它是从Udacity的tensorflow_workspace项目中提取的,具体来说,这是program,我收到了这个错误:

$ python udacity_word2vec.py 
reading data
Data size 17005207
Most common words (+UNK) [['UNK', 418391], ('the', 1061396), ('of', 593677), ('and', 416629), ('one', 411764)]
Sample data [5239, 3084, 12, 6, 195, 2, 3137, 46, 59, 156]
data: ['anarchism', 'originated', 'as', 'a', 'term', 'of', 'abuse', 'first']
 with num_skips = 2 and skip_window = 1:
 with num_skips = 4 and skip_window = 2:
Traceback (most recent call last):
  File "udacity_word2vec.py", line 148, in <module>
    train_labels, num_sampled, vocabulary_size))
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_impl.py", line 1247, in sampled_softmax_loss
    name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/nn_impl.py", line 981, in _compute_sampled_logits
    sampled_logits = math_ops.matmul(inputs, sampled_w, transpose_b=True)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/math_ops.py", line 1844, in matmul
    a, b, transpose_a=transpose_a, transpose_b=transpose_b, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/ops/gen_math_ops.py", line 1289, in _mat_mul
    transpose_b=transpose_b, name=name)
  File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/op_def_library.py", line 526, in apply_op
    inferred_from[input_arg.type_attr]))
TypeError: Input 'b' of 'MatMul' Op has type float32 that does not match type int32 of argument 'a'.

我已经尝试过查看解释问题的许多其他链接,但所有这些链接都解决了函数&#39; MatMul&#39;的直接用法问题。我不在这里使用它。无论如何,这是直接从教程中提取的,所以我确定它已经过测试,它应该可以工作......

有谁知道发生了什么?我已经被困在这个问题太久了。

1 个答案:

答案 0 :(得分:1)

啊,是的。

试试这个:

batch_size = 128
embedding_size = 32
voc_size = len(data["dict"])
num_sampled = 64
# definitions
graph = tf.Graph()
with graph.as_default(), tf.device('/cpu:0'):
    train_dataset = tf.placeholder(tf.int32, shape=[batch_size])
    train_labels = tf.placeholder(tf.int32, shape=[batch_size, 1])
    embeddings = tf.Variable(
        tf.random_uniform([voc_size, embedding_size], -1.0, 1.0))
    softmax_weights = tf.Variable(
        tf.truncated_normal([voc_size, embedding_size],
        stddev=1.0 / math.sqrt(embedding_size)))
    softmax_biases = tf.Variable(tf.zeros([voc_size]))
    embed = tf.nn.embedding_lookup(embeddings, train_dataset)
    loss = tf.reduce_mean(
        tf.nn.sampled_softmax_loss(\
                weights=softmax_weights,\
                biases=softmax_biases,\
                inputs=embed,\
                labels=train_labels,\
                num_sampled=num_sampled,\
                num_classes=voc_size))
    optimizer = tf.train.AdagradOptimizer(1.0).minimize(loss)
    norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, 
        keep_dims=True))
    normalized_embeddings = embeddings / norm