将LSTM中的Tanh激活更改为ReLU

问题描述 投票:3回答:1

LSTM类中的默认非线性激活函数是tanh。我希望将ReLU用于我的项目。浏览文档和其他资源,我无法以简单的方式找到方法。我找到的唯一方法是定义我自己的自定义LSTMCell,但here作者说自定义LSTMCells不支持GPU加速功能(或者自文章发布以来已经改变了吗?)。我需要使用CUDA来加速我的训练。任何帮助,将不胜感激。

lstm pytorch
1个回答
5
投票

自定义LSTMCell不支持GPU加速功能 - 这个陈述可能意味着如果您使用LSTMCell,GPU加速功能会受到限制。当然,您可以编写自己的LSTM实现,但需要牺牲运行时。

例如,一旦我实现了LSTM(基于线性层),如下所述,当用作深度神经模型的一部分时,曾经比LSTM(在PyTorch中提供)花费2~3倍的时间。

class LSTMCell(nn.Module):
    def __init__(self, input_size, hidden_size, nlayers, dropout):
        """"Constructor of the class"""
        super(LSTMCell, self).__init__()

        self.nlayers = nlayers
        self.dropout = nn.Dropout(p=dropout)

        ih, hh = [], []
        for i in range(nlayers):
            ih.append(nn.Linear(input_size, 4 * hidden_size))
            hh.append(nn.Linear(hidden_size, 4 * hidden_size))
        self.w_ih = nn.ModuleList(ih)
        self.w_hh = nn.ModuleList(hh)

    def forward(self, input, hidden):
        """"Defines the forward computation of the LSTMCell"""
        hy, cy = [], []
        for i in range(self.nlayers):
            hx, cx = hidden[0][i], hidden[1][i]
            gates = self.w_ih[i](input) + self.w_hh[i](hx)
            i_gate, f_gate, c_gate, o_gate = gates.chunk(4, 1)

            i_gate = F.sigmoid(i_gate)
            f_gate = F.sigmoid(f_gate)
            c_gate = F.tanh(c_gate)
            o_gate = F.sigmoid(o_gate)

            ncx = (f_gate * cx) + (i_gate * c_gate)
            nhx = o_gate * F.tanh(ncx)
            cy.append(ncx)
            hy.append(nhx)
            input = self.dropout(nhx)

        hy, cy = torch.stack(hy, 0), torch.stack(cy, 0)
        return hy, cy

我很高兴知道LSTM的自定义实现的运行时是否可以改进!

© www.soinside.com 2019 - 2024. All rights reserved.