使用张量流层,模型未经过训练

时间:2017-06-30 09:14:22

标签: python tensorflow layer

之前我在Theano之上使用了keras,现在想要编写tensorflow风格的代码,这对我来说是新的。我尝试编写一个非常简单的模型(我在keras上实现并且它有效),但是训练过程似乎不起作用。无论我走多少个时代,模型总是做出相同的预测,这表明模型在训练过程中根本没有更新。我想我一定是误解了一些东西并犯了一个愚蠢的错误,但找不到它的位置。

我确信输入数据和标签是正确的,因为我之前使用过它们。输入数据training_input [0]和training_input [1]分别是2D numpy数组。标签是单热的,有4个尺寸。

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)

1 个答案:

答案 0 :(得分:1)

您在最后一层上应用softmax次激活两次。 tf.losses.softmax_cross_entropy函数在内部应用softmax,因此请通过设置logits删除activation=None上的激活。