重新編輯于20180301, 曾經寫過的內容有不嚴謹的地方,畢竟當時自己也是初學者, 括號內為新加的內容
今天我們來逐條學一下基于keras的mnist網絡的搭建,因為只是單純的復制和粘貼別人的代碼是永遠學不會DL的,當然還有Markdown。
# 這部分是聲明
from __future__ import print_function(這句可以不要)
import keras (這句也沒啥用)
from keras.datasets import mnist
from keras.models import Sequential(序貫模型,還有另一種更高級,更靈活的Model模型)
from keras.layers import Dense, Dropout, Flatten(一些基本的層,Dropout在這種簡單網絡中作用不明確,不明顯)
from keras.layers import Conv2D, MaxPooling2D
from keras import backend as K
# batch_size 太小會導致訓練慢,過擬合等問題,太大會導致欠擬合。所以要適當選擇
(batchsize太小可能會導致波動幅度過大,極端情況,=1的時候,等于隨機梯度下降,根據你的顯存大小而定,選擇合適的大小,實在顯存受限=1也是可以的)
batch_size = 128
# 0-9手寫數字一個有10個類別
num_classes = 10
# 12次完整迭代,差不多夠了
(一般還是搞50以上吧,取決于loss是否收斂了)
epochs = 12
# 輸入的圖片是28*28像素的灰度圖
img_rows, img_cols = 28, 28
# 訓練集,測試集收集非常方便
(x_train, y_train), (x_test, y_test) = mnist.load_data()
# keras輸入數據有兩種格式,一種是通道數放在前面,一種是通道數放在后面,
# 其實就是格式差別而已,圖像數量,顏色通道,行,列
(實際上就是使數據和網絡的輸入在維度上保持一致,這在其他的模型訓練中也是需要經常注意的)
if K.image_data_format() == 'channels_first':
x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
input_shape = (1, img_rows, img_cols) #這里再次提示了一次數據格式
else:
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(所有圖像,1灰度通道,行,列)
(數據的歸一化處理,減去均值除以范圍,最終是0-1的范圍,
所以最后的激活函數應該是sigmoid,如果是-1~1,那么激活函數應該是tanh)
# 把數據變成float32更精確
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
(想看看維度的話就顯示一下,或者監控顯示x_train.shape也可以)
print('x_train shape:', x_train.shape)
print(x_train.shape[0], 'train samples')
print(x_test.shape[0], 'test samples')
# 把類別0-9變成2進制,方便訓練(one-hot, 為最后的softmax輸出判斷類別做準備)
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
# Sequential類可以讓我們靈活地插入不同的神經網絡層
model = Sequential()
# 加上一個2D卷積層, 32個輸出(也就是卷積通道),激活函數選用relu,
# 卷積核的窗口選用3*3像素窗口
model.add(Conv2D(32,
activation='relu',
input_shape=input_shape, (注意首個網絡層需要指定shape)
nb_row=3,
nb_col=3))
# 自己改寫成了:
model.add(Conv2D(32,3,
activation='relu',input_shape=input_shape))
# 效果完全一致
- keras.layers.convolutional.Conv2D
(filters, kernel_size, strides=(1, 1), padding='valid', data_format=None, dilation_rate=(1, 1), activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
- 二維卷積層,即對圖像的空域卷積。該層對二維輸入進行滑動窗卷積,當使用該層作為第一層時,應提供input_shape參數。例如input_shape = (128,128,3)代表128*128的彩色RGB圖像(data_format='channels_last')
- Conv2D最重要的參數,
其一,filters即輸出的維度,也就是卷積核的數目,也就是將平面的圖像,拉伸成filters維的空間矩陣,
其二,strides,也就是步長,它和padding一起決定了卷積操作后的圖像大小,
其三,注意,第一層需要指定input_shape,例如,input_shape = (128,128,3),也就是原圖的大小以及顏色通道數量。
Conv2D
這里詳細說明Conv2D的用法(有點跑題了,mark下圖像卷積,以及維度變化的內涵):
filters:是輸出的維度,它不用考慮輸入維度,nice
kernel_size:是卷積核的大小,有什么作用?,
程序里直接沒提kernel_size的大小。strides:決定每次卷積核前進的數量,如果是1,則卷積后的大小與原先圖像大小一致,如果是2,則圖像的大小減半。
padding:
valid: new_height = new_width = (W – F + 1) / S #結果向上取整
same: new_height = new_width = W / S #結果向上取整
圖片.png
- activation:激活函數,為預定義的激活函數名(參考激活函數),或逐元素(element-wise)的Theano函數。如果不指定該參數,將不會使用任何激活函數(即使用線性激活函數:a(x)=x)
# 64個通道的卷積層
model.add(Conv2D(64, activation='relu',
nb_row=3,
nb_col=3))
# 池化層是2*2像素的
model.add(MaxPooling2D(pool_size=(2, 2)))
MaxPooling2D層
keras.layers.pooling.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=None)
-
參數
- pool_size:整數或長為2的整數tuple,代表在兩個方向(豎直,水平)上的下采樣因子,如取(2,2)將使圖片在兩個維度上均變為原長的一半。為整數意為各個維度值相同且為該數字。
- strides:整數或長為2的整數tuple,或者None,步長值。
- border_mode:‘valid’或者‘same’
參考源程序分析:
model.add(MaxPooling2D(pool_size=(2, 2)))
- 可以看到只有一個pool_size,strides默認應該是1,padding默認是valid,所以這里并不需要考慮輸出的大小,這點非常好用。
下面繼續疊加一層卷積層和一層maxpooling層
這里研究一下激活函數的類型
- softmax:對輸入數據的最后一維進行softmax,輸入數據應形如(nb_samples, nb_timesteps, nb_dims)或(nb_samples,nb_dims)
- elu
- selu: 可伸縮的指數線性單元(Scaled Exponential Linear Unit),參考Self-Normalizing Neural Networks
- softplus
- softsign
- relu
- tanh
- sigmoid
- hard_sigmoid
- linear
Dropout層
- Dropout, Dense,Flatten層都是keras的
# 對于池化層的輸出,采用0.35概率的Dropout
model.add(Dropout(0.35))
- keras.layers.core.Dropout(rate, noise_shape=None, seed=None)
為輸入數據施加Dropout。Dropout將在訓練過程中每次更新參數時按一定概率(rate)隨機斷開輸入神經元,Dropout層用于防止過擬合。 - rate:0~1的浮點數,控制需要斷開的神經元的比例
Flatten層
(把卷積核展開成向量,這里損失了像素的空間關系,參數數量急劇增加,為了克服這些,可以采用1*1卷積,請自行百度)
keras.layers.core.Flatten()
- Flatten層用來將輸入“壓平”,即把多維的輸入一維化,常用在從卷積層到全連接層的過渡。Flatten不影響batch的大小。
# 展平所有像素,比如[28*28] -> [784]
model.add(Flatten())
Dense層
# 對所有像素使用全連接層,輸出為128,激活函數選用relu
model.add(Dense(128, activation='relu'))
- 用法:
keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform', bias_initializer='zeros', kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)
# 對輸入采用0.5概率的Dropout
model.add(Dropout(0.5))
# 對剛才Dropout的輸出采用softmax激活函數,得到最后結果0-9
model.add(Dense(num_classes, activation='softmax'))
Model.Compile
# 模型我們使用交叉熵損失函數,最優化方法選用Adadelta
model.compile(loss=keras.metrics.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
- compile(self, optimizer, loss, metrics=None, sample_weight_mode=None)
編譯用來配置模型的學習過程,其參數有:
- optimizer:字符串(預定義優化器名)或優化器對象,參考優化器
- loss:字符串(預定義損失函數名)或目標函數,參考損失函數
- metrics:列表,包含評估模型在訓練和測試時的網絡性能的指標,典型用法是metrics=['accuracy']
Model.fit
# 令人興奮的訓練過程
model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs,
verbose=1, validation_data=(x_test, y_test))
fit(self, x, y, batch_size=32, epochs=10, verbose=1, callbacks=None, validation_split=0.0, validation_data=None, shuffle=True, class_weight=None, sample_weight=None, initial_epoch=0)
用法:
- x:輸入數據。如果模型只有一個輸入,那么x的類型是numpy array,如果模型有多個輸入,那么x的類型應當為list,list的元素是對應于各個輸入的numpy array
- y:標簽,numpy array
- batch_size:整數,指定進行梯度下降時每個batch包含的樣本數。訓練時一個batch的樣本會被計算一次梯度下降,使目標函數優化一步。
- epochs:整數,訓練的輪數,每個epoch會把訓練集輪一遍。
- validation_data:形式為(X,y)的tuple,是指定的驗證集。此參數將覆蓋validation_spilt。
這里的validation_data是可以不寫的
Model.evaluate
score = model.evaluate(x_test, y_test, verbose=1)
print('Test loss:', score[0])
print('Test accuracy:', score[1])
用法:
evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None)
感謝大家觀看,最后更新放出完整代碼了,喜歡的話請點個贊,歡迎轉載,轉載請復制出處.
# coding:utf-8
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Conv2D,MaxPool2D,Dense,Flatten, Activation
from keras.callbacks import TensorBoard
from keras.models import save_model,load_model
import keras,numpy
from keras.utils import plot_model
batch_size = 32
num_classes = 10
epoch = 20
img_rows, img_cols = 28, 28
input_shape = (img_rows,img_cols,1)
(x_train,y_train),(x_test,y_test) = mnist.load_data()
if keras.backend.image_data_format()=='channels_first':
x_train = numpy.reshape(x_train,[x_train.shape[0],1,img_rows,img_cols])
x_test = numpy.reshape(x_test, [x_test.shape[0], 1,img_rows, img_cols])
input_shape = (1,img_rows,img_cols)
else:
x_train = numpy.reshape(x_train,[x_train.shape[0],img_rows,img_cols,1])
x_test = numpy.reshape(x_test, [x_test.shape[0],img_rows, img_cols,1])
input_shape = (img_rows,img_cols,1)
# 數據預處理,歸一化,one-hot化
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
x_train = x_train/255
x_test = x_test/255
y_train = keras.utils.np_utils.to_categorical(y_train, num_classes)
y_test = keras.utils.np_utils.to_categorical(y_test, num_classes)
# 構造lenet模型,簡化版
mnist_model = Sequential()
mnist_model.add(Conv2D(6, (5, 5), activation='relu', input_shape=input_shape))
# mnist_model.add(Conv2D(6,1,5,5))
mnist_model.add(Activation('relu'))
mnist_model.add(MaxPool2D(pool_size=(2, 2)))
mnist_model.add(Conv2D(16, (5, 5), activation='relu'))
mnist_model.add(MaxPool2D(pool_size=(2, 2)))
mnist_model.add(Flatten())
mnist_model.add(Dense(120, activation='relu'))
mnist_model.add(Dense(84, activation='relu'))
mnist_model.add(Dense(num_classes, activation='softmax'))
# 保存模型(拓撲圖)
plot_model(mnist_model, to_file='lenet.png')
# 編譯
mnist_model.compile(optimizer='Adadelta',loss='categorical_crossentropy',
metrics=['accuracy'])
# 訓練
mnist_model.fit(x_train,y_train,batch_size = batch_size,epochs=epoch,verbose=1,
callbacks=[TensorBoard(log_dir='./log')])
# 保存模型
save_model(mnist_model,'lenet.h5')
# 測試
score = mnist_model.evaluate(x_test,y_test,verbose=1)
print('\n')
print('test loss:',score[0])
print('test accuracy:',score[1])