【发布时间】:2019-05-04 01:17:13
【问题描述】:
我刚开始使用keras,我尝试为keras.datasets中的mnist数据集构建模型
这是我的初始代码:
import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
然后,我定义了一个模型:
model = tf.keras.Sequential()
model.add(tf.keras.layers.Flatten(input_shape=(28,28)))
model.add(tf.keras.layers.Dense(512, activation = tf.nn.relu))
model.add(tf.keras.layers.Dense(10, activation = tf.nn.softmax))
model.compile(loss = 'sparse_categorical_crossentropy', optimizer='rmsprop')
model.fit(train_images, train_labels, epochs=10)
我使用model.compile(loss = 'sparse_categorical_crossentropy', optimizer='rmsprop') 尝试了这个模型,并且模型训练得很好
后来,我尝试评估模型:
loss, accuracy = model.evaluate(test_images, test_labels)
print('Accuracy on the test set: '+str(accuracy))
它显示了以下错误:
10000/10000 [==============================] - 0s 50us/step
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-68-7ccd830be0cb> in <module>()
----> 1 loss, accuracy = model.evaluate(test_images, test_labels)
2 print('Accuracy on the test set: '+str(accuracy))
TypeError: 'numpy.float64' object is not iterable
但是,当我尝试使用 predictions = model.predict(test_images) 对 test_images 进行预测时,它工作正常。
我正在使用 google colab 进行编码。 请帮忙!
【问题讨论】:
-
我没有看到你的 model.fit() 方法。
-
对不起,我已经在代码中找到了,但我忘了在帖子中包含它
标签: python tensorflow keras deep-learning google-colaboratory