2017-06-30 165 views
1

我之前在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) 
+0

確定終於我發現我的問題。我用numpy數組作爲一個熱矢量而不是張量。所以我需要添加一行'labels = tf.cast(labels,tf.int32)'。希望顯示我的錯誤可以幫助其他人。 – ymeng

回答

1

您正在對最後一層應用softmax激活兩次。 tf.losses.softmax_cross_entropy函數內部應用softmax,因此通過設置activation=None來刪除logits上的激活。

+0

謝謝,這確實是一個錯誤,我糾正它。然而,這並不是模型根本不起作用的原因。改正後仍然存在問題。我還打印出預測結果,對於任何輸入,輸出(logits)完全相同。出於某種原因,輸入數據無法正確迭代,或者模型存在根本偏差。我會仔細看看的。 – ymeng

+0

我只能在那裏顯示的代碼中找到一個錯誤。 Logits作爲輸入被傳遞,我不能猜測它的類型爲問題。 –

相關問題