It's a very simple story, but it's a memorandum because it seems to melt for a long time someday.
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, p_n_features_num, timesteps) -> None:
self.p_n_features_num = p_n_features_num
self.linear = nn.Linear(
p_n_features_num, p_n_features_num, bias=True)
self.leakyrelu = nn.LeakyReLU()
def forward(self, input_net):
input_net = input_net.view(input_net.size(0), self.p_n_features_num)
return self.leakyrelu(self.linear(input_net))
If you call \ _ \ _ init \ _ \ _ of Encoder here
cannot assign module before Module.init() call
```I get the error.
## Solution, fix code
This is because the super method was not called first because it inherited nn.Module at the time of init. Therefore, it can be fixed by modifying it as follows.
```python
import torch
import torch.nn as nn
class Encoder(nn.Module):
def __init__(self, p_n_features_num, timesteps) -> None:
super(Encoder, self).__init__()
self.p_n_features_num = p_n_features_num
self.linear = nn.Linear(
p_n_features_num, p_n_features_num, bias=True)
self.leakyrelu = nn.LeakyReLU()
def forward(self, input_net):
input_net = input_net.view(input_net.size(0), self.p_n_features_num)
return self.leakyrelu(self.linear(input_net))
Recommended Posts