AttributeError: module 'torch.nn' has no attribute 'Conv2D'

username: chrishki
cooked: <p>I am getting the following error while trying to use <code>Conv2D</code>  from  <code>torch.nn</code>:<br>
<code>AttributeError: module 'torch.nn' has no attribute 'Conv2D'</code></p>
<p>I am wondering why it is happening?</p>
<p>This is the code that I’m running.</p>
<pre><code class="lang-python">model = pretrainedmodels.__dict__['squeezenet1_1'](num_classes=1000, pretrained='imagenet')
model.last_conv = nn.Conv2D(512, 3, 1, 1)
</code></pre>
<p>This is my environment information:<br>
PyTorch version: 1.5.1<br>
Is debug build: No<br>
CUDA used to build PyTorch: 10.2</p>
<p>OS: Ubuntu 16.04.6 LTS<br>
GCC version: (Ubuntu 5.4.0-6ubuntu1~16.04.12) 5.4.0 20160609<br>
CMake version: version 3.5.1</p>
<p>Python version: 3.7<br>
Is CUDA available: Yes<br>
CUDA runtime version: 10.0.130<br>
GPU models and configuration: Could not collect<br>
Nvidia driver version: Could not collect<br>
cuDNN version: /usr/lib/x86_64-linux-gnu/libcudnn.so.7.6.5</p>
<p>Versions of relevant libraries:<br>
[pip3] numpy==1.19.0<br>
[pip3] torch==1.5.1<br>
[pip3] torchvision==0.6.1</p>

username: Nikronic
cooked: <p>Hi,<br>
PyTorch uses lower case for dimension in layers.</p>
<aside class="quote no-group" data-username="chrishki" data-post="1" data-topic="90038">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/chrishki/48/28637_2.png" class="avatar"> chrishki:</div>
<blockquote>
<p><code>nn.Conv2D</code></p>
</blockquote>
</aside>
<p>Use <code>nn.Conv2d</code> instead.<br>
<a href="https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html</a></p>
<p>Bests</p>

username: swapnil_kore
cooked: <p>Hey, I am getting this error and I don’t know what to do could anyone resolve it ?</p>
<p>AttributeError: module ‘torch.nn’ has no attribute ‘module’</p>
<p>This is the code for which I am facing the above error</p>
<p>import torch<br>
import torch.nn as nn<br>
import torch.nn.functional as F</p>
<p>class Net(nn.module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.con1 = nn.Conv2d(1, 32, 5)  # here 1=input, 32=output i.e. 32 conv-features, 5=kernel size<br>
self.con2 = nn.Conv2d(32, 64, 5)<br>
self.con3 = nn.Conv2d(64, 128, 5)</p>
<pre><code>    x = torch.randn(50,50).view(-1,1,50,50)
    self._to_linear = None
    self.convs(x)
    
    self.fc1 = nn.Linear(self._to_linear, 512)
    self.fc2 = nn.Linear(512, 2)
def convs(self, x):
    x = F.max_pool2d(F.relu(self.conv1(x)),  (2,2))
    x = F.max_pool2d(F.relu(self.conv2(x)),  (2,2))
    x = F.max_pool2d(F.relu(self.conv3(x)),  (2,2))
    
    print(x[0].shape)
    
    if self._to_linear is None:
        self._to_linear = x[0].shape*x[0].shape[1]*x[0].shape[2]
    return x

def forward(self, x):
    x = self.convs(x)
    x = x.view(-1,self._to_linear)
    x = F.relu(self.fc1(x))
    x = self.fc2(x)
    return F.softmax(x, dim=1)
</code></pre>
<p>net = Net()</p>

username: ptrblck
cooked: <p>You would have to use the uppercase <code>M</code> in <code>nn.Module</code>.</p>