我被赋予了一个实现卷积神经网络的任务,该网络可以使用网络结构来评估MNIST dataset中发现的手写数字:

python - 如何使用其他CNN而不损失准确性-LMLPHP

我已经实现了与该体系结构相匹配的CNN,不幸的是,它的准确度仅为10%。我在网上查看并尝试了其他示例CNN,以确保是否有其他原因导致此问题,但是它们似乎工作正常,并给我提供了〜99%的准确性。我将两个CNN都放入了我的代码中,并进行了布尔切换以显示两者之间的区别:

import tensorflow
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.layers import Conv2D, MaxPooling2D

batch_size = 128
num_classes = 10
epochs = 1
img_rows, img_cols = 28, 28


(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
input_shape = (img_rows, img_cols, 1)

x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')

y_train = tensorflow.keras.utils.to_categorical(y_train, num_classes)
y_test = tensorflow.keras.utils.to_categorical(y_test, num_classes)

exampleModel = False  # Use to toggle which CNN goes into the model

if exampleModel:  # An example CNN that I found for MNIST
    model = Sequential()
    model.add(Conv2D(32, kernel_size=(3, 3),
                     activation='relu',
                     input_shape=input_shape))
    model.add(Conv2D(64, (3, 3), activation='relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Dropout(0.25))
    model.add(Flatten())
    model.add(Dense(128, activation='relu'))
    model.add(Dropout(0.5))
    model.add(Dense(num_classes, activation='softmax'))
else:  # The CNN I created
    input_layer = tensorflow.keras.layers.Input(shape=input_shape)
    conv1 = Conv2D(32, (1, 1), activation='relu')(input_layer)
    pool1 = MaxPooling2D(2, 2)(conv1)
    conv2_1 = Conv2D(64, (1, 1), activation='relu', padding='same')(pool1)
    pool2_1 = MaxPooling2D(2, 2)(conv2_1)
    drop2_1 = Dropout(0.5)(pool2_1)
    conv2_2 = Conv2D(64, (1, 1), activation='relu', padding='same')(pool1)
    pool2_2 = MaxPooling2D(2, 2)(conv2_2)
    drop2_2 = Dropout(0.5)(pool2_2)
    conv3_1 = Conv2D(256, (1, 1), activation='relu', padding='same')(drop2_1)
    conv3_2 = Conv2D(256, (1, 1), activation='relu', padding='same')(drop2_2)
    merged = tensorflow.keras.layers.concatenate([conv3_1, conv3_2], axis=-1)
    merged = Dropout(0.5)(merged)
    merged = Flatten()(merged)
    fc1 = Dense(1000, activation='relu')(merged)
    fc2 = Dense(500, activation='relu')(fc1)
    out = Dense(10)(fc2)
    model = tensorflow.keras.models.Model(input_layer, out)

model.compile(loss=tensorflow.keras.losses.categorical_crossentropy,
              optimizer=tensorflow.keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.fit(x_train, y_train,
          batch_size=batch_size,
          epochs=epochs,
          verbose=1,
          validation_data=(x_test, y_test))
score = model.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])


为了完成任务,我相信我必须将示例CNN逐段转换为所需的体系结构。尽管我不知道该怎么做,但是它们看起来彼此完全不同(一个纯粹是顺序的,另一个使用并行层并合并)。我是机器学习的初学者,因此可能找不到某些东西,尽管我找不到在线转换过程中的资源。任何与此有关的帮助表示赞赏。

最佳答案

您只需要将softmax激活添加到最后一个out层:

out = Dense(10, activation="softmax")(fc2)


因此,您的模型具有完整的形式:

input_layer = tensorflow.keras.layers.Input(shape=input_shape)
conv1 = Conv2D(32, (1, 1), activation='relu')(input_layer)
pool1 = MaxPooling2D(2, 2)(conv1)
conv2_1 = Conv2D(64, (1, 1), activation='relu', padding='same')(pool1)
pool2_1 = MaxPooling2D(2, 2)(conv2_1)
drop2_1 = Dropout(0.5)(pool2_1)
conv2_2 = Conv2D(64, (1, 1), activation='relu', padding='same')(pool1)
pool2_2 = MaxPooling2D(2, 2)(conv2_2)
drop2_2 = Dropout(0.5)(pool2_2)
conv3_1 = Conv2D(256, (1, 1), activation='relu', padding='same')(drop2_1)
conv3_2 = Conv2D(256, (1, 1), activation='relu', padding='same')(drop2_2)
merged = tensorflow.keras.layers.concatenate([conv3_1, conv3_2], axis=-1)
merged = Dropout(0.5)(merged)
merged = Flatten()(merged)
fc1 = Dense(1000, activation='relu')(merged)
fc2 = Dense(500, activation='relu')(fc1)
out = Dense(10, activation="softmax")(fc2)


出:

x_train shape: (60000, 28, 28, 1)
60000 train samples
10000 test samples
Train on 60000 samples, validate on 10000 samples
Epoch 1/1
60000/60000 [==============================] - 25s 416us/step - loss: 0.6394 - acc: 0.7858 - val_loss: 0.2956 - val_acc: 0.9047
Test loss: 0.29562548571825026
Test accuracy: 0.9047

关于python - 如何使用其他CNN而不损失准确性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/53254870/

10-12 23:51