我正在构建一个模型,使用循环图层(GRU)将字符串转换为另一个字符串.我已经尝试了Dense和TimeDistributed(密集)层作为最后一层,但我不明白使用return_sequences = True时两者之间的区别,特别是因为它们似乎具有相同数量的参数.
我的简化模型如下:
InputSize = 15
MaxLen = 64
HiddenSize = 16
inputs = keras.layers.Input(shape=(MaxLen, InputSize))
x = keras.layers.recurrent.GRU(HiddenSize, return_sequences=True)(inputs)
x = keras.layers.TimeDistributed(keras.layers.Dense(InputSize))(x)
predictions = keras.layers.Activation('softmax')(x)
Run Code Online (Sandbox Code Playgroud)
网络摘要是:
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
input_1 (InputLayer) (None, 64, 15) 0
_________________________________________________________________
gru_1 (GRU) (None, 64, 16) 1536
_________________________________________________________________
time_distributed_1 (TimeDist (None, 64, 15) 255
_________________________________________________________________
activation_1 (Activation) (None, 64, 15) 0
=================================================================
Run Code Online (Sandbox Code Playgroud)
这对我来说很有意义,因为我对TimeDistributed的理解是它在所有时间点都应用相同的层,因此Dense层有16*15 + 15 = 255个参数(权重+偏差).
但是,如果我切换到一个简单的Dense图层:
inputs = keras.layers.Input(shape=(MaxLen, InputSize))
x = keras.layers.recurrent.GRU(HiddenSize, return_sequences=True)(inputs)
x = …
Run Code Online (Sandbox Code Playgroud) machine-learning neural-network keras recurrent-neural-network keras-layer