'network' object has no attribute '_modules'

ATHARVA_BADVE: <p>I am trying to transfer a model to gpu<br>
But I am getting error as <code>'colorizer' object has no attribute '_modules'</code><br>
My model is</p>
<pre><code class="lang-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)
</code></pre>
<p>Complete error trace</p>
<pre><code class="lang-auto">cuda:0
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
&lt;ipython-input-39-a923d94fe148&gt; in &lt;module&gt;()
     21 
     22 net = network()
---&gt; 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 
--&gt; 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):
--&gt; 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         """
--&gt; 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()
--&gt; 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(
--&gt; 539             type(self).__name__, name))
    540 
    541     def __setattr__(self, name, value):

AttributeError: 'network' object has no attribute '_modules'
</code></pre>
<p>Is there anything wrong in Model definition?</p>
<p>Thank You?</p>

ATHARVA_BADVE: <p>I forgot to mention, I am using Google Colab.</p>

ptrblck: <p>You forgot to register the modules using <code>self</code>:</p>
<pre><code class="lang-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)
</code></pre>

ATHARVA_BADVE: <p>Thanks!!!<br>
I didn’t noticed that</p>