【发布时间】:2021-10-20 16:06:30
【问题描述】:
我有这个 CNN:
def cnn(trainImages, trainLabels, testImages, testLabels):
trainImages = np.array(trainImages)
trainLabels = np.array(trainLabels)
testImages = np.array(testImages)
testLabels = np.array(testLabels)
trainImages = trainImages / 255
testImages = testImages / 255
model = Sequential()
model.add(Conv2D(filters = 32, kernel_size = (3, 3), padding = 'same', activation = 'relu', input_shape = (224, 224, 3)))
model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))
model.add(Conv2D(filters = 64, kernel_size = (3, 3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))
model.add(Conv2D(filters = 128, kernel_size = (3, 3), padding = 'same', activation = 'relu'))
model.add(MaxPool2D(pool_size = (2, 2), strides = (2, 2)))
model.add(Flatten())
model.add(Dense(256, activation = 'relu'))
model.add(Dense(9))
opt = Adam(learning_rate = 0.001)
model.compile(optimizer = opt, loss = tensorflow.keras.losses.SparseCategoricalCrossentropy(from_logits = True), metrics = ['accuracy'])
model.fit(trainImages, trainLabels, epochs = 20, batch_size = 64)
predictionResult = model.predict(testImages)
pred = []
for i in range(len(predictionResult)):
pred.append(np.argmax(predictionResult[i], axis = -1))
vehicles = ['Black Vehicles', 'Blue Vehicles', 'Brown Vehicles', 'Green Vehicles', 'Pink Vehicles', 'Red Vehicles', 'Silver Vehicles', 'White Vehicles', 'Yellow Vehicles']
print('Accuracy: ', metrics.accuracy_score(testLabels, pred))
print(metrics.classification_report(testLabels, pred, target_names = vehicles))
print(metrics.confusion_matrix(testLabels, pred))
在本地我得到了 93%,但在 Google Colab 中只有 10%。发生什么了?我看到在 Google Colab 上,第一次训练 epoch 的准确率很差,只有 10-15%,然后在一个 epoch 突然增加到 35%,第二个 60%,第三个超过 90%。
【问题讨论】:
-
那么,基本上最后两者都有相似的准确性?如果是这样,那有什么问题?
-
测试数据集的准确性很差,在 google colab 上大约为 10%。训练时期的准确性只是一个观察结果。
-
一开始训练的准确率很低,可能是因为随机权重初始化。至于模型,在拟合模型时尝试使用“validation_data”。并查看它在本地和 colab 上的表现。
-
考虑到从数据拆分到超参数的所有内容都保持不变,那么唯一可能的差异可能来自@user3503711 所指出的
random weight initialization,但在测试集上的最终准确度仅为 10% colab 和 90% on local 确实很奇怪,因为差异通常很小。 -
我在本地打印了所有预测,它们很好,它不是错误。我尝试了一个 svm 和它同样的问题,本地 86%,google colab 大约 13%。
标签: python tensorflow deep-learning conv-neural-network google-colaboratory