Replacing a decoder linear layer with an LSTM

Viewed 75

I am using this NET: https://www.kaggle.com/ceshine/pytorch-temporal-convolutional-networks

I would like to replace the decoder with an LSTM but I am getting an error.

Old:

class TCNModel(nn.Module):
    def __init__(self, num_channels, kernel_size=2, dropout,history):
        super(TCNModel, self).__init__()
        self.tcn = TemporalConvNet(
            history, num_channels, kernel_size=kernel_size, dropout=dropout)
        self.dropout = nn.Dropout(dropout)
        self.decoder = nn.Linear(num_channels[-1], 1)

     def forward(self, x):
        #print(np.shape( self.dropout(self.tcn(x)[:, :, -1])) )
        return self.decoder(self.dropout(self.tcn(x)[:, :, -1]))
  

New (dont work)

class TCNModel(nn.Module):
    def __init__(self, num_channels, kernel_size=2, dropout=0.02,history_len = 3):
        super(TCNModel, self).__init__()
        self.tcn = TemporalConvNet(
            history_len, num_channels, kernel_size=kernel_size, dropout=dropout)
        self.dropout = nn.Dropout(dropout)
        #self.decoder = nn.Linear(num_channels[-1], 1)
        self.hidden_dim = 2
        
        self.decoder = nn.LSTM(input_size=num_channels[-1],hidden_size=self.hidden_dim ,num_layers=2,bidirectional=False)
 
        #self.decoder = LLinear(num_channels[-1], 1)
        
    def forward(self, x):
        print(np.shape( self.dropout(self.tcn(x)[:, :, -1])) )
        #return self.decoder(self.dropout(self.tcn(x)[:, :, -1]))
        y = self.tcn(x)[:, :, -1]
        h0 = torch.zeros(2, y.size(0), self.hidden_dim).requires_grad_()
        # Initialize cell state
        c0 = torch.zeros(2, y.size(0), self.hidden_dim).requires_grad_()

        out, (hn, cn) = self.decoder(y, (h0.detach(), c0.detach()))
        
        return self.decoder(self.dropout(out) )
0 Answers
Related