【发布时间】:2019-10-18 10:07:20
【问题描述】:
我正在学习神经网络,目前使用 Keras 库在 CFAR-10 数据集上实现对象分类。这是我对 Keras 定义的神经网络的定义:
# Define the model and train it
model = Sequential()
model.add(Dense(units = 60, input_dim = 1024, activation = 'relu'))
model.add(Dense(units = 50, activation = 'relu'))
model.add(Dense(units = 60, activation = 'relu'))
model.add(Dense(units = 70, activation = 'relu'))
model.add(Dense(units = 30, activation = 'relu'))
model.add(Dense(units = 10, activation = 'sigmoid'))
model.compile(loss='binary_crossentropy',
optimizer='adam',
metrics=['accuracy'])
model.fit(X_train, y_train, epochs=50, batch_size=10000)
所以我有 1 个输入层,其输入尺寸为 1024 或 (1024, )(每个 32 * 32 *3 的图像首先转换为灰度,从而产生 32 * 32 的尺寸)、5 个隐藏层和 1 个输出上面代码中定义的层。
当我训练我的模型超过 50 个 epoch 时,我得到了 0.9 或 90% 的准确率。此外,当我使用测试数据集对其进行评估时,我得到了大约的准确度。 90%。这是评估模型的代码行:
print (model.evaluate(X_test, y_test))
这将打印以下损失和准确性:
[1.611809492111206, 0.8999999761581421]
但是当我通过对每个测试数据图像进行预测来手动计算准确度时,我得到了大约 11% 的准确度(这与随机做出预测的概率几乎相同)。这是我手动计算的代码:
wrong = 0
for x, y in zip(X_test, y_test):
if not (np.argmax(model.predict(x.reshape(1, -1))) == np.argmax(y)):
wrong += 1
print (wrong)
这会打印出 10000 个错误预测中的 9002 个。那么我在这里缺少什么?为什么两种准确度正好相反(100 - 89 = 11%)?任何直观的解释都会有所帮助!谢谢。
编辑:
这是我处理数据集的代码:
# Process the training and testing data and make in Neural Network comfortable
# convert given colored image to grayscale
def rgb2gray(rgb):
return np.dot(rgb, [0.2989, 0.5870, 0.1140])
X_train, y_train, X_test, y_test = [], [], [], []
def process_batch(batch_path, is_test = False):
batch = unpickle(batch_path)
imgs = batch[b'data']
labels = batch[b'labels']
for img in imgs:
img = img.reshape(3,32,32).transpose([1, 2, 0])
img = rgb2gray(img)
img = img.reshape(1, -1)
if not is_test:
X_train.append(img)
else:
X_test.append(img)
for label in labels:
if not is_test:
y_train.append(label)
else:
y_test.append(label)
process_batch('cifar-10-batches-py/data_batch_1')
process_batch('cifar-10-batches-py/data_batch_2')
process_batch('cifar-10-batches-py/data_batch_3')
process_batch('cifar-10-batches-py/data_batch_4')
process_batch('cifar-10-batches-py/data_batch_5')
process_batch('cifar-10-batches-py/test_batch', True)
number_of_classes = 10
number_of_batches = 5
number_of_test_batch = 1
X_train = np.array(X_train).reshape(meta_data[b'num_cases_per_batch'] * number_of_batches, -1)
print ('Shape of training data: {0}'.format(X_train.shape))
# create labels to one hot format
y_train = np.array(y_train)
y_train = np.eye(number_of_classes)[y_train]
print ('Shape of training labels: {0}'.format(y_train.shape))
# Process testing data
X_test = np.array(X_test).reshape(meta_data[b'num_cases_per_batch'] * number_of_test_batch, -1)
print ('Shape of testing data: {0}'.format(X_test.shape))
# create labels to one hot format
y_test = np.array(y_test)
y_test = np.eye(number_of_classes)[y_test]
print ('Shape of testing labels: {0}'.format(y_test.shape))
【问题讨论】:
-
这是一个黄金问题,谢谢!
标签: python machine-learning keras neural-network artificial-intelligence