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

fairy: <p>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:</p>
<pre><code class="lang-auto">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
</code></pre>
<pre><code class="lang-auto">net = SiameseNetwork()
net.cuda()
net.train()
</code></pre>
<p>And when I tried to called net.cuda() it give an error:</p>
<blockquote>
<p>7     # Declare Siamese Network<br>
8     net = SiameseNetwork()<br>
 → 9     net.cuda()<br>
10     <span class="hashtag-raw">#device</span> = torch.device(“cuda:0” if torch.cuda.is_available() else “cpu”)<br>
11     <span class="hashtag-raw">#net</span> = net.to(device)<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in cuda(self, device)<br>
678             Module: self<br>
679         “”"<br>
 → 680         return self._apply(lambda t: t.cuda(device))<br>
681<br>
682     def xpu(self: T, device: Optional[Union[int, device]] = None) → T:<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in _apply(self, fn)<br>
567<br>
568     def _apply(self, fn):<br>
 → 569         for module in self.children():<br>
570             module._apply(fn)<br>
571<br>
TypeError: children() missing 1 required positional argument: ‘node’</p>
</blockquote>
<p>while if I called net.train():</p>
<blockquote>
<p> → 2     net.train()<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/module.py in train(self, mode)<br>
1714             raise ValueError(“training mode is expected to be boolean”)<br>
1715         self.training = mode<br>
 → 1716         for module in self.children():<br>
1717             module.train(mode)<br>
1718         return self<br>
TypeError: children() missing 1 required positional argument: ‘node’</p>
</blockquote>
<p>Does anyone know how to solved this problem? Thank you.</p>

ptrblck: <p>I cannot reproduce the issue using:</p>
<pre><code class="lang-python">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()
</code></pre>
<p>Which PyTorch version are you using?</p>

fairy: <p>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.</p>

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

fairy: <p>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.</p>