【问题标题】:Which axis of the input of the RNN is used as the "temporal" axis in Keras?RNN 输入的哪一个轴被用作 Keras 中的“时间”轴?
【发布时间】:2020-06-23 12:34:23
【问题描述】:

将 SimpleRNN 或 LSTM 用于经典情感分析算法时(此处适用于长度

model = Sequential()
model.add(Embedding(5000, 32, input_length=250))   # Output shape: (None, 250, 32)
model.add(SimpleRNN(100))                          # Output shape: (None, 100)
model.add(Dense(1, activation='sigmoid'))          # Output shape: (None, 1)

在哪里指定RNN输入的哪个轴作为“时间”轴?

更准确地说,在 Embedding 层之后,给定的输入句子,例如“the cat sat on the mat”被编码为形状为 (250, 32) 的矩阵 x,其中 250 是输入文本的最大长度(以字为单位),32 是嵌入的维度。然后,在 Keras 中的哪个位置指定是否使用它:

h[t] = activation( W_h * x[:, t] + U_h * h[t-1] + b_h )

或者这个:

h[t] = activation( W_h * x[t, :] + U_h * h[t-1] + b_h )

(在这两种情况下,y[t] = activation( W_y * h[t] + b_y )

TL;DR:如果 RNN Keras 层的输入大小为 (250, 32),默认情况下它使用哪个轴作为时间轴? Keras 或 Tensorflow 文档中的哪个位置对此进行了详细说明?

【问题讨论】:

标签: classification tensorflow keras model


【解决方案1】:

公式h[t] = activation( W_h * x[t, :] + U_h * h[t-1] + b_h ) 将被KerasTensorflow 使用。

在这个Tensorflow Documentation中,提到了

inputs: A 3D tensor, with shape [batch, timesteps, feature].

我们将忽略第一个参数batch 并考虑其他两个参数一段时间。

在情绪分析的情况下,我们可以将每个word/token(转换为向量)视为Timestep,如下面的屏幕截图所示。

如果您的示例使用形状为 (250, 32) 的编码矩阵,则意味着每个评论或实例有 250 个 words/tokensTimesteps

所以,等式h[t] = activation( W_h * x[t, :] + U_h * h[t-1] + b_h )可以翻译成

h[t] = activation( W_h * x[Each Time Step/Word, All Features] + U_h * h[Previous Time Step/Word] + b_h )

希望这能澄清您的问题。快乐学习!

【讨论】:

    猜你喜欢
    • 2020-06-19
    • 1970-01-01
    • 2022-01-15
    • 2020-06-10
    • 2019-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多