RuntimeError: shape '[-1, 1, 28, 28]' is invalid for input of size 2752512

username: Sam3
cooked: <p>ّI have this error in my cnn… i don,t know why!</p>
<p>class CNN(nn.Module):<br>
def  <strong>init</strong>(self):<br>
super(CNN, self).<strong>init</strong>()<br>
<span class="hashtag">#mri</span> lf layer 1<br>
self.conv1 = nn.Sequential(  # input shape (1, 28, 28)<br>
nn.Conv2d(<br>
in_channels=1,      # input height<br>
out_channels=16,    # n_filters<br>
kernel_size=9, # filter size<br>
stride=1,           # filter movement/step<br>
padding=2           #<br>
))</p>
<pre><code>    self.batch1 = nn.BatchNorm2d(16),
           #output shape (16, 28, 28)
    self.relu1 =  nn.LeakyReLU(0.2, True)    #activation
    
    #mri hf layer 1 
    self.conv2 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 32, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
         ))
    
    self.batch2 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu2 = nn.LeakyReLU(0.2,True)
    
    self.conv3 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 64, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    self.batch3 = nn.BatchNorm2d(64),      #output shape (1,28,28)
    self.relu3 = nn.LeakyReLU(0.2,True)
    
    self.conv4 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 64,
            out_channels = 32, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch4 = nn.BatchNorm2d(32),      #output shape (1,28,28)
    self.relu4 = nn.LeakyReLU(0.2,True)
    
    self.conv5 = nn.Sequential(  # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 32,
            out_channels = 16, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
        ))
    
    self.batch5 = nn.BatchNorm2d(16),      #output shape (1,28,28)
    self.relu5 = nn.LeakyReLU(0.2,True)
    
    self.conv6 = nn.Sequential(      # input shape (16, 28, 28)
        nn.Conv2d(
            in_channels  = 16,
            out_channels = 1, 
            kernel_size  = 9,
            stride       = 1,
            padding      = 2
             ))
    
    self.batch6 = nn.BatchNorm2d(1),      #output shape (1,28,28)
    self.relu6 = nn.LeakyReLU(0.2,True)
    

def forward(self, x,y):
   
    
    x1 = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1= self.conv1(x)
    x1 = self.batch1(x)
    x1 = slef.relu1(x)
     #x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x1 = self.conv2(x)
    x1 = self.batch2(x)
    x1 = slef.relu2(x)
    x1 = self.conv3(x)
    x1 = self.batch3(x)
    x1 = slef.relu3(x)
    x1 = self.conv4(x)
    x1 = self.batch4(x)
    x1 = slef.relu4(x)
    x1 = self.conv5(x)
    x1 = self.batch5(x)
    x1 = slef.relu5(x)
    x1 = self.conv6(x)
    x1 = self.batch6(x)
    x1 = slef.relu6(x)
    
    x2 = y.reshape(-1,1,28,28) #careful: need to change by yourself
    x2= self.conv1(y)
    x2 = self.batch1(y)
    x2 = slef.relu1(y)
    # x = x.reshape(-1,1,28,28) #careful: need to change by yourself
    x2 = self.conv2(y)
    x2 = self.batch2(y)
    x2 = slef.relu2(y)
    x2 = self.conv3(y)
    x2 = self.batch3(y)
    x2 = slef.relu3(y)
    x2 = self.conv4(y)
    x2 = self.batch4(y)
    x2 = slef.relu4(y)
    x2 = self.conv5(y)
    x2 = self.batch5(y)
    x2 = slef.relu5(y)
    x2 = self.conv6(y)
    x2 = self.batch6(y)
    x2 = slef.relu6(y)
    
    x = torch.cat((x1, x2), 1)
    x = torch.tanh(x)
    x = x.reshape(-1)
    x = x.view(x.size(0), -1)
    #x = x.view(x.size(0), 1*28*28)                              #  (batch_size, 32 * 7 * 7)
    return x
   
    #execute the network
</code></pre>
<h1>reference:</h1>
<h1><a href="https://medium.com/unit8-machine-learning-publication/computing-the-jacobian-matrix-of-a-neural-network-in-python-4f162e5db180" rel="nofollow noopener">https://medium.com/unit8-machine-learning-publication/computing-the-jacobian-matrix-of-a-neural-network-in-python-4f162e5db180</a></h1>
<h1><a href="https://stackoverflow.com/questions/59161001/using-autograd-to-compute-jacobian-matrix-of-outputs-with-respect-to-inputs" rel="nofollow noopener">https://stackoverflow.com/questions/59161001/using-autograd-to-compute-jacobian-matrix-of-outputs-with-respect-to-inputs</a></h1>
<p>cnn = CNN()<br>
print(cnn)</p>
<p><span class="hashtag">#define</span> the optimizers and loss functions<br>
optimizer = torch.optim.Adam(cnn.parameters(), lr=LR)   # optimize all cnn parameters<br>
loss_func = SSIM()  # the target label is not one-hotted</p>
<h1>training</h1>
<p>for epoch in range(EPOCH):<br>
for step, (b_x , b_y) in enumerate(train_loader):</p>
<pre><code>   # b_x = b_x.reshape(-1)
    #b_y= b_y.reshape(-1)
    output = cnn(b_x,b_y)               # cnn output
    loss = loss_func(output, b_x)+ loss_func(output,b_y)#+ cross entropy loss
    optimizer.zero_grad()           # clear gradients for this training step
    loss.backward()                 # backpropagation, compute gradients
    optimizer.step()                # apply gradients</code></pre>