gpt4 book ai didi

tensorflow - 了解 Seq2Seq 模型

转载 作者:行者123 更新时间:2023-12-04 12:28:12 26 4
gpt4 key购买 nike

这是我对基本序列到序列 LSTM 的理解。假设我们正在处理问答设置。

你有两组 LSTM(下面的绿色和蓝色)。每组分别共享权重(即 4 个绿色单元格中的每一个都具有相同的权重,并且与蓝色单元格相似)。第一个是多对一LSTM,总结了问题在最后一个隐藏层/单元内存 .

第二组(蓝色)是多对多 LSTM,其权重与第一组 LSTM 不同。输入只是答案句子,而输出是同一个句子移位了一个。

问题有两个方面:
1.我们是否通过了最后一个隐藏状态仅限 将蓝色 LSTM 作为初始隐藏状态。还是最后一个隐藏状态和单元内存 .
2. 有没有办法在 Keras 或 Tensorflow 中设置初始隐藏状态和单元内存?如果有引用?

http://suriyadeepan.github.io/img/seq2seq/seq2seq2.png
(图片取自 suriyadeepan.github.io)

最佳答案

  1. Are we passing the last hidden state only to the blue LSTMs as the initial hidden state. Or is it last hidden state and cell memory.


两种隐藏状态 h和单元内存 c传递给解码器。

TensorFlow

seq2seq source code ,您可以在 basic_rnn_seq2seq() 中找到以下代码:

_, enc_state = rnn.static_rnn(enc_cell, encoder_inputs, dtype=dtype)
return rnn_decoder(decoder_inputs, enc_state, cell)

如果您使用 LSTMCell ,返回 enc_state来自编码器的将是一个元组 (c, h) .如您所见,元组直接传递给解码器。

喀拉斯

在 Keras 中,为 LSTMCell 定义的“状态”也是一个元组 (h, c) (注意顺序与TF不同)。在 LSTMCell.call() , 你可以找到:
    h_tm1 = states[0]
c_tm1 = states[1]

获取从 LSTM 返回的状态层,可以指定 return_state=True .返回值是一个元组 (o, h, c) .张量 o是该层的输出,将等于 h除非您指定 return_sequences=True .

  1. Is there a way to set the initial hiddden state and cell memory in Keras or Tensorflow? If so reference?


TensorFlow

只需将初始状态提供给 LSTMCell调用它时。例如,在 official RNN tutorial :
lstm = tf.contrib.rnn.BasicLSTMCell(lstm_size)
...
output, state = lstm(current_batch_of_words, state)

还有一个 initial_state tf.nn.static_rnn 等函数的参数.如果您使用 seq2seq 模块,请将状态提供给 rnn_decoder如问题 1 的代码所示。

喀拉斯

使用关键字参数 initial_state在 LSTM 函数调用中。
out = LSTM(32)(input_tensor, initial_state=(h, c))

您实际上可以在 the official documentation 上找到这种用法。 :

Note on specifying the initial state of RNNs

You can specify the initial state of RNN layers symbolically by calling them with the keyword argument initial_state. The value of initial_state should be a tensor or list of tensors representing the initial state of the RNN layer.



编辑:

现在 Keras 中有一个示例脚本 ( lstm_seq2seq.py),展示了如何在 Keras 中实现基本的 seq2seq。该脚本还介绍了如何在训练 seq2seq 模型后进行预测。

关于tensorflow - 了解 Seq2Seq 模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46355651/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com