Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用tensorflow层,未训练模型_Python_Tensorflow_Layer - Fatal编程技术网

Python 使用tensorflow层,未训练模型

Python 使用tensorflow层,未训练模型,python,tensorflow,layer,Python,Tensorflow,Layer,我以前在Theano上使用过keras,现在想用tensorflow风格编写代码,这对我来说是新的。我试着编写一个非常简单的模型(我在keras上实现了这个模型,它成功了),但培训过程似乎不起作用。无论我经历了多少个时代,模型总是做出相同的预测,这表明模型在训练过程中根本没有更新。我想我一定是误会了什么,犯了一个愚蠢的错误,但找不到它在哪里 我确信输入的数据和标签是正确的,因为我以前使用过它们。输入数据training_Input[0]和training_Input[1]分别是2D numpy数

我以前在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)

您在最后一层上应用了两次
softmax
激活。
tf.loss.softmax\u cross\u entropy
函数在内部应用
softmax
,因此通过设置
activation=None
删除
logits上的激活

谢谢,这确实是一个错误,我纠正了它。然而,这并不是模型根本不起作用的原因。经过修正后,它仍然存在问题。我还打印了预测,对于任何输入,输出(logits)都是完全相同的。出于某种原因,要么输入数据没有正确迭代,要么模型存在严重偏差。我会调查的。我只能在那里显示的代码中找到一个bug。Logits被作为输入传递,我猜不出它的类型是问题。好的,最后我找到了我的问题。我使用numpy数组作为一个热向量,而不是张量。所以我需要添加一行
labels=tf.cast(labels,tf.int32)
。希望我的错误能帮助别人。