Keras学习笔记:序列式模型
目錄:
- 目錄:
- 快速開始序列(Sequential)模型
- 指定輸入數(shù)據(jù)的shape
- 編譯
- 訓(xùn)練
- 例子
- 用于序列分類的棧式LSTM
- 采用stateful LSTM的相同模型
本系列參考官方文檔官方文檔
這就是keras可以參考前篇:這就是keras
學(xué)習(xí)筆記 Keras:一些基本概念 一些基本概念
Keras:常見問題學(xué)習(xí)筆記:Keras常見問題
Windows 下Keras安裝和配置指南:Windows 下Keras安裝和配置指南
快速開始序列(Sequential)模型
序列模型是多個網(wǎng)絡(luò)層的線性堆疊,也就是“一條路走到黑”。
可以通過向Sequential模型傳遞一個layer的list來構(gòu)造該模型:
from keras.models import Sequential from keras.layers import Dense, Activationmodel = Sequential([ Dense(12, units=784), Activation('relu'), Dense(10), Activation('softmax'), ])也可以通過.add()方法一個個的將layer加入模型中:
model = Sequential() model.add(Dense(12 , input_shape=(784,))) model.add(Activation('relu'))指定輸入數(shù)據(jù)的shape
模型需要知道輸入數(shù)據(jù)的shape,因此,Sequential的第一層需要接受一個關(guān)于輸入數(shù)據(jù)shape的參數(shù),后面的各個層則可以自動的推導(dǎo)出中間數(shù)據(jù)的shape,因此不需要為每個層都指定這個參數(shù)。有幾種方法來為第一層指定輸入數(shù)據(jù)的shape
傳遞一個input_shape的關(guān)鍵字參數(shù)給第一層,input_shape是一個tuple類型的數(shù)據(jù),其中也可以填入None,如果填入None則表示此位置可能是任何正整數(shù)。數(shù)據(jù)的batch大小不應(yīng)包含在其中。
有些2D層,如Dense,支持通過指定其輸入維度input_dim來隱含的指定輸入數(shù)據(jù)shape,是一個Int類型的數(shù)據(jù)。一些3D的時域?qū)又С滞ㄟ^參數(shù)input_dim和input_length來指定輸入shape。
如果你需要為輸入指定一個固定大小的batch_size(常用于stateful RNN網(wǎng)絡(luò)),可以傳遞batch_size參數(shù)到一個層中,例如你想指定輸入張量的batch大小是32,數(shù)據(jù)shape是(6,8),則你需要傳遞batch_size=32和input_shape=(6,8)。
model = Sequential() model.add(Dense(32, input_dim=784))model = Sequential() model.add(Dense(32, input_shape=(784,)))編譯
在訓(xùn)練模型之前,我們需要通過compile來對學(xué)習(xí)過程進行配置。compile接收三個參數(shù):
優(yōu)化器optimizer:該參數(shù)可指定為已預(yù)定義的優(yōu)化器名,如rmsprop、adagrad,或一個Optimizer類的對象,詳情見optimizers
損失函數(shù)loss:該參數(shù)為模型試圖最小化的目標函數(shù),它可為預(yù)定義的損失函數(shù)名,如categorical_crossentropy、mse,也可以為一個損失函數(shù)。詳情見losses
指標列表metrics:對分類問題,我們一般將該列表設(shè)置為metrics=[‘a(chǎn)ccuracy’]。指標可以是一個預(yù)定義指標的名字,也可以是一個用戶定制的函數(shù).指標函數(shù)應(yīng)該返回單個張量,或一個完成metric_name - > metric_value映射的字典.請參考性能評估
# For a multi-class classification problem model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])# For a binary classification problem model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])# For a mean squared error regression problem model.compile(optimizer='rmsprop',loss='mse')# For custom metrics import keras.backend as Kdef mean_pred(y_true, y_pred):return K.mean(y_pred)model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy', mean_pred])訓(xùn)練
Keras以Numpy數(shù)組作為輸入數(shù)據(jù)和標簽的數(shù)據(jù)類型。訓(xùn)練模型一般使用fit函數(shù),該函數(shù)的詳情見這里。下面是一些例子。
# For a single-input model with 2 classes (binary classification):model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(1, activation='sigmoid')) model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['accuracy'])# Generate dummy data import numpy as np data = np.random.random((1000, 100)) labels = np.random.randint(2, size=(1000, 1))# Train the model, iterating on the data in batches of 32 samples model.fit(data, labels, epochs=10, batch_size=32)# For a single-input model with 10 classes (categorical classification):model = Sequential() model.add(Dense(32, activation='relu', input_dim=100)) model.add(Dense(10, activation='softmax')) model.compile(optimizer='rmsprop',loss='categorical_crossentropy',metrics=['accuracy'])# Generate dummy data import numpy as np data = np.random.random((1000, 100)) labels = np.random.randint(10, size=(1000, 1))# Convert labels to categorical one-hot encoding one_hot_labels = keras.utils.to_categorical(labels, num_classes=10)# Train the model, iterating on the data in batches of 32 samples model.fit(data, one_hot_labels, epochs=10, batch_size=32)例子
這里是一些幫助你開始的例子
在Keras代碼包的examples文件夾中,你將找到使用真實數(shù)據(jù)的示例模型:
CIFAR10 小圖片分類:使用CNN和實時數(shù)據(jù)提升 IMDB 電影評論觀點分類:使用LSTM處理成序列的詞語 Reuters(路透社)新聞主題分類:使用多層感知器(MLP) MNIST手寫數(shù)字識別:使用多層感知器和CNN 字符級文本生成:使用LSTM ...基于多層感知器的softmax多分類:
from keras.models import Sequential from keras.layers import Dense, Dropout, Activation from keras.optimizers import SGD# Generate dummy data import numpy as np x_train = np.random.random((1000, 20)) y_train = keras.utils.to_categorical(np.random.randint(10, size=(1000, 1)), num_classes=10) x_test = np.random.random((100, 20)) y_test = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10)model = Sequential() # Dense(64) is a fully-connected layer with 64 hidden units. # in the first layer, you must specify the expected input data shape: # here, 20-dimensional vectors. model.add(Dense(64, activation='relu', input_dim=20)) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy',optimizer=sgd,metrics=['accuracy'])model.fit(x_train, y_train,epochs=20,batch_size=128) score = model.evaluate(x_test, y_test, batch_size=128)MLP的二分類:
import numpy as np from keras.models import Sequential from keras.layers import Dense, Dropout# Generate dummy data x_train = np.random.random((1000, 20)) y_train = np.random.randint(2, size=(1000, 1)) x_test = np.random.random((100, 20)) y_test = np.random.randint(2, size=(100, 1))model = Sequential() model.add(Dense(64, input_dim=20, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(64, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy']) model.fit(x_train, y_train,epochs=20,batch_size=128) score = model.evaluate(x_test, y_test, batch_size=128)類似VGG的卷積神經(jīng)網(wǎng)絡(luò):
import numpy as np import keras from keras.models import Sequential from keras.layers import Dense, Dropout, Flatten from keras.layers import Conv2D, MaxPooling2D from keras.optimizers import SGD# Generate dummy data x_train = np.random.random((100, 100, 100, 3)) y_train = keras.utils.to_categorical(np.random.randint(10, size=(100, 1)), num_classes=10) x_test = np.random.random((20, 100, 100, 3)) y_test = keras.utils.to_categorical(np.random.randint(10, size=(20, 1)), num_classes=10)model = Sequential() # input: 100x100 images with 3 channels -> (100, 100, 3) tensors. # this applies 32 convolution filters of size 3x3 each. model.add(Conv2D(32, (3, 3), activation='relu', input_shape=(100, 100, 3))) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(MaxPooling2D(pool_size=(2, 2))) model.add(Dropout(0.25))model.add(Conv2D(64, (3, 3), activation='relu')) 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(256, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(10, activation='softmax'))sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True) model.compile(loss='categorical_crossentropy', optimizer=sgd)model.fit(x_train, y_train, batch_size=32, epochs=10) score = model.evaluate(x_test, y_test, batch_size=32)使用LSTM的序列分類
from keras.models import Sequential from keras.layers import Dense, Dropout from keras.layers import Embedding from keras.layers import LSTMmodel = Sequential() model.add(Embedding(max_features, output_dim=256)) model.add(LSTM(128)) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])model.fit(x_train, y_train, batch_size=16, epochs=10) score = model.evaluate(x_test, y_test, batch_size=16)使用1D卷積的序列分類
from keras.models import Sequential from keras.layers import Dense, Dropout from keras.layers import Embedding from keras.layers import Conv1D, GlobalAveragePooling1D, MaxPooling1Dmodel = Sequential() model.add(Conv1D(64, 3, activation='relu', input_shape=(seq_length, 100))) model.add(Conv1D(64, 3, activation='relu')) model.add(MaxPooling1D(3)) model.add(Conv1D(128, 3, activation='relu')) model.add(Conv1D(128, 3, activation='relu')) model.add(GlobalAveragePooling1D()) model.add(Dropout(0.5)) model.add(Dense(1, activation='sigmoid'))model.compile(loss='binary_crossentropy',optimizer='rmsprop',metrics=['accuracy'])model.fit(x_train, y_train, batch_size=16, epochs=10) score = model.evaluate(x_test, y_test, batch_size=16)用于序列分類的棧式LSTM
在該模型中,我們將三個LSTM堆疊在一起,使該模型能夠?qū)W習(xí)更高層次的時域特征表示。
開始的兩層LSTM返回其全部輸出序列,而第三層LSTM只返回其輸出序列的最后一步結(jié)果,從而其時域維度降維(即將輸入序列轉(zhuǎn)換為單個向量)
regular_stacked_lstmfrom keras.models import Sequential from keras.layers import LSTM, Dense import numpy as npdata_dim = 16 timesteps = 8 num_classes = 10# expected input data shape: (batch_size, timesteps, data_dim) model = Sequential() model.add(LSTM(32, return_sequences=True,input_shape=(timesteps, data_dim))) # returns a sequence of vectors of dimension 32 model.add(LSTM(32, return_sequences=True)) # returns a sequence of vectors of dimension 32 model.add(LSTM(32)) # return a single vector of dimension 32 model.add(Dense(10, activation='softmax'))model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])# Generate dummy training data x_train = np.random.random((1000, timesteps, data_dim)) y_train = np.random.random((1000, num_classes))# Generate dummy validation data x_val = np.random.random((100, timesteps, data_dim)) y_val = np.random.random((100, num_classes))model.fit(x_train, y_train,batch_size=64, epochs=5,validation_data=(x_val, y_val))采用stateful LSTM的相同模型
stateful LSTM的特點是,在處理過一個batch的訓(xùn)練數(shù)據(jù)后,其內(nèi)部狀態(tài)(記憶)會被作為下一個batch的訓(xùn)練數(shù)據(jù)的初始狀態(tài)。狀態(tài)LSTM使得我們可以在合理的計算復(fù)雜度內(nèi)處理較長序列
請FAQ中關(guān)于stateful LSTM的部分獲取更多信息
from keras.models import Sequential from keras.layers import LSTM, Dense import numpy as npdata_dim = 16 timesteps = 8 num_classes = 10 batch_size = 32# Expected input batch shape: (batch_size, timesteps, data_dim) # Note that we have to provide the full batch_input_shape since the network is stateful. # the sample of index i in batch k is the follow-up for the sample i in batch k-1. model = Sequential() model.add(LSTM(32, return_sequences=True, stateful=True,batch_input_shape=(batch_size, timesteps, data_dim))) model.add(LSTM(32, return_sequences=True, stateful=True)) model.add(LSTM(32, stateful=True)) model.add(Dense(10, activation='softmax'))model.compile(loss='categorical_crossentropy',optimizer='rmsprop',metrics=['accuracy'])# Generate dummy training data x_train = np.random.random((batch_size * 10, timesteps, data_dim)) y_train = np.random.random((batch_size * 10, num_classes))# Generate dummy validation data x_val = np.random.random((batch_size * 3, timesteps, data_dim)) y_val = np.random.random((batch_size * 3, num_classes))model.fit(x_train, y_train,batch_size=batch_size, epochs=5, shuffle=False,validation_data=(x_val, y_val))總結(jié)
以上是生活随笔為你收集整理的Keras学习笔记:序列式模型的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html遮罩层模态提示,页面遮罩层,并且
- 下一篇: 单核工作法15:循序渐进