Sur*_*ain 2 python deep-learning keras tensorflow google-colaboratory
我刚开始使用 keras,我尝试为mnist数据集构建一个模型keras.datasets
这是我的初始代码:
import tensorflow as tf
(train_images, train_labels), (test_images, test_labels) = tf.keras.datasets.mnist.load_data()
Run Code Online (Sandbox Code Playgroud)
然后,我定义了一个模型:
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)
Run Code Online (Sandbox Code Playgroud)
我尝试使用这个模型model.compile(loss = 'sparse_categorical_crossentropy', optimizer='rmsprop')并且模型训练得很好
后来,我尝试评估模型:
loss, accuracy = model.evaluate(test_images, test_labels)
print('Accuracy on the test set: '+str(accuracy))
Run Code Online (Sandbox Code Playgroud)
它显示以下错误:
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
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试使用 对 test_images 进行预测时predictions = model.predict(test_images),它工作正常。
我正在使用 google colab 进行编码。请帮忙!
由于缺少model.compile()的指标参数,您的模型没有指标
编译
编译(优化器,损失=无,指标=无, loss_weights=无,sample_weight_mode=None,weighted_metrics=None,target_tensors=None)
调用,因此根据文档:
退货
标量测试损失(如果模型有单个输出且没有指标)或标量列表(如果模型有多个输出和/或指标)。属性 model.metrics_names 将为您提供标量输出的显示标签。
Keras 模型evaluate()只返回损失。
因此,如果您更改代码:
model.compile(loss='sparse_categorical_crossentropy', metrics=['accuracy'], optimizer='rmsprop')
Run Code Online (Sandbox Code Playgroud)
你也可以获得准确性。
| 归档时间: |
|
| 查看次数: |
3126 次 |
| 最近记录: |