이론과 관련된 내용은 여기를 참고하시면 됩니다.
간단한 RNN 모델
from keras.models import Sequential
from keras.layers import SimpleRNN
model = Sequential()
model.add(SimpleRNN(3, input_shape=(2,10)))
# model.add(SimpleRNN(3, input_length=2, input_dim=10))와 동일함.
model.summary()
----------------------------------------------------------------
Layer (type) Output Shape Param #
=================================================================
simple_rnn_1 (SimpleRNN) (None, 3) 42
=================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
model = Sequential()
model.add(SimpleRNN(3, batch_input_shape=(8,2,10)))
model.summary()
----------------------------------------------------------
Layer (type) Output Shape Param #
=================================================================
simple_rnn_2 (SimpleRNN) (8, 3) 42
=================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
model = Sequential()
model.add(SimpleRNN(3, batch_input_shape=(8,2,10), return_sequences=True))
model.summary()
------------------------------------------------------------------------
Layer (type) Output Shape Param #
=================================================================
simple_rnn_3 (SimpleRNN) (8, 2, 3) 42
=================================================================
Total params: 42
Trainable params: 42
Non-trainable params: 0
rnn = SimpleRNN(3, return_sequences=True, return_state=True)
hidden_states, last_state = rnn(train_X)
print('hidden states : {}, shape: {}'.format(hidden_states, hidden_states.shape))
print('last hidden state : {}, shape: {}'.format(last_state, last_state.shape))
-----------------------------------------------------------------------------------
hidden states : [[[ 0.29839835 -0.99608386 0.2994854 ]
[ 0.9160876 0.01154806 0.86181474]
[-0.20252597 -0.9270214 0.9696659 ]
[-0.5144398 -0.5037417 0.96605766]]], shape: (1, 4, 3)
last hidden state : [[-0.5144398 -0.5037417 0.96605766]], shape: (1, 3)
깊은 RNN 모델
model = Sequential()
model.add(SimpleRNN(hidden_size, return_sequences = True))
model.add(SimpleRNN(hidden_size, return_sequences = True))
Bidirectional RNN 모델
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import SimpleRNN, Bidirectional
model = Sequential()
model.add(Bidirectional(SimpleRNN(hidden_size, return_sequences = True), input_shape=(timesteps, input_dim)))