2017-04-11 37 views
3

當我在Linux上使用此代碼時。有用。但在Windows上它並沒有。順便說一句,我的蟒蛇版本是3.5我的窗戶上'Mul'的輸入'y'的類型爲float32,與參數'x'的類型int32不匹配

with graph.as_default(): 

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) 


with tf.device('/cpu:0'): 

embeddings = tf.Variable(
    tf.random_uniform([vocabulary_size, embedding_size], -1.0, 1.0)) 
embed = tf.nn.embedding_lookup(embeddings, train_inputs) 


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


loss = tf.reduce_mean(
    tf.nn.nce_loss(nce_weights, nce_biases, embed, train_labels,num_sampled, vocabulary_size)) 
+0

我檢查嵌入類型(float32)和train_labels(int32)。我應該改變其中的一種嗎?如何? – HanLuo

回答

0

我也遇到了這個錯誤。代碼非常類似於你的代碼。 當我在Floydhub上使用env = tensorflow(這意味着Python3上的Tensorflow 1.1.0 + Keras 2.0.4)運行它時,它拋出了上述錯誤。

但是,在我改變環境使用tensorflow-1.0(Python3上的Tensorflow 1.0.0 + Keras 1.2.2)後,它運行良好。

0

您需要將train_labels類型轉換爲float32。 [你已經提到,train_labels是int32型和嵌入是float32類型。]

這是你如何轉換的Int32類型FLOAT32

tf.cast(train_labels, tf.float32) 

然後計算出的損失。

1

tensorflow的新版本已經改變了ncs_loss的參數順序。

試圖改變爲

tf.nn.nce_loss(nce_weights, nce_biases, train_labels, embed, num_sampled, vocabulary_size)

0

我遇到了同樣的問題,但不同的損失函數。您缺少參數名稱,傳遞參數名稱和錯誤將消失。檢查下面的代碼行例如。

loss = tf.reduce_mean(
        tf.nn.nce_losss(weights=nce_weights, biases=nce_biases, 
            inputs=embed, labels=train_labels, 
            num_sampled=num_sampled, 
            num_classes=vocabulary_size)) 
相關問題