'network' object has no attribute '_modules'

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

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

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?

I forgot to mention, I am using Google Colab.

You forgot to register the modules using self:


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)
3 Likes

Thanks!!!
I didn’t noticed that