尽管我对机器学习并不陌生,但我对神经网络仍然比较陌生,更具体地说是如何实现它们(在 Keras/Python 中)。前馈和卷积架构相当简单,但我在使用 RNN 时遇到了麻烦。
我的 X
数据由可变长度序列组成,该序列中的每个数据点都有 26 个特征。我的 y
数据虽然长度可变,但每对 X
和 y
具有相同的长度,例如:
X_train[0].shape: (226,26)
y_train[0].shape: (226,)
X_train[1].shape: (314,26)
y_train[1].shape: (314,)
X_train[2].shape: (189,26)
y_train[2].shape: (189,)
我的目标是将序列中的每个项目分类为 39 个类别之一。
到目前为止,我通过阅读示例代码可以了解到,我们做了如下的事情:
encoder_inputs = Input(shape=(None, 26))
encoder = GRU(256, return_state=True)
encoder_outputs, state_h = encoder(encoder_inputs)
decoder_inputs = Input(shape=(None, 39))
decoder_gru= GRU(256, return_sequences=True)
decoder_outputs, _ = decoder_gru(decoder_inputs, initial_state=state_h)
decoder_dense = Dense(39, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.compile(loss=keras.losses.categorical_crossentropy,
optimizer=keras.optimizers.Adadelta(),
metrics=['accuracy'])
这对我来说很有意义,因为每个序列都有不同的长度。
因此,对于遍历所有序列的 for 循环,我们在第一个 GRU 层的输入形状中使用 None
因为我们不确定序列长度是多少,然后返回隐藏状态 该编码器的state_h
。第二个 GRU 层返回序列,并且初始状态是从编码器返回的状态,然后我们将输出传递到最终的 softmax 激活层。
显然这里有一些缺陷,因为我得到:
decoder_outputs, _ = decoder_gru(decoder_inputs, initial_state=state_h)
File "/usr/local/lib/python3.6/dist-
packages/tensorflow/python/framework/ops.py", line 458, in __iter__
"Tensor objects are only iterable when eager execution is "
TypeError: Tensor objects are only iterable when eager execution is
enabled. To iterate over this tensor use tf.map_fn.
This link指向建议的解决方案,但我不明白为什么您要将编码器状态添加到网络中尽可能多的层的元组中。
我真的在寻求帮助,以便能够成功编写这个 RNN 来完成这项任务,同时也理解。我对 RNN 非常感兴趣,并希望更深入地了解它们,以便将它们应用于其他问题。
作为额外说明,每个序列的形状为 (sequence_length, 26)
,但我将 (1,equence_length, 26)
的维度扩展为 >X
和 (1,sequence_length)
为 y
,然后将它们传递到 for 循环中以进行拟合,并使用 decoder_target_data
比当前输入提前一步:
for idx in range(X_train.shape[0]):
X_train_s = np.expand_dims(X_train[idx], axis=0)
y_train_s = np.expand_dims(y_train[idx], axis=0)
y_train_s1 = np.expand_dims(y_train[idx+1], axis=0)
encoder_input_data = X_train_s
decoder_input_data = y_train_s
decoder_target_data = y_train_s1
model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
epochs=50,
validation_split=0.2)
对于我编写的其他网络(前馈和 CNN),我通过在 Keras 的 Sequential 类之上添加层来指定模型。由于 RNN 固有的复杂性,我认为使用上面的 Keras 输入类并检索隐藏状态(以及 LSTM 的单元状态)等的一般格式是合乎逻辑的,但我也看到它们是使用 Keras 的 Sequential 类构建的。尽管这些是多对一类型的任务,但我也对您如何以这种方式编写它感兴趣。
最佳答案
问题是 decoder_gru
层不返回其状态,因此您不应使用 _
作为状态的返回值(即仅删除 ,_
):
decoder_outputs = decoder_gru(decoder_inputs, initial_state=state_h)
<小时/>
由于输入和输出长度相同,并且输入和输出的元素之间存在一对一的映射,因此您也可以通过以下方式构建模型:
inputs = Input(shape=(None, 26))
gru = GRU(64, return_sequences=True)(inputs)
outputs = Dense(39, activation='softmax')(gru)
model = Model(inputs, outputs)
现在,您可以通过将多个 GRU 层堆叠在一起来使该模型更加复杂(即增加其容量):
inputs = Input(shape=(None, 26))
gru = GRU(256, return_sequences=True)(inputs)
gru = GRU(128, return_sequences=True)(gru)
gru = GRU(64, return_sequences=True)(gru)
outputs = Dense(39, activation='softmax')(gru)
model = Model(inputs, outputs)
此外,您可以使用具有更多表示能力的 LSTM 层,而不是使用 GRU 层(当然这可能会以增加计算成本为代价)。并且不要忘记,当您增加模型的容量时,也会增加过度拟合的机会。因此,您必须牢记这一点并考虑防止过度拟合的解决方案(例如添加正则化)。
旁注:如果您有可用的 GPU,则可以使用 CuDNNGRU
(或 CuDNNLSTM
)层,它已针对 GPU 进行了优化,因此与 GRU
相比,它的运行速度要快得多。
关于python - 如何在 Keras 中正确设置 RNN 以进行序列到序列建模?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52023702/