TVP

# 理解循环神经网络RNN

#Listing 6.19 Pseudocode RNN

'''The state at t

'''

state_t=

'''Iterates over sequence elements

'''

forinput_tininput_sequence:

output_t=f(input_t,state_t)

'''The previous output becomes the state for the next iteration.

'''

state_t=output_t

#Listing 6.20 More detailed pseudocode for the RNN

state_t=

forinput_tininput_sequence:

output_t=activation(dot(W,input_t)+dot(U,state_t)+b)

state_t=output_t

#Listing 6.21 Numpy implementation of a simple RNN

importnumpyasnp

'''Number of timesteps in

the input sequence

'''

timesteps=100

'''Dimensionality of the

input feature space

'''

input_features=32

'''Dimensionality of the

output feature space

'''

output_features=64

'''Input data: random

noise for the sake of

the example

'''

inputs=np.random.random((timesteps,input_features))

'''Initial state: an

all-zero vector

'''

state_t=np.zeros((output_features,))

'''Creates random weight matrices

'''

W=np.random.random((output_features,input_features))

U=np.random.random((output_features,output_features))

b=np.random.random((output_features,))

successive_outputs= []

'''input_t is a vector of shape (input_features,).

'''

forinput_tininputs:

'''Combines the input with the current

state (the previous output) to obtain

the current output

'''

output_t=np.tanh(np.dot(W,input_t)+np.dot(U,state_t)+b)

'''Stores this output in a list

'''

successive_outputs.append(output_t)

network for the next timestep

'''

state_t=output_t

'''The final output is a 2D tensor of

shape (timesteps, output_features).

'''

final_output_sequence=np.concatenate(successive_outputs,axis=)

output_t=np.tanh(np.dot(W,input_t)+np.dot(U,state_t)+b)

fromkeras.layersimportSimpleRNN

fromkeras.modelsimportSequential

fromkeras.layersimportEmbedding,SimpleRNN

model=Sequential()

model.summary()

________________________________________________________________

Layer (type) Output Shape Param#

================================================================

embedding_22 (Embedding) (None, None,32)320000

________________________________________________________________

simplernn_10 (SimpleRNN) (None,32)2080

================================================================

Total params:322,080

Trainable params:322,080

Non-trainable params:

model=Sequential()

model.summary()

________________________________________________________________

Layer(type)OutputShapeParam#

================================================================

embedding_23(Embedding) (None,None,32)320000

________________________________________________________________

simplernn_11(SimpleRNN) (None,None,32)2080

================================================================

Totalparams:322,80

Trainableparams:322,80

Non-trainableparams:

>>>model=Sequential()

#Last layer only returns the last output

>>>model.summary()

________________________________________________________________

Layer (type) Output Shape Param#

================================================================

embedding_24 (Embedding) (None, None,32)320000

________________________________________________________________

simplernn_12 (SimpleRNN) (None, None,32)2080

________________________________________________________________

simplernn_13 (SimpleRNN) (None, None,32)2080

________________________________________________________________

simplernn_14 (SimpleRNN) (None, None,32)2080

________________________________________________________________

simplernn_15 (SimpleRNN) (None,32)2080

================================================================

Total params:328,320

Trainable params:328,320

Non-trainable params:

#Listing 6.22 Preparing the IMDB data

fromkeras.datasetsimportimdb

fromkeras.preprocessingimportsequence

'''Number of words to

consider as features

'''

max_features=10000

'''Cuts off texts after this many words (among

the max_features most common words)

'''

maxlen=500

batch_size=32

num_words=max_features)

print(len(input_train),'train sequences')

print(len(input_test),'test sequences')

print('input_train shape:',input_train.shape)

print('input_test shape:',input_test.shape)

#Listing 6.23 Training the model with Embedding and SimpleRNN layers

fromkeras.layersimportDense

model=Sequential()

model.compile(optimizer='rmsprop',loss='binary_crossentropy',metrics=['acc'])

history=model.fit(input_train,y_train,

epochs=10,

batch_size=128,

validation_split=0.2)

#Listing 6.24 Plotting results

importmatplotlib.pyplotasplt

acc=history.history['acc']

val_acc=history.history['val_acc']

loss=history.history['loss']

val_loss=history.history['val_loss']

epochs=range(1,len(acc)+1)

plt.plot(epochs,acc,'bo',label='Training acc')

plt.plot(epochs,val_acc,'b',label='Validation acc')

plt.title('Training and validation accuracy')

plt.legend()

plt.figure()

plt.plot(epochs,loss,'bo',label='Training loss')

plt.plot(epochs,val_loss,'b',label='Validation loss')

plt.title('Training and validation loss')

plt.legend()

plt.show()

y=activation(dot(state_t,U)+dot(input_t,W)+b)

#Listing 6.25 Pseudocode details of the LSTM architecture (1/2)

output_t=activation(dot(state_t,Uo)+dot(input_t,Wo)+dot(C_t,Vo)+bo)

i_t=activation(dot(state_t,Ui)+dot(input_t,Wi)+bi)

f_t=activation(dot(state_t,Uf)+dot(input_t,Wf)+bf)

k_t=activation(dot(state_t,Uk)+dot(input_t,Wk)+bk)

#Listing 6.26 Pseudocode details of the LSTM architecture (2/2)

c_t+1=i_t*k_t+c_t*f_t

#Listing 6.27 Using the LSTM layer in Keras

fromkeras.layersimportLSTM

model=Sequential()

model.compile(optimizer='rmsprop',

loss='binary_crossentropy',

metrics=['acc'])

history=model.fit(input_train,y_train,

epochs=10,

batch_size=128,

validation_split=0.2)

LSTM是什么？它为什么在处理长序列上比原生RNN效果好？

• 发表于:
• 原文链接https://kuaibao.qq.com/s/20180903G09IVB00?refer=cp_1026
• 腾讯「腾讯云开发者社区」是腾讯内容开放平台帐号（企鹅号）传播渠道之一，根据《腾讯内容开放平台服务协议》转载发布内容。
• 如有侵权，请联系 cloudcommunity@tencent.com 删除。

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25

2024-06-25