RNN神经网络层的输出格式和形状
tf.keras.layers.RNN(
cell, return_sequences=False, return_state=False, go_backwards=False,
stateful=False, unroll=False, time_major=False, **kwargs
)
RNN的输出非常tricky,官方文档要细读。
If return_state
: a list of tensors. The first tensor is the output. The remaining tensors are the last states, each with shape [batch_size, state_size]
, where state_size could be a high dimension tensor shape.
If return_sequences: N-D tensor with shape [batch_size, timesteps, output_size]
, where output_size could be a high dimension tensor shape, or [timesteps, batch_size, output_size]
when time_major is True.
Else, N-D tensor with shape [batch_size, output_size]
, where output_size could be a high dimension tensor shape.
如果return_state=True
,return_sequences=True
,输出的一个list,第一个元素是output,shape [batch_size, timesteps, output_size]
,第二个元素是latest states,注意是latest,最后时刻的states,而不是每个时刻的states,所以shape [batch_size, state_size]
out = keras.layers.RNN(LNSimpleRNNCell(20), return_sequences=True, return_state=True,
input_shape=[None, 1])(X_train)
X_train.shape # (7000, 50, 1)
out[0].shape # ([7000, 50, 20])
out[1].shape # ([7000, 20])