Pytorch model.cuda() and model.train() error

Hi, I tried to create a siamese model and encounter an error every time I tried to connect the model to cuda or called model.train(). Here some code snippet of the model forward:

class SiameseNetwork(nn.Module):
    def __init__(self):
        super(SiameseNetwork, self).__init__()
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.dropout = 0.17
        self.n_units = 100
        self.n_label = 2
        self.w = nn.Linear(self.n_units,self.n_label).to(self.device)
        self.w_v = nn.Linear(2*self.n_units,self.n_units).to(self.device)
        self.lstm = nn.LSTM(input_size = self.n_units, hidden_size=self.n_units).to(self.device)
        self.blstm = nn.LSTM(input_size = self.n_units, hidden_size=self.n_units).to(self.device)
  
    def forward(self, object1, object2):
      output1 = self._traverse_obj(object1)
      output2 = self._traverse_obj(object2)
         
      return output1, output2
net = SiameseNetwork()
net.cuda()
net.train()

And when I tried to called net.cuda() it give an error:

7 # Declare Siamese Network
8 net = SiameseNetwork()
→ 9 net.cuda()
10 #device = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)
11 #net = net.to(device)
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in cuda(self, device)
678 Module: self
679 “”"
→ 680 return self._apply(lambda t: t.cuda(device))
681
682 def xpu(self: T, device: Optional[Union[int, device]] = None) → T:
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
567
568 def _apply(self, fn):
→ 569 for module in self.children():
570 module._apply(fn)
571
TypeError: children() missing 1 required positional argument: ‘node’

while if I called net.train():

→ 2 net.train()
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in train(self, mode)
1714 raise ValueError(“training mode is expected to be boolean”)
1715 self.training = mode
→ 1716 for module in self.children():
1717 module.train(mode)
1718 return self
TypeError: children() missing 1 required positional argument: ‘node’

Does anyone know how to solved this problem? Thank you.

I cannot reproduce the issue using:

class SiameseNetwork(nn.Module):
    def __init__(self):
        super(SiameseNetwork, self).__init__()
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        self.dropout = 0.17
        self.n_units = 100
        self.n_label = 2
        self.w = nn.Linear(self.n_units,self.n_label).to(self.device)
        self.w_v = nn.Linear(2*self.n_units,self.n_units).to(self.device)
        self.lstm = nn.LSTM(input_size = self.n_units, hidden_size=self.n_units).to(self.device)
        self.blstm = nn.LSTM(input_size = self.n_units, hidden_size=self.n_units).to(self.device)
  
    def forward(self, object1, object2):
      output1 = self._traverse_obj(object1)
      output2 = self._traverse_obj(object2)
         
      return output1, output2


model = SiameseNetwork()
model.cuda()

Which PyTorch version are you using?

I use Python version : 3.7 and Torch version : 1.10.0+cu111. The reason I used .to(self.device) in model init because it’s the only way I can use GPU in the model (because I can’t use model.cuda() ) and self._traverse_obj is a recursive method for my research.

I still cannot reproduce the error after removing the to(self.device) usage in 1.10.0 so feel free to post an executable code snippet, which would raise the error.

I solved the problem. It was because I had a method called def children in my model. I changed the method name and the problem solved. Thank you for your help.

1 Like