绩效评估模型不起作用



我试图通过调用模型的evaluate方法来评估模型在测试集上的性能。

import tensorflow as tf
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline 
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.preprocessing import image

导入libs之后我上传了数据集

mnist_data = tf.keras.datasets.mnist
(train_images, train_labels), (test_images, test_labels) = mnist_data.load_data()
def scale_mnist_data(train_images, test_images):
return (train_images/255, test_images/255)
scaled_train_images, scaled_test_images = scale_mnist_data(train_images, test_images)
scaled_train_images = scaled_train_images[..., np.newaxis]
scaled_test_images = scaled_test_images[..., np.newaxis]
def get_model(input_shape):

model = Sequential([
Conv2D(filters=8, kernel_size=3, padding='same', activation= 
relu', input_shape=input_shape),
MaxPooling2D((2,2)),
Flatten(),
Dense(64, activation='relu'),
Dense(64, activation='relu'),
Dense(10, activation='softmax')
])
return model
model = get_model(scaled_train_images[0].shape)
model.summary()
def compile_model(model):
opt = tf.keras.optimizers.Adam(learning_rate = 0.005)
acc = tf.keras.metrics.SparseCategoricalAccuracy()
mae = tf.keras.metrics.MeanAbsoluteError()

model.compile(
optimizer = opt,
loss = 'sparse_categorical_crossentropy', 
metrics = ['acc', 'mae'] 
)
compile_model(model)
def train_model(model, train_images, train_labels):

history = model.fit(scaled_train_images, train_labels, epochs=5, 
batch_size=256)
return history
history = train_model(model, scaled_train_images, train_labels)
frame = pd.DataFrame(history.history)
acc_plot = frame.plot(y="acc", title="Accuracy vs Epochs", legend=False)
acc_plot.set(xlabel="Epochs", ylabel="acc")
acc_plot = frame.plot(y="loss", title = "Loss vs Epochs",legend=False)
acc_plot.set(xlabel="Epochs", ylabel="Loss")
def evaluate_model(model, scaled_test_images, test_labels):

(test_loss, test_accuracy) = model.evaluate(scaled_test_images, 
test_labels)
return (test_loss, test_accuracy)

现在,问题真的来自这一部分,我不知道为什么?

test_loss, test_accuracy = evaluate_model(model, scaled_test_images, test_labels)
print(f"Test loss: {test_loss}")
print(f"Test accuracy: {test_accuracy}")

我收到这个错误:

10000/1 [=================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================================5s 499us/sample - loss: 0.0256 - acc: 0.9840 - mae: 4.3630
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-16-0cceed9839ec> in <module>
1 # Run your function to evaluate the model
2 
----> 3 test_loss, test_accuracy = evaluate_model(model, scaled_test_images, test_labels)
4 
5 print(f"Test loss: {test_loss}")
<ipython-input-15-2804922fd7ac> in evaluate_model(model, scaled_test_images, test_labels)
9     Your function should return a tuple (test_loss, test_accuracy).
10     """
---> 11     (test_loss, test_accuracy) = model.evaluate(scaled_test_images, test_labels)
12     return (test_loss, test_accuracy)
ValueError: too many values to unpack (expected 2)

并且模型预测中的保护是正确的

我想之后运行的模型pred是:

num_test_images = scaled_test_images.shape[0]
random_inx = np.random.choice(num_test_images, 4)
random_test_images = scaled_test_images[random_inx, ...]
random_test_labels = test_labels[random_inx, ...]
predictions = model.predict(random_test_images)
fig, axes = plt.subplots(4, 2, figsize=(16, 12))
fig.subplots_adjust(hspace=0.4, wspace=-0.2)
for i, (prediction, image, label) in enumerate(zip(predictions, random_test_images, random_test_labels)):
axes[i, 0].imshow(np.squeeze(image))
axes[i, 0].get_xaxis().set_visible(False)
axes[i, 0].get_yaxis().set_visible(False)
axes[i, 0].text(10., -1.5, f'Digit {label}')
axes[i, 1].bar(np.arange(len(prediction)), prediction)
axes[i, 1].set_xticks(np.arange(len(prediction)))
axes[i, 1].set_title(f"Categorical distribution. Model prediction: {np.argmax(prediction)}")

plt.show()

预测结果Pic

您必须删除其中一个度量accmae,原因是它与冲突

model.compile(
optimizer = opt,
loss = 'sparse_categorical_crossentropy', 
metrics = ['acc'] # this or metrics = ['mae']
)

使用evaluate函数将返回lossaccmae,具体取决于您设置的值。

#using accuracy
(test_loss, test_accuracy) = model.evaluate(scaled_test_images, test_labels)

#using mae
(test_loss, test_mae) = model.evaluate(scaled_test_images, test_labels)

在你的情况下,我建议使用"acc",这是准确的,因为这是一个分类问题,但这取决于你使用它的目的。

您也可以使用函数tf.keras.metrics.mean_absolute_error(x_valid, results)来计算mean absolute error

相关内容

  • 没有找到相关文章

最新更新