本文介绍了PyTorch中是否存在干净且可扩展的LSTM实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我想自己创建一个 LSTM 类,但是,我不想重写经典的 LSTM 从头开始重新运行。I would like to create an LSTM class by myself, however, I don't want to rewrite the classic LSTM functions from scratch again.在 PyTorch 的代码中,我发现一个肮脏的实现至少涉及3-4个带有继承的类:Digging in the code of PyTorch, I only find a dirty implementation involving at least 3-4 classes with inheritance: https://github.com/pytorch/pytorch/blob/98c24fae6b6400a7d1e13610b20aa05f86f77070/torch/nn/modules/rnn.py#L323 https://github.com/pytorch/pytorch /blob/98c24fae6b6400a7d1e13610b20aa05f86f77070/torch/nn/modules/rnn.py#L12 https://github.com/pytorch/pytorch/blob/98c24fae6b6400a7py1e13610b20aa05f86f770n a>是否对 LSTM clean PyTorch 实施$ c>存在于某处?任何链接都将有所帮助。Does a clean PyTorch implementation of an LSTM exist somewhere? Any links would help.例如,我知道 TensorFlow 中存在 LSTM 的干净实现,但我需要派生一个 PyTorch 。For example, I know that clean implementations of a LSTM exists in TensorFlow, but I would need to derive a PyTorch one.举个简单的例子,我要搜索的是一个与此,但在 PyTorch 中:For a clear example, what I'm searching for is an implementation as clean as this, but in PyTorch:推荐答案我发现的最佳实现是在这里 > https://github.com/pytorch/benchmark/blob/master/rnns/benchmarks/lstm_variants/lstm.pyThe best implementation I found is herehttps://github.com/pytorch/benchmark/blob/master/rnns/benchmarks/lstm_variants/lstm.py它甚至实现了周期性辍学的四种不同变体,这非常有用! 如果将辍学部分拿走,您会得到It even implements four different variants of recurrent dropout, which is very useful!If you take the dropout parts away you getimport mathimport torch as thimport torch.nn as nnclass LSTM(nn.Module): def __init__(self, input_size, hidden_size, bias=True): super(LSTM, self).__init__() self.input_size = input_size self.hidden_size = hidden_size self.bias = bias self.i2h = nn.Linear(input_size, 4 * hidden_size, bias=bias) self.h2h = nn.Linear(hidden_size, 4 * hidden_size, bias=bias) self.reset_parameters() def reset_parameters(self): std = 1.0 / math.sqrt(self.hidden_size) for w in self.parameters(): w.data.uniform_(-std, std) def forward(self, x, hidden): h, c = hidden h = h.view(h.size(1), -1) c = c.view(c.size(1), -1) x = x.view(x.size(1), -1) # Linear mappings preact = self.i2h(x) + self.h2h(h) # activations gates = preact[:, :3 * self.hidden_size].sigmoid() g_t = preact[:, 3 * self.hidden_size:].tanh() i_t = gates[:, :self.hidden_size] f_t = gates[:, self.hidden_size:2 * self.hidden_size] o_t = gates[:, -self.hidden_size:] c_t = th.mul(c, f_t) + th.mul(i_t, g_t) h_t = th.mul(o_t, c_t.tanh()) h_t = h_t.view(1, h_t.size(0), -1) c_t = c_t.view(1, c_t.size(0), -1) return h_t, (h_t, c_t) PS:存储库包含LSTM和其他RNN的更多变体: https://github.com/pytorch/benchmark/tree/master/rnns/benchmarks 。 检查出来,也许您已经想到的扩展名已经存在!PS: The repository contains many more variants of LSTM and other RNNs:https://github.com/pytorch/benchmark/tree/master/rnns/benchmarks.Check it out, maybe the extension you had in mind is already there!编辑: 如评论中所述,您可以包装LSTM上面的单元格以处理顺序输出:As mentioned in the comments, you can wrap the LSTM cell above to process sequential output:import mathimport torch as thimport torch.nn as nnclass LSTMCell(nn.Module): def __init__(self, input_size, hidden_size, bias=True): # As before def reset_parameters(self): # As before def forward(self, x, hidden): if hidden is None: hidden = self._init_hidden(x) # Rest as before @staticmethod def _init_hidden(input_): h = th.zeros_like(input_.view(1, input_.size(1), -1)) c = th.zeros_like(input_.view(1, input_.size(1), -1)) return h, cclass LSTM(nn.Module): def __init__(self, input_size, hidden_size, bias=True): super().__init__() self.lstm_cell = LSTMCell(input_size, hidden_size, bias) def forward(self, input_, hidden=None): # input_ is of dimensionalty (1, time, input_size, ...) outputs = [] for x in torch.unbind(input_, dim=1): hidden = self.lstm_cell(x, hidden) outputs.append(hidden[0].clone()) return torch.stack(outputs, dim=1)由于我正在使用convLSTM实现,因此我没有测试过代码。I havn't tested the code since I'm working with a convLSTM implementation. Please let me know if something is wrong.更新:固定链接。 这篇关于PyTorch中是否存在干净且可扩展的LSTM实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-03 10:04