# 'network' object has no attribute '\_modules'

**URL:** <https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123>\
**Category:** Uncategorized\
**Created:** [May 13, 2019, 3:42pm UTC](https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123 "2019-05-13T15:42:29Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ATHARVA\_BADVE](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/atharva_badve/32/10961_2.png) [@ATHARVA\_BADVE](https://discuss.pytorch.org/u/ATHARVA_BADVE)\
**Post date:** [May 13, 2019, 3:42pm UTC](https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123/1 "2019-05-13T15:42:29Z")

</div>

I am trying to transfer a model to gpu  
But I am getting error as `'colorizer' object has no attribute '_modules'`  
My model is

```auto
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
print (device)

class network(torch.nn.Module):
  def __init__ (self):
    upscale = nn.Upsample(scale_factor=2, mode='bilinear')
    conv1 = nn.Conv2d(963, 128, kernel_size=3, padding=1)
    conv2 = nn.Conv2d(128,64, kernel_size=3, padding=1)
    conv3 = nn.Conv2d(64, 2, kernel_size=3, padding=1)
  
  def forward(self, inputs):
    hyperclm = inputs[3]
    hyperclm = torch.cat((inputs[2], upscale(hyperclm)),1)
    hyperclm = torch.cat((inputs[1], upscale(hyperclm)),1)
    hyperclm = torch.cat((inputs[0], upscale(hyperclm)),1)
    hyperclm = torch.cat((inputs[3], hyperclm),1)
    hyperclm = conv1(hyperclm)
    hyperclm = conv2(hyperclm)
    hyperclm = conv3(hyperclm)
    return hyperclm
  
net = network()
net.to(device)

```

Complete error trace

```auto
cuda:0
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-39-a923d94fe148> in <module>()
     21 
     22 net = network()
---> 23 net.to(device)

4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in to(self, *args, **kwargs)
    384 return t.to(device, dtype if t.is_floating_point() else None, non_blocking)
    385 
--> 386 return self._apply(convert)
    387 
    388 def register_backward_hook(self, hook):

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in _apply(self, fn)
    190 
    191 def _apply(self, fn):
--> 192 for module in self.children():
    193 module._apply(fn)
    194 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in children(self)
    895 Module: a child module
    896 """
--> 897 for name, module in self.named_children():
    898 yield module
    899 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in named_children(self)
    913 """
    914 memo = set()
--> 915 for name, module in self._modules.items():
    916 if module is not None and module not in memo:
    917 memo.add(module)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __getattr__ (self, name)
    537 return modules[name]
    538 raise AttributeError("'{}' object has no attribute '{}'".format(
--> 539 type(self). __name__ , name))
    540 
    541 def __setattr__ (self, name, value):

AttributeError: 'network' object has no attribute '_modules'

```

Is there anything wrong in Model definition?

Thank You?

---

<div class="post-metadata">

**Author:** ![ATHARVA\_BADVE](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/atharva_badve/32/10961_2.png) [@ATHARVA\_BADVE](https://discuss.pytorch.org/u/ATHARVA_BADVE)\
**Post date:** [May 13, 2019, 3:46pm UTC](https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123/2 "2019-05-13T15:46:26Z")

</div>

I forgot to mention, I am using Google Colab.

---

<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:** [May 13, 2019, 4:12pm UTC](https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123/3 "2019-05-13T16:12:14Z")

</div>

You forgot to register the modules using `self`:

```python

class network(torch.nn.Module):
  def __init__ (self):
    self.upscale = nn.Upsample(scale_factor=2, mode='bilinear')
    self.conv1 = nn.Conv2d(963, 128, kernel_size=3, padding=1)
    self.conv2 = nn.Conv2d(128,64, kernel_size=3, padding=1)
    self.conv3 = nn.Conv2d(64, 2, kernel_size=3, padding=1)

```

---

<div class="post-metadata">

**Author:** ![ATHARVA\_BADVE](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/atharva_badve/32/10961_2.png) [@ATHARVA\_BADVE](https://discuss.pytorch.org/u/ATHARVA_BADVE)\
**Post date:** [May 13, 2019, 4:25pm UTC](https://discuss.pytorch.org/t/network-object-has-no-attribute-modules/45123/4 "2019-05-13T16:25:12Z")

</div>

Thanks!!!  
I didn’t noticed that
