如何向这个问题添加卷积层?

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

我是Convents的新手,我设计了一个三层网络,其中包含2个隐藏层以对MNIST数据集进行分类。使用L2正则化,辍学和提早停止。这段代码是我到目前为止编写的。

我想知道如何在网络中添加2层卷积神经网络?我了解尽早停止可能会有些困难。我只想绘制训练损失和验证错误,以与没有convnet的代码进行比较(我已经写的代码发布在这里)

from __future__ import print_function
# tensorflow
import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop,SGD,adam
from keras import regularizers

import numpy as np
import matplotlib.pyplot as plt
# %matplotlib inline

"""Let's explore the dataset a little bit"""

# Load the data, shuffled and split between train and test sets
(x_train, y_train), (x_test, y_test) = mnist.load_data()

x_train[0].shape

#Let's just look at a particular example to see what is inside

x_train[333]  ## Just a 28 x 28 numpy array of ints from 0 to 255

# What is the corresponding label in the training set?
y_train[333]

# Let's see what this image actually looks like

plt.imshow(x_train[333], cmap='Greys_r')

# this is the shape of the np.array x_train
# it is 3 dimensional.
print(x_train.shape, 'train samples')
print(x_test.shape, 'test samples')

## For our purposes, these images are just a vector of 784 inputs, so let's convert
x_train = x_train.reshape(len(x_train), 28*28)
x_test = x_test.reshape(len(x_test), 28*28)

## Keras works with floats, so we must cast the numbers to floats
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

## Normalize the inputs so they are between 0 and 1
x_train /= 255
x_test /= 255

# convert class vectors to binary class matrices (one-hot encoding)
num_classes = 10
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

y_train[333]  # now the digit k is represented by a 1 in the kth entry (0-indexed) of the length 10 vector

### Build your model here
model_3 = Sequential()
model_3.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.01), input_shape=(784,)))
model_3.add(Dropout(0.2))
model_3.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model_3.add(Dropout(0.2))
model_3.add(Dense(64, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model_3.add(Dropout(0.2))
model_3.add(Dense(10, activation='softmax'))

learning_rate = .001
model_3.compile(loss='categorical_crossentropy',
              optimizer='adam',
              metrics=['accuracy'])

model_3.summary()

# And now let's fit.

batch_size = 128  # mini-batch with 32 examples
epochs = 250
history = model_3.fit(
    x_train, y_train,
    batch_size=batch_size,
    epochs=epochs,
    verbose=1,
    validation_data=(x_test, y_test))

score = model_3.evaluate(x_test, y_test, verbose=0)
print('Test loss:', score[0])
print('Test accuracy:', score[1])



def plot_loss_accuracy(history):
    fig = plt.figure(figsize=(12, 6))
    ax = fig.add_subplot(1, 2, 1)
    ax.plot(history.history["loss"],'r-x', label="Train Loss")
    ax.plot(history.history["val_loss"],'b-x', label="Validation Loss")
    ax.legend()
    ax.set_title('cross_entropy loss')
    ax.grid(True)


    ax = fig.add_subplot(1, 2, 2)
    ax.plot(history.history["accuracy"],'r-x', label="Train Accuracy")
    ax.plot(history.history["val_accuracy"],'b-x', label="Validation Accuracy")
    ax.legend()
    ax.set_title('accuracy')
    ax.grid(True)


plot_loss_accuracy(history)
plt.show()
python deep-learning neural-network conv-neural-network mnist
1个回答
0
投票

您想在哪里添加转换层?Conv2D获取(N,H,W,C)作为输入和输出,其中N是批次大小,H是高度,W是宽度,C是通道大小。

您的网络具有所有(N,C)张量。因此,您需要将矢量重塑为图像,或将图像展平为矢量。

这是获取(NHWC)张量的示例,因此您不需要x_train.reshape来展平。 VGG,ResNet等许多著名的网络首先具有Conv层,最后具有Dense层。它旨在从转换层中获取要素,并通过密集层预测类别。

import keras
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, Flatten
from keras import regularizers
import numpy as np


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

# MNIST shape has lacks of channels, so add it. MNIST is gray scale so channel size is 1.
# (60000, 28, 28) -> (60000, 28, 28, 1)
# If you have color image, it shoud be 3 channels, RGB: [BATCH_SIZE, H, W, 3]
x_train = np.expand_dims(x_train, axis=-1)
x_test = np.expand_dims(x_test, axis=-1)

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

model_3 = Sequential()
model_3.add(Conv2D(32, kernel_size=2, padding='same', activation='relu', input_shape=(28, 28, 1)))
model_3.add(Conv2D(64, kernel_size=2, padding='same', activation='relu'))
model_3.add(Flatten())
model_3.add(Dense(256, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model_3.add(Dropout(0.2))
model_3.add(Dense(128, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model_3.add(Dropout(0.2))
model_3.add(Dense(64, activation='relu',kernel_regularizer=regularizers.l2(0.01)))
model_3.add(Dropout(0.2))
model_3.add(Dense(10, activation='softmax'))

model_3.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adam(),
              metrics=['accuracy'])

history = model_3.fit(
    x_train, y_train,
    batch_size=128,
    epochs=250,
    verbose=1,
    validation_data=(x_test, y_test))

对于重塑,https://keras.io/layers/core/

还有Conv2D MNIST示例。https://github.com/keras-team/keras/blob/master/examples/mnist_cnn.py

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