11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Keras , LSTM:return_sequencesとreturn_stateの違い

Last updated at Posted at 2018-11-14

はじめに

KerasのRecurrentLayerにおける引数return_sequencesとreturn_stateの違いの認識が曖昧だったので,忘備録として書き残しておきます.
詳細は参考文献.

return_sequences

return_sequences=Trueで出力系列を返す.

from keras.models import Model
from keras.layers import Input
from keras.layers import LSTM
from numpy import array
# モデルの定義
inputs1 = Input(shape=(3, 1))
lstm = LSTM(1, return_sequences=True)(inputs1)
model = Model(inputs=inputs1, outputs=lstm)
# インプットデータの成型
data = array([0.1, 0.2, 0.3]).reshape((1,3,1))
# 予測
print(model.predict(data))

入力のshapeが(3, 1)(timestepが3)なので, 各timestep毎の隠れ層の出力(入力系列に対する出力)が以下のように返される.

[[[-0.02243521]
  [-0.06210149]
  [-0.11457888]]]

LSTMを複数重ねるときや,各出力を組み合わせて使うときなどに用いるらしい.

return_state

return_state=Trueで出力とともに最後の状態を返す.
隠れ層の最後の出力とともに,cellの(最後の)内部状態state_cを返す.

from keras.models import Model
from keras.layers import Input
from keras.layers import LSTM
from numpy import array
# モデルの定義
inputs1 = Input(shape=(3, 1))
lstm1, state_h, state_c = LSTM(1, return_state=True)(inputs1)
model = Model(inputs=inputs1, outputs=[lstm1, state_h, state_c])
# インプットデータの成型
data = array([0.1, 0.2, 0.3]).reshape((1,3,1))
# 予測
print(model.predict(data))
[array([[ 0.10951342]], dtype=float32),
 array([[ 0.10951342]], dtype=float32),
 array([[ 0.24143776]], dtype=float32)]

出力はそれぞれ,
-LSTMの隠れ層の最後の出力.
-LSTMの隠れ層の最後の出力(return_sequencesとreturn_stateを組み合わせて出力するときに有用(詳細は引用文献)).
-LSTMの隠れ層の最後の状態.
LSTMの最後の状態を他のLSTMの初期状態として用いるときなどに利用する.(ただしセルの数は等しい必要がある)

引用
Understand the Difference Between Return Sequences and Return States for LSTMs in Keras
Keras Documentation

11
8
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?