1. 程式人生 > >keras系列(二):模型設定

keras系列(二):模型設定

Keras模型簡介

Keras的初始構建塊是一個模型,最簡單的模型稱為序列。Keras序列模型是一個神經網路層的線性管道(一個堆疊)。

from keras.models import Sequential
model = Sequential()
model.add(Dense(12, input_dim=8, kernel_initializer='random_uniform',bias_initializer='zeros')))

上述程式碼定義了一個12個人工神經元單層網路,需要8個輸入變數(特徵值),每個神經元用具體的權重初始化。

構成模組的第二種方式是通過functional API,在那裡可以定義複雜的模型,例如有向無環圖、具有共享層的模型或多輸出模型。

常規的全連線層

keras.layers.core.Dense(units, activation=None, use_bias=True, kernel_initializer='glorot_uniform')

預先定義的RNN

keras.layers.recurrent.Recurrent(return_sequences=False, go_backwards=False, stateful=
keras.layers.recurrent.SimpleRNN(units, activation='tanh', use_bias=True, kernel_initializer=
keras.layers.recurrent.GRU(units, activation='tanh', recurrent_activation='hard_sigmoid'
keras.layers.recurrent.LSTM(units, activation='tanh', recurrent_activation='hard_sigmoid'

卷積和池化層

ConvNets是一種使用卷積和池化操作的神經網路,它基於抽象的漸進層次逐步學習相當複雜的模型。這種漸進抽象的學習類似於在人類大腦中進化了數百萬年的視覺模型。幾年前,人們把它叫做深度3-5層,現在已經上升到100-200。

keras.layers.convolutional.Conv1D(filters, kernel_size, strides=1, padding='valid', dilation_rate=
keras.layers.convolutional.Conv2D(filters, kernel_size, strides=(1, 1), padding='valid'
keras.layers.pooling.MaxPooling1D(pool_size=2, strides=None, padding='valid')
keras.layers.pooling.MaxPooling2D(pool_size=(2, 2), strides=None, padding='valid', data_format=

Keras的基本操作

根據以上的方法,設定模型引數:

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
np.random.seed(1671) # for reproducibility
# network and training
NB_EPOCH = 200
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 # number of outputs = number of digits
OPTIMIZER = SGD() # SGD optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
# data: shuffled and split between train and test sets
# (
X_train, y_train), (X_test, y_test) =
mnist.load_data()
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
# X_train =
X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)

通常,設定完以上引數時,模型可以設定編譯了,如下:

# 10 outputs
# final stage is softmax
model = Sequential()
model.add(Dense(NB_CLASSES, input_shape=(RESHAPED,)))
model.add(Activation('softmax'))
model.summary() # output the model constuct including the total params
model.compile(loss='categorical_crossentropy', optimizer=OPTIMIZER, metrics=['accuracy'])

一旦模型被編譯完成,接著就可以用 fit 函式訓練了,其中需要設定引數如下:

epochs

這是模型用於訓練的次數。在每次迭代中,優化器嘗試調整權重,使目標函式最小化。

batch_size

這是在優化器執行權重更新之前用到的訓練例項的數量。

設定完以上引數,則可以對模型進行 fit 操作了:

history = model.fit(X_train, Y_train,
batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT)

一旦訓練了模型,我們就可以在測試集上進行評估。通過這種方法,我們可以得到目標函式所達到的最小值,以及評價指標達到的最佳值。

score = model.evaluate(X_test, Y_test, verbose=VERBOSE)
print("Test score:", score[0]) #loss
print('Test accuracy:', score[1]) #accuracy

基於以上層,再加入隱藏層和Drop out,程式碼如下:

import numpy as np
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers.core import Dense, Activation
from keras.optimizers import SGD
from keras.utils import np_utils
np.random.seed(1671) # for reproducibility
# network and training
NB_EPOCH = 20
BATCH_SIZE = 128
VERBOSE = 1
NB_CLASSES = 10 # number of outputs = number of digits
OPTIMIZER = SGD() # optimizer, explained later in this chapter
N_HIDDEN = 128
VALIDATION_SPLIT=0.2 # how much TRAIN is reserved for VALIDATION
# data: shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
#X_train is 60000 rows of 28x28 values --> reshaped in 60000 x 784
RESHAPED = 784
# X_train =
X_train.reshape(60000, RESHAPED)
X_test = X_test.reshape(10000, RESHAPED)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# normalize
X_train /= 255
X_test /= 255
print(X_train.shape[0], 'train samples')
print(X_test.shape[0], 'test samples')
# convert class vectors to binary class matrices

Y_train = np_utils.to_categorical(y_train, NB_CLASSES)
Y_test = np_utils.to_categorical(y_test, NB_CLASSES)
# M_HIDDEN hidden layers
# 10 outputs
# final stage is softmax

model = Sequential()
model.add(Dense(N_HIDDEN, input_shape=(RESHAPED,)))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(N_HIDDEN))
model.add(Activation('relu'))
model.add(Dropout(DROPOUT))
model.add(Dense(NB_CLASSES))
model.add(Activation('softmax'))

model.summary()
model.compile(loss='categorical_crossentropy',
optimizer=OPTIMIZER,
metrics=['accuracy'])
history = model.fit(X_train, Y_train,
batch_size=BATCH_SIZE, epochs=NB_EPOCH,
verbose=VERBOSE, validation_split=VALIDATION_SPLIT)
score = model.evaluate(X_test, Y_test, verbose=VERBOSE)
print("Test score:", score[0])
print('Test accuracy:', score[1])

預測輸出

當一個網路被訓練時,它可以被用來預測。在Keras這很簡單,我們可以使用以下方法:

# calculate predictions
predictions = model.predict(X)

對於給定的輸入,可以計算幾種型別的輸出,包括方法:

model.evaluate(): This is used to compute the loss values
model.predict_classes(): This is used to compute category outputs
model.predict_proba(): This is used to compute class probabilities