tf.keras.Model.fit不训练模型

时间:2018-08-18 22:18:27

标签: tensorflow keras deep-learning

我对tf.keras遇到了奇怪的问题。我可以使用显式训练循环-Dlog4j.configurationFile=<PATH>/log4j2.xml 来训练模型,但不能使用sess.run([loss, train_op], feed_dict)管道来训练模型。感觉像是这种情况下没有将渐变应用于变量。有最简单的代码可以重现该问题:

.compile() .fit()

输出:

import tensorflow as tf
cifar10 = tf.keras.datasets.cifar10.load_data()
(X_train, y_train), (X_test, y_test) = cifar10

image_shape = (32, 32, 3)

model = tf.keras.Sequential([
    tf.layers.Flatten(input_shape=image_shape),
    tf.layers.Dense(128, activation=tf.nn.relu),
    tf.layers.Dense(10)
])
model.compile('adam', 'sparse_categorical_crossentropy')
model.fit(X_train, y_train, epochs=5)

1 个答案:

答案 0 :(得分:0)

您正在执行分类,这意味着输出应具有softmax激活功能。您的模型没有激活,因此这些值对损失毫无意义,这会导致优化过程不佳。

将模型更改为:

model = tf.keras.Sequential([
    tf.layers.Flatten(input_shape=image_shape),
    tf.layers.Dense(128, activation=tf.nn.relu),
    tf.layers.Dense(10, activation=tf.nn.softmax)
])

它应该表现得更好,但还要记住,这是一个非常简单的模型,它在CIFAR 10上不能很好地工作。

相关问题