keras.layers.LSTM()函数有两个重要的参数return_sequences和return_state
return_sequences和return_state默认都为false,接下来分别讲解当return_sequences和return_state取不同值时LSTM函数的返回值
return_sequences=True会返回每个时间步的隐藏状态,=False只会返回最后一个时间步的隐藏状态
return_state=True时会返回三个变量:lstm, state_h, state_c,lsmt为最后一个时间步的隐藏状态或每个时间步的隐藏状态(取决于return_sequences的值),state_h为最后一个时间步的隐藏状态,state_c为最后一个时间步的cell状态
state_h = LSTM(,return_sequences = False, return_state = False)
返回:
state_h:最后一个时间步的隐藏状态
lstm = LSTM(,return_sequences = True, return_state = False)
返回:
lstm:每个时间步的隐藏状态(例如输入为长度为3的序列,则输出3个隐藏状态)
lstm, state_h, state_c = LSTM(,return_sequences = False, return_state = True)
返回:
lstm:最后一个时间步的隐藏状态
state_h:最后一个时间步的隐藏状态(和lstm相同)
state_c:最后一个时间步的cell状态
lstm, state_h, state_c = LSTM(,return_sequences = True, return_state = True)
返回:
lstm:每个时间步的隐藏状态
state_h:最后一个时间步的隐藏状态
state_c:最后一个时间步的cell状态