deep-learning - PyTorch 中带有 Sequential 模块的简单 LSTM

标签 deep-learning torch lstm recurrent-neural-network pytorch

在 PyTorch 中,我们可以通过多种方式定义架构。在这里,我想使用 Sequential 创建一个简单的 LSTM 网络。模块。

在 Lua 的火炬中,我通常会选择:

model = nn.Sequential()
model:add(nn.SplitTable(1,2))
model:add(nn.Sequencer(nn.LSTM(inputSize, hiddenSize)))
model:add(nn.SelectTable(-1)) -- last step of output sequence
model:add(nn.Linear(hiddenSize, classes_n))

但是,在 PyTorch 中,我找不到 SelectTable 的等价物。获得最后的输出。
nn.Sequential(
  nn.LSTM(inputSize, hiddenSize, 1, batch_first=True),
  # what to put here to retrieve last output of LSTM ?,
  nn.Linear(hiddenSize, classe_n))

最佳答案

首先,我让 i 类提取最后一个单元格输出,如下所示

class extractlastcell(nn.Module):
def forward(self,x):
    out , _ = x
    return out[:, -1, :]
当我想在你的例子中使用它时,它会是这样的
nn.Sequential(
nn.LSTM(inputSize, hiddenSize, 1, batch_first=True),
extractlastcell(),
nn.Linear(hiddenSize, classe_n))

关于deep-learning - PyTorch 中带有 Sequential 模块的简单 LSTM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44130851/

相关文章:

python - Keras 中 LSTM 的时间分布式层和返回序列等

python - 在 Tensorflow 中微调深度神经网络

image-processing - 解析文本格式 caffe.NetParameter : 54:17: Message type "caffe.ConvolutionParameter" has no field named "sparse_ratio" 时出错

lua - 使用Torch-hdf5将Tensor保存到Hdf5

python - 将 'int' 转换为 pytorch 'Variable' 会出现问题

python - ValueError : Error when checking target: expected dense_19 to have 3 dimensions, 但得到形状为 (5, 3) 的数组

machine-learning - Keras 中 LSTM 输入维度的问题

machine-learning - Keras:堆叠多个 LSTM 层

tensorflow - 如何使用 tensorflow 执行多标签分类?

Lua类成员函数相互调用