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