上一篇介紹了卷基層,可以用來構(gòu)建很常見的卷積神經(jīng)網(wǎng)絡(luò)等模型。那么今天將要介紹的是遞歸層,是一個(gè)可以用來構(gòu)建遞歸網(wǎng)絡(luò)(RNN)的基礎(chǔ)部件。具體的RNN知識(shí),可以參考文章:《深入探究遞歸神經(jīng)網(wǎng)絡(luò)》。如果感覺上面這篇文章比較抽象,那么強(qiáng)烈建議讀者閱讀一下《遞歸神經(jīng)網(wǎng)絡(luò)不可思議的有效性》,因?yàn)樗Y(jié)合實(shí)際講述了RNN的強(qiáng)大。下面來看下遞歸層都有哪些結(jié)構(gòu)。
一、SimpleRNN
keras.layers.recurrent.SimpleRNN(output_dim,
init='glorot_uniform', inner_init='orthogonal', activation='sigmoid', weights=None,
truncate_gradient=-1, return_sequences=False, input_dim=None, input_length=None)
一種輸出反饋給輸入的全連接RNN。
** inputshape: 3維 tensor(nb_samples, timesteps,input_dim)
** outputshape: 如果return_sequences=True,那么輸出3維 tensor(nb_samples, timesteps, output_dim) .否則輸出2維tensor(nb_samples,output_dim)。
** Masking:This layer supports masking forinput data with a variable number of timesteps To introduce masks to your data,use an Embedding layer with themask_zero parameter set toTrue.
** 參數(shù):
- output_dim : 內(nèi)部計(jì)算和最終輸出的維度。
- init : 初始化權(quán)值的函數(shù)名稱或Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置初始化權(quán)值函數(shù)見這里),也可以傳遞自己編寫的Theano function。如果不給weights傳遞參數(shù)時(shí),則該參數(shù)必須指明。
- activation : 激活函數(shù)名稱或者Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置激活函數(shù)見這里),也可以是傳遞自己編寫的Theano function。如果不明確指定,那么將沒有激活函數(shù)會(huì)被應(yīng)用。
- weights :用于初始化權(quán)值的numpy arrays組成的list。這個(gè)List應(yīng)該有3個(gè)元素,它們的shape是[(input_dim, output_dim), (output_dim, output_dim),(output_dim,)]
- truncate_gradient: 在BPTT(back propgation throughtime, BP算法加入了時(shí)間維度)算法中的truncate步數(shù)。
- return_sequence: Boolean.False返回在輸出序列中的最后一個(gè)輸出;True返回整個(gè)序列。
- input_dim:輸入數(shù)據(jù)的維度。當(dāng)把該層作為模型的第一層時(shí),這個(gè)參數(shù)和input_shape至少要提供一個(gè)傳值。
- input_length:輸入序列的長度。This argument is required ifyou are going to connectFlatten thenDense layers upstream (without it,the shape of the dense outputs cannot be computed)
二、SimpleDeepRNN
keras.layers.recurrent.SimpleDeepRNN(output_dim,depth=3,
init='glorot_uniform', inner_init='orthogonal',
activation='sigmoid', inner_activation='hard_sigmoid',
weights=None, truncate_gradient=-1, return_sequences=False,
input_dim=None, input_length=None)
一種經(jīng)過多步(由參數(shù)depth決定)計(jì)算輸出反饋給輸入的全連接RNN。示例代碼如下:
output= activation(W.x_t + b +inner_activation(U_1.h_tm1) +inner_activation(U_2.h_tm2) + ...)
** inputshape: 3維 tensor(nb_samples, timesteps,input_dim)
** outputshape: 如果return_sequences=True,那么輸出3維 tensor(nb_samples, timesteps, output_dim) .否則輸出2維tensor(nb_samples,output_dim)。
** Masking:This layer supports masking forinput data with a variable number of timesteps To introduce masks to your data,use an Embedding layer with themask_zero parameter set toTrue.
** 參數(shù):
- output_dim : 內(nèi)部計(jì)算和最終輸出的維度。
- depth : int>=1.循環(huán)迭代的次數(shù)。如果depth=1,那么就等價(jià)于SimpleRNN。
- init : 初始化權(quán)值的函數(shù)名稱或Theano function。可以使用Keras內(nèi)置的(內(nèi)置初始化權(quán)值函數(shù)見這里),也可以傳遞自己編寫的Theano function。如果不給weights傳遞參數(shù)時(shí),則該參數(shù)必須指明。
- inner_init : 內(nèi)部神經(jīng)元的權(quán)值初始化。
- activation : 激活函數(shù)名稱或者Theano function。可以使用Keras內(nèi)置的(內(nèi)置激活函數(shù)見這里),也可以是傳遞自己編寫的Theano function。如果不明確指定,那么將沒有激活函數(shù)會(huì)被應(yīng)用。
- inner_activation: 內(nèi)部隱層的激活函數(shù)。
- weights :用于初始化權(quán)值的numpy arrays組成的list。這個(gè)List應(yīng)該有depth+2個(gè)元素。
- truncate_gradient: 在BPTT(back propgation throughtime, BP算法加入了時(shí)間維度)算法中的truncate步數(shù)。
- return_sequence: Boolean.False返回在輸出序列中的最后一個(gè)輸出;True返回整個(gè)序列。
- input_dim:輸入數(shù)據(jù)的維度。當(dāng)把該層作為模型的第一層時(shí),這個(gè)參數(shù)和input_shape至少要提供一個(gè)傳值。
- input_length:輸入序列的長度。This argument is required ifyou are going to connectFlatten thenDense layers upstream (without it,the shape of the dense outputs cannot be computed)
三、GRU
keras.layers.recurrent.GRU(output_dim,
init='glorot_uniform', inner_init='orthogonal',
activation='sigmoid', inner_activation='hard_sigmoid',
weights=None, truncate_gradient=-1, return_sequences=False,
input_dim=None, input_length=None)
GRU(Gated Recurrent Unit)單元(2014年提出)。是實(shí)現(xiàn)RNN模型的主要單元之一。
** inputshape: 3維 tensor(nb_samples, timesteps,input_dim)
** outputshape: 如果return_sequences=True,那么輸出3維 tensor(nb_samples, timesteps, output_dim) .否則輸出2維tensor(nb_samples,output_dim)。
** Masking:This layer supports masking forinput data with a variable number of timesteps To introduce masks to your data,use an Embedding layer with themask_zero parameter set toTrue.
** 參數(shù):
- output_dim : 內(nèi)部計(jì)算和最終輸出的維度。
- init : 初始化權(quán)值的函數(shù)名稱或Theano function。可以使用Keras內(nèi)置的(內(nèi)置初始化權(quán)值函數(shù)見這里),也可以傳遞自己編寫的Theano function。如果不給weights傳遞參數(shù)時(shí),則該參數(shù)必須指明。
- inner_init : 內(nèi)部神經(jīng)元的權(quán)值初始化。
- activation : 激活函數(shù)名稱或者Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置激活函數(shù)見這里),也可以是傳遞自己編寫的Theano function。如果不明確指定,那么將沒有激活函數(shù)會(huì)被應(yīng)用。
- inner_activation: 內(nèi)部隱層的激活函數(shù)。
- weights :用于初始化權(quán)值的numpy arrays組成的list。這個(gè)List應(yīng)該有9個(gè)元素。
- truncate_gradient: 在BPTT(back propgation throughtime, BP算法加入了時(shí)間維度)算法中的truncate步數(shù)。
- return_sequence: Boolean.False返回在輸出序列中的最后一個(gè)輸出;True返回整個(gè)序列。
- input_dim:輸入數(shù)據(jù)的維度。當(dāng)把該層作為模型的第一層時(shí),這個(gè)參數(shù)和input_shape至少要提供一個(gè)傳值。
-
input_length:輸入序列的長度。This argument is required ifyou are going to connectFlatten thenDense layers upstream (without it,the shape of the dense outputs cannot be computed)
** 本小節(jié)參考文獻(xiàn)**:
On the Properties of NeuralMachine Translation: Encoder–Decoder Approaches
Empirical Evaluation of GatedRecurrent Neural Networks on Sequence Modeling
四、LSTM
keras.layers.recurrent.LSTM(output_dim,
init='glorot_uniform', inner_init='orthogonal', forget_bias_init='one',
activation='tanh', inner_activation='hard_sigmoid',
weights=None, truncate_gradient=-1, return_sequences=False,
input_dim=None, input_length=None)
LSTM(Long-Short Term Memoryunit)單元(1997年Hochreiter提出)。是用來構(gòu)建RNN網(wǎng)絡(luò)的主要單元之一。
** inputshape: 3維 tensor(nb_samples, timesteps,input_dim)
** outputshape: 如果return_sequences=True,那么輸出3維 tensor(nb_samples, timesteps, output_dim) .否則輸出2維tensor(nb_samples,output_dim)。
** Masking:This layer supports masking forinput data with a variable number of timesteps To introduce masks to your data,use an Embedding layer with themask_zero parameter set toTrue.
** 參數(shù):
- output_dim : 內(nèi)部計(jì)算和最終輸出的維度。
- init : 初始化權(quán)值的函數(shù)名稱或Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置初始化權(quán)值函數(shù)見這里),也可以傳遞自己編寫的Theano function。如果不給weights傳遞參數(shù)時(shí),則該參數(shù)必須指明。
- inner_init : 內(nèi)部神經(jīng)元的權(quán)值初始化。
- forget_bias_init: 初始化forget gate的偏置函數(shù)。Jozefowiczet al.推薦初始化為1。
- activation : 激活函數(shù)名稱或者Theano function。可以使用Keras內(nèi)置的(內(nèi)置激活函數(shù)見這里),也可以是傳遞自己編寫的Theano function。如果不明確指定,那么將沒有激活函數(shù)會(huì)被應(yīng)用。
- inner_activation: 內(nèi)部隱層的激活函數(shù)。
- weights :用于初始化權(quán)值的numpy arrays組成的list。這個(gè)List應(yīng)該有12個(gè)元素。
- truncate_gradient: 在BPTT(back propgation throughtime, BP算法加入了時(shí)間維度)算法中的truncate步數(shù)。
- return_sequence: Boolean.False返回在輸出序列中的最后一個(gè)輸出;True返回整個(gè)序列。
- input_dim:輸入數(shù)據(jù)的維度。當(dāng)把該層作為模型的第一層時(shí),這個(gè)參數(shù)和input_shape至少要提供一個(gè)傳值。
-
input_length:輸入序列的長度。This argument is required ifyou are going to connectFlatten thenDense layers upstream (without it,the shape of the dense outputs cannot be computed)
**** 本小節(jié)參考文獻(xiàn):
Longshort-term memory (original 1997 paper)
Learningto forget: Continual prediction with LSTM
Supervised sequencelabelling with recurrent neural networks
五、JZS1, JZS2, JZS3
keras.layers.recurrent.JZS1(output_dim,
init='glorot_uniform', inner_init='orthogonal',
activation='tanh', inner_activation='sigmoid',
weights=None, truncate_gradient=-1, return_sequences=False,
input_dim=None, input_length=None)
**** ****是在近千種模型評(píng)估中進(jìn)化而來的Top 3的RNN結(jié)構(gòu)單元。它的作用與GRU和LSTM是一樣的。其對(duì)應(yīng)的MUT1, MUT2, 和MUT3結(jié)構(gòu)是在《An Empirical Exploration of Recurrent NetworkArchitectures, Jozefowicz et al. 2015》中的提出來的。
**** inputshape: 3維 tensor(nb_samples, timesteps,input_dim)
**** outputshape: 如果return_sequences=True,那么輸出3維 tensor(nb_samples, timesteps, output_dim) .否則輸出2維tensor(nb_samples,output_dim)。
**** Masking:This layer supports masking forinput data with a variable number of timesteps To introduce masks to your data,use an Embedding layer with themask_zero parameter set toTrue.
**** 參數(shù):
- output_dim : 內(nèi)部計(jì)算和最終輸出的維度。
- init : 初始化權(quán)值的函數(shù)名稱或Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置初始化權(quán)值函數(shù)見這里),也可以傳遞自己編寫的Theano function。如果不給weights傳遞參數(shù)時(shí),則該參數(shù)必須指明。
- inner_init : 內(nèi)部神經(jīng)元的權(quán)值初始化。
- forget_bias_init: 初始化forget gate的偏置函數(shù)。Jozefowiczet al.推薦初始化為1。
- activation : 激活函數(shù)名稱或者Theano function??梢允褂肒eras內(nèi)置的(內(nèi)置激活函數(shù)見這里),也可以是傳遞自己編寫的Theano function。如果不明確指定,那么將沒有激活函數(shù)會(huì)被應(yīng)用。
- inner_activation: 內(nèi)部隱層的激活函數(shù)。
- weights :用于初始化權(quán)值的numpy arrays組成的list。這個(gè)List應(yīng)該有9個(gè)元素。
- truncate_gradient: 在BPTT(back propgation throughtime, BP算法加入了時(shí)間維度)算法中的truncate步數(shù)。
- return_sequence: Boolean.False返回在輸出序列中的最后一個(gè)輸出;True返回整個(gè)序列。
- input_dim:輸入數(shù)據(jù)的維度。當(dāng)把該層作為模型的第一層時(shí),這個(gè)參數(shù)和input_shape至少要提供一個(gè)傳值。
-
input_length:輸入序列的長度。This argument is required ifyou are going to connectFlatten thenDense layers upstream (without it,the shape of the dense outputs cannot be computed)
**** 本小節(jié)參考文獻(xiàn):
An EmpiricalExploration of Recurrent Network Architectures