2017-08-26 72 views
0

我試圖按照word2vec的例子,但我得到這個錯誤:類型錯誤:的「MATMUL」歐普輸入「B」的類型是FLOAT32不匹配參數的INT32輸入「a」

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 =真)

這是整個代碼:

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() 

我該如何解決這個問題?

回答

0

你爲什麼要在整數空間中進行矩陣乘法?你可能希望這兩個tf.cast都是tf.float32。

相關問題