RuntimeError: Expected 4-dimensional input for 4-dimensional weight [500, 1, 5, 5], but got 3-dimensional input of size [1, 250, 250] instead

Rakuen: <p>I saw these kind of topics a lot in forum but generally batch dimension is missing in their tensors. But in my case channel dimension is missing and I don’t know how to put it. Here is my code</p>
<p>class MyMatDataset(Dataset):</p>
<pre><code>def __init__(self, distFunc_paths, flowfield_paths):
    self.distFunc_paths = distFunc_paths
    self.flowfield_paths = flowfield_paths
                
def __getitem__(self, index):
    x = scipy.io.loadmat(self.distFunc_paths[index])
    x = torch.from_numpy(x['MD'])
    y = scipy.io.loadmat(self.flowfield_paths[index])
    y = torch.from_numpy(y['z1'])
    return x, y

def __len__(self):
    return len(self.distFunc_paths)
</code></pre>
<p>root = “./Data/”<br>
distFunc_paths = []<br>
flowfield_paths = []<br>
for r, d, f in os.walk(root):<br>
for files in f:<br>
if “_distFunc.mat” in files:<br>
distFunc_paths.append(osp.join(r, files))<br>
if “_flowfield.mat” in files:<br>
flowfield_paths.append(osp.join(r,files))</p>
<p>dataset = MyMatDataset(distFunc_paths = distFunc_paths, flowfield_paths = flowfield_paths)</p>
<p>train_loader = torch.utils.data.DataLoader(<br>
dataset, batch_size=1, shuffle=True<br>
)</p>
<p>class CFD_CNN(nn.Module):<br>
def <strong>init</strong>(self, out_ch=500):<br>
super(CFD_CNN, self).<strong>init</strong>()<br>
self.encoder = nn.Sequential(<br>
nn.Conv2d(1, out_ch, kernel_size=5, stride=5, padding=0),<br>
nn.ReLU(True),<br>
nn.Conv2d(out_ch, out_ch, kernel_size=5, stride=5, padding=0),<br>
nn.ReLU(True),<br>
nn.Conv2d(out_ch, out_ch, kernel_size=2, stride=2, padding=0),<br>
nn.ReLU(True),<br>
)</p>
<h1></h1>
<pre><code>    self.decoder = nn.Sequential(
            nn.ConvTranspose2d(out_ch, out_ch, kernel_size=2, stride=2, padding=0),
            nn.ReLU(True),
            nn.ConvTranspose2d(out_ch, out_ch, kernel_size=5, stride=5, padding=0),
            nn.ReLU(True),
            nn.ConvTranspose2d(out_ch, 1, kernel_size=5, stride=5, padding=0),
            nn.ReLU(True)
    )
    
def forward(self, x):
    x = self.encoder(x)
    print(x.size())
    print(x.size())
    x = self.decoder(x)
    print(x.size())
    return x
</code></pre>
<p>net = CFD_CNN()</p>
<p>criterion = nn.CrossEntropyLoss()<br>
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)</p>
<p>for epoch in range(2):</p>
<pre><code>running_loss = 0.0
for i, data in enumerate(train_loader,0):
    
    distfunc, flowfields = data
    
    optimizer.zero_grad()

    outputs = net(distfunc)
    
    loss = criterion(outputs, flowfields)
    
    loss.backward()
    
    optimizer.step()
    
    running_loss += loss.item()
</code></pre>
<p>print(“Finished Training”)</p>
<p>Thanks in advance.</p>

phan_phan: <p>Your first convolution has <code>in_channels=1</code>,<br>
so the tensor given to the model should be of shape <code>[batch_size, 1, height, width]</code>.<br>
It seems that it receives instead a tensor of shape <code>[1, height, width]</code>.</p>
<p>If the <code>1</code> already represents your <code>batch_size=1</code>, then you need to add another <em>channel</em> dimension before the height (so at position 1):</p>
<pre><code class="lang-auto">outputs = net(distfunc.unsqueeze(dim=1))
</code></pre>

Rakuen: <p>Thanks but I already solved it. I have another question. Why are my tensors have extra brackets like below one? Am i doing something wrong or it is ok? I am new in pytorch and still learning as you can see. Thanks.</p>
<p>In [35]: distfunc<br>
Out[35]:<br>
tensor([[[[0.7022, 0.6965, 0.6908,  …, 0.6956, 0.7012, 0.7069],<br>
[0.6995, 0.6937, 0.6879,  …, 0.6927, 0.6984, 0.7040],<br>
[0.6967, 0.6909, 0.6851,  …, 0.6899, 0.6955, 0.7012],<br>
…,<br>
[0.6967, 0.6909, 0.6851,  …, 0.6899, 0.6955, 0.7012],<br>
[0.6995, 0.6937, 0.6879,  …, 0.6927, 0.6984, 0.7040],<br>
[0.7022, 0.6965, 0.6908,  …, 0.6956, 0.7012, 0.7069]]]],<br>
dtype=torch.float64)</p>

phan_phan: <p>Extra brackets means extra dimensions.<br>
A tensor with 1 dimension will be printed with 1 opening bracket, kind of like a <em>list</em> .<br>
A tensor with 2 dimensions will be printed with 2 opening brackets, kind of like a <em>list of lists</em> .<br>
A tensor with 4 dimensions will be printed with 4 opening brackets, kind of like a <em>list of lists of lists of lists</em> .</p>
<p>Like in the movie Inception they go 4 levels deep, so we see them start the dream machine 4 times.</p>

Hayat_Ullah: <p>Simply add batch_size dimension to your input, because it is essential in PyTorch to provide batch_size information even you have single image.<br>
For instance: your input is ==&gt; (3, 227, 227) you can easily add extra dimension for batch_size as follow:<br>
inputs = inputs.unsqueeze(0)<br>
In this case the shape of your input will be: (1, 3, 227, 227).</p>