【问题标题】:MatMul Op type float32 does not match type int32MatMul Op 类型 float32 与 int32 类型不匹配
【发布时间】:2017-08-22 21:29:10
【问题描述】:

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

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'.

我已尝试查看许多其他链接来解释该问题,但它们都解决了函数“MatMul”的直接用法。我没有在这里使用它。无论如何,这是直接从教程中提取的,所以我确信它已经过测试并且应该可以工作......

有人知道发生了什么吗?我被困在这件事上太久了。

【问题讨论】:

    标签: python tensorflow


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

    【讨论】:

      猜你喜欢
      • 2016-07-12
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-04
      • 2021-01-06
      • 1970-01-01
      相关资源
      最近更新 更多