V1n*_*3nt 6 python neural-network deep-learning lstm keras
我想使用Yang等人的注意机制的实现..我发现了一个自定义图层的工作实现,它在这里使用了这种注意力机制.而不是使用我的LSTM的输出值:
my_lstm = LSTM(128, input_shape=(a, b), return_sequences=True)
my_lstm = AttentionWithContext()(my_lstm)
out = Dense(2, activation='softmax')(my_lstm)
Run Code Online (Sandbox Code Playgroud)
我想使用LSTM的隐藏状态:
my_lstm = LSTM(128, input_shape=(a, b), return_state=True)
my_lstm = AttentionWithContext()(my_lstm)
out = Dense(2, activation='softmax')(my_lstm)
Run Code Online (Sandbox Code Playgroud)
但我得到错误:
TypeError:只能将元组(不是"int")连接到元组
我尝试将它与return_sequences结合使用但到目前为止我尝试过的所有内容都失败了.如何修改返回的张量以便像返回的输出序列一样使用它?
谢谢!
Nic*_*ite 11
我认为你的困惑可能源于Keras文档有点不清楚.
return_sequences: Boolean. Whether to return the last output in the output sequence, or the full sequence.
return_state: Boolean. Whether to return the last state in addition to the output.
Run Code Online (Sandbox Code Playgroud)
文档return_state特别令人困惑,因为它们暗示隐藏状态与输出不同,但它们是同一个.对于LSTM,这有点模糊,因为除了隐藏(输出)状态之外,还有单元状态.我们可以通过查看Keras代码中的LSTM步骤函数来确认这一点:
class LSTM(Recurrent):
def step(...):
...
return h, [h, c]
Run Code Online (Sandbox Code Playgroud)
此步骤函数的返回类型是output, states.因此我们可以看到隐藏状态h实际上是输出,对于状态,我们得到隐藏状态h和单元状态c.这就是为什么您可以互换地使用术语"隐藏"和"输出"链接的Wiki文章.
看一下你把论文链接得更近一点,在我看来你最初的实现就是你想要的.
my_lstm = LSTM(128, input_shape=(a, b), return_sequences=True)
my_lstm = AttentionWithContext()(my_lstm)
out = Dense(2, activation='softmax')(my_lstm)
Run Code Online (Sandbox Code Playgroud)
这会将每个时间步的隐藏状态传递给您的注意层.你运气不好的唯一情况是你真正希望将每个时间步的细胞状态传递到你的注意层(这是我最初的想法),但我认为这不是你想要的.您链接的纸张实际上使用GRU图层,该图层没有单元格状态的概念,并且其step函数也将隐藏状态作为输出返回.
class GRU(Recurrent):
def step(...):
...
return h, [h]
Run Code Online (Sandbox Code Playgroud)
所以本文几乎肯定是指隐藏状态(也就是输出)而不是细胞状态.
| 归档时间: |
|
| 查看次数: |
3816 次 |
| 最近记录: |