Getting an error in Activation function "TypeError: 'Tensor' object is not callable"

Viewed 72

I am trying to use the relu activation function in pytorch LSTM but getting the error on " Tensor object is not callable. any guideline or help? Can i use the different activation function in forward propogation? but i am using the same activation function in hidden layers and forward propogation. your kind review will be more help full.

class LSTM(nn.Module):
  def __init__(self, input_size=1, hidden_layer_size=20, output_size=1):
    super().__init__()

    self.hidden_layer_size = hidden_layer_size
    self.lstm = nn.LSTM(input_size, hidden_layer_size)
    self.relu = nn.functional.relu(torch.FloatTensor(hidden_layer_size), torch.FloatTensor(output_size))     
    self.hidden_cell = (torch.zeros(1, 1, self.hidden_layer_size),    
                        torch.zeros(1, 1, self.hidden_layer_size))
     def forward(self, input_seq):
    lstm_out, self.hidden_cell = self.lstm(input_seq.view(len(input_seq), 1, -1), self.hidden_cell)
    predictions = self.relu(lstm_out.view(len(input_seq), -1))
    return predictions[-1]

    model = LSTM()
    loss_function = nn.MSELoss()
    optimizer = torch.optim.Adam(model.parameters(), lr = 0.0001)

    epochs = 150

     for i in range(epochs):
       for seq, labels in train_inout_seq:
        optimizer.zero_grad()
        model.hidden_cell = (torch.zeros(1, 1, model.hidden_layer_size),
                            torch.zeros(1, 1, model.hidden_layer_size))    
       y_pred = model(seq)

    single_loss = loss_function(y_pred, labels)
    single_loss.backward()
    optimizer.step()

     if i%25 == 1:
    print(f'epoch: {i:3} loss: {single_loss.item():10.8f}')

    print(f'epoch: {i:3} loss: {single_loss.item():10.10f}')

After that i am getting an error:`

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-108-5fcfb471ed9a> in <module>
      6     model.hidden_cell = (torch.zeros(1, 1, model.hidden_layer_size),
      7                          torch.zeros(1, 1, model.hidden_layer_size))    
----> 8     y_pred = model(seq)
      9 
     10     single_loss = loss_function(y_pred, labels)

/opt/conda/lib/python3.8/site-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--> 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

<ipython-input-105-221892d3f487> in forward(self, input_seq)
     12   def forward(self, input_seq):
     13     lstm_out, self.hidden_cell = self.lstm(input_seq.view(len(input_seq), 1, -1), self.hidden_cell)
---> 14     predictions = self.relu(lstm_out.view(len(input_seq), -1))
     15     return predictions[-1]

TypeError: 'Tensor' object is not callable
1 Answers
self.relu = nn.functional.relu(torch.FloatTensor(hidden_layer_size), torch.FloatTensor(output_size))     

This line doesn't really define a ReLU function for further use, but instead it applies the ReLU function to an arbitrary tensor (which is torch.FloatTensor(hidden_layer_size)) and returns the resultant tensor! So self.relu is not a function but a tensor, hence the error.

A remedy is to use the following instead of the line above:

self.relu = nn.ReLU()

This gives an nn.ReLU instance that you can use as a ReLU function as you are doing in the forward method by calling it.

Another solution would be not define a self.relu at all, and use this line in the forward:

    predictions = nn.functional.relu(lstm_out.view(len(input_seq), -1))

This is a difference between the modular and the functional approach.

See here for some reason(s) as to why prefer one over another: https://discuss.pytorch.org/t/whats-the-difference-between-nn-relu-vs-f-relu/27599

Related