我之前在Theano之上使用了keras,現在想要以tensorflow樣式編寫對我而言是新的代碼。我試着寫了一個非常簡單的模型(我在keras上實現並且工作正常),但是訓練過程似乎不起作用。無論我經歷多少個時代,模型都會做出相同的預測,這說明模型在訓練過程中根本沒有更新。我想我一定誤解了一些東西,犯了一個愚蠢的錯誤,但是找不到它在哪裏。使用張量流圖層,模型沒有訓練
我確定輸入數據和標籤是正確的,因爲我之前使用過它們。輸入數據training_input [0]和training_input [1]分別是二維numpy陣列。標籤是四維的一個熱門標籤。
def model_1(features, labels):
hl_input = features['hl_input']
bd_input = features['bd_input']
encoder = tf.concat([hl_input, bd_input], axis=1)
encoder = tf.layers.dense(encoder, 128, activation=tf.nn.relu)
decoder = tf.layers.dense(encoder, 64)
logits = tf.layers.dense(decoder, 4, activation=tf.nn.softmax)
predictions = tf.argmax(logits, 1, name="predictions")
loss = tf.losses.softmax_cross_entropy(onehot_labels=labels, logits=logits)
train_op = tf.contrib.layers.optimize_loss(loss, tf.contrib.framework.get_global_step(), optimizer='Adam',
learning_rate=0.1)
predictions = {"classes": predictions, "probabilities": logits}
return predictions, loss, train_op
... ...
classifier = tf.contrib.learn.Estimator(model_fn=model_1)
classifier.fit(x={'hl_input':training_input[0], 'bd_input':training_input[1]}, y=training_labels, batch_size=batch_size, steps=steps)
確定終於我發現我的問題。我用numpy數組作爲一個熱矢量而不是張量。所以我需要添加一行'labels = tf.cast(labels,tf.int32)'。希望顯示我的錯誤可以幫助其他人。 – ymeng