Python中使用Cifar-10数据集的模型预测

问题描述 投票:0回答:1

我为Cifar-10数据集编写了一个简短的CNN模型,以学习汽车的模式。现在,在编译模型并让其运行87个纪元后,我想计算AUC值。在另一个项目中,以下几行有效:

# Needed libraries
from sklearn.metrics import roc_curve
from sklearn.metrics import roc_auc_score
import matplotlib.pyplot as plt

print("Running on test sample. This may take a moment.")
probs = model.predict(df_test[cols]) #predict probability over test sample
AUC = roc_auc_score(df_test["is_signal_new"], probs[:,1])
print(df_test[cols].shape)

fpr, tpr, _ = roc_curve(df_test["is_signal_new"], df_test["signalprob"]) #extract true positive    
rate and false positive rate

但是,在这种情况下,当我写时

model.predict(X_test)

因为Python抱怨了Cifar10数据集的输入形状,所以它不起作用:

ValueError: Error when checking input: expected dense_22_input to have 2 dimensions, but got array with shape (10000, 32, 32, 3)

我了解为什么会出现错误消息,但是我不确定如何解决此错误。你会怎么做?

祝一切顺利。

python keras predict
1个回答
0
投票

我怀疑您在数据上训练了CNN模型,因为您没有展平CIFAR-10数据集。由于没有将数据拼合为矢量表示形式而导致的错误。

如果使用keras,则执行model.add(Flatten())

您可以简单地做:

train_flatten = train_x.reshape(train_x.shape[0], -1).T   # train_x is your training data
test_flatten = test_x.reshape(test_x.shape[0], -1).T # test_x is your test data

拼合数据的维数应为(3072,m),其中m为否。火车/测试示例。为您的测试数据提供了10000个示例。然后扁平化的向量维将为(3072,10000)

因此,总而言之,您的数据不会被拼合。这就是为什么出现错误的原因,因为它期望的是展平的数据(二维矢量),而不是原始的numpy数组。

© www.soinside.com 2019 - 2024. All rights reserved.