备注:
return_sequences: 默认是False,控制LSTM的输出:
False: 仅返回最后一个时刻的状态(hidden_state),shape = [B,H,W,C],是一个4维张量True: 返回所有时刻的输出(hidden_state),shape = [B,Clip_len,H,W,C],是一个5维张量用return_state=True控制
import tensorflow as tf import numpy as np import keras from keras.layers import ConvLSTM2D lstm_input = np.random.random((4,6,30,30,3)).astype(np.float32) lstm_input = tf.convert_to_tensor(lstm_input) lstm_out,state_h,state_c = ConvLSTM2D(filters=1,kernel_size=[5,5],strides=(1,1),padding='valid',activation='relu', batch_input_shape=(-1,6,30,30,3),return_sequences=False,return_state=True)(lstm_input) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) lstm_out_,state_h_,state_c_= sess.run([lstm_out,state_h,state_c]) print(lstm_out_==state_h_) print(lstm_out_.shape) print(state_h_.shape) print(state_c_.shape) """ 返回: [ True]]]] (4, 26, 26, 1) (4, 26, 26, 1) (4, 26, 26, 1) """综上:
return_sequences: 决定是否返回所有时刻的状态
return_state:决定是否返回最后一个时刻的cell状态,由示例2结果可见,最后一个时刻的state = [h,c]
注意:在keras 中文文档中,在介绍ConvLSTM2D时,没有介绍 return_state 参数,该参数在LSTM的介绍中介绍,但是在ConvLSTM2D中通用。。。自己差点以为ConvLSTM2D中没有这个功能。。。
