2017-06-30 112 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作为输入被传递,我不能猜测它的类型为问题。 –

相关问题