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

**URL:** <https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607>\
**Category:** Uncategorized\
**Created:** [December 6, 2021, 12:11am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607 "2021-12-06T00:11:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![fairy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fairy/32/36947_2.png) [@fairy](https://discuss.pytorch.org/u/fairy)\
**Post date:** [December 6, 2021, 12:11am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607/1 "2021-12-06T00:11:57Z")

</div>

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:

```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

```

```auto
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.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 6, 2021, 1:00am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607/2 "2021-12-06T01:00:21Z")

</div>

I cannot reproduce the issue using:

```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()

```

Which PyTorch version are you using?

---

<div class="post-metadata">

**Author:** ![fairy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fairy/32/36947_2.png) [@fairy](https://discuss.pytorch.org/u/fairy)\
**Post date:** [December 6, 2021, 1:11am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607/3 "2021-12-06T01:11:59Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [December 6, 2021, 1:25am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607/4 "2021-12-06T01:25:41Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![fairy](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/fairy/32/36947_2.png) [@fairy](https://discuss.pytorch.org/u/fairy)\
**Post date:** [December 6, 2021, 2:17am UTC](https://discuss.pytorch.org/t/pytorch-model-cuda-and-model-train-error/138607/5 "2021-12-06T02:17:29Z")

</div>

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.
