RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x1065360 and 12544x30)

Yasar_Niyazoglu: <pre><code class="lang-auto">dataset = veri(
    csv_file="f111.csv",
    root_dir=r"C:\Users\yasar\Desktop\f1_classification",
    transform=torchvision.transforms.Compose([transforms.ToTensor(),
                                              transforms.Resize(size=(28*28)),
                                             transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]))```

</code></pre>
<p>train_set, test_set = torch.utils.data.random_split(dataset, [200, 79])<br>
train_loader = DataLoader(dataset=train_set, batch_size=1, shuffle=True)<br>
test_loader = DataLoader(dataset=test_set, batch_size=1, shuffle=True)```</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=4,kernel_size=(5,5))
        self.conv2=nn.Conv2d(in_channels=4,out_channels=8,kernel_size=(3,3))
        self.conv3=nn.Conv2d(in_channels=8,out_channels=16,kernel_size=(2,2))
        #self.conv4=nn.Conv2d(in_channels=32,out_channels=64,kernel_size=(3,3))
        #self.conv5=nn.Conv2d(in_channels=64,out_channels=128,kernel_size=(5,5))

        self.max=nn.MaxPool2d(kernel_size=(2,2))
        self.func=nn.ReLU()
        self.out=nn.Sigmoid()
        
        self.fc1=nn.Linear(in_features=16*28*28,out_features=30)
        self.fc2=nn.Linear(in_features=30,out_features=30)
        self.fc3=nn.Linear(in_features=30,out_features=4)
        #self.fc4=nn.Linear(in_features=200,out_features=200)
        #self.fc5=nn.Linear(in_features=200,out_features=200)
        #self.fc6=nn.Linear(in_features=200,out_features=4)
        
        
    def forward(self,x):
        
        x=self.conv1(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv2(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv3(x)
        x=self.func(x)

        #x=self.max(x)
        
        #x=self.conv4(x)
        #x=self.func(x)

        #x = x.view(x.size(0),16*4*4) 
        x=x.view(x.size(0), -1)
        x=self.fc1(x)
        x=self.func(x)
        x=self.fc2(x)
        x=self.func(x)
        x=self.fc3(x)
        #x=self.fc4(x)
        #x=self.fc5(x)
        #x=self.fc6(x)
        
        return x        ```

</code></pre>
<p>device=torch.device(“cuda”)<br>
model=Net()</p>
<p>optimizer=torch.optim.Adam(model.parameters(),lr=0.01)<br>
error=torch.nn.CrossEntropyLoss()</p>
<p>epoch=1</p>
<p>for i in range(epoch):<br>
for i,(images,label) in enumerate(train_loader):</p>
<pre><code>    #images=images.to(device)
    #label=label.to(device)
    
    out=model(images)
    
    optimizer.zero_grad()
    
    loss=error(out,label)
    
    loss.backward()
    
    optimizer.step()    ```
</code></pre>
<p>can you help me please</p>

ptrblck: <p>This error is raised in <code>self.fc1</code>, which expects <code>16*28*28=12544</code> input features, while the input activation <code>x</code> is flattened to <code>[1, 1065360]</code> in <code>x=x.view(x.size(0), -1)</code>.<br>
You could either change the <code>in_feautres</code> of <code>self.fc1</code> to <code>1065360</code> or make sure the input tensor has such a spatial size that the flattened activation has <code>12544</code> features.</p>

Yasar_Niyazoglu: <p>thank you. how can I do that<br>
This is how I did it and it worked. but i am not sure of its accuracy. because I entered a value like 1065360 in the fc1 layer</p>
<pre><code class="lang-auto">class Net(nn.Module):
    def __init__(self):
        super(Net,self).__init__()
        
        self.conv1=nn.Conv2d(in_channels=3,out_channels=4,kernel_size=(5,5))
        self.conv2=nn.Conv2d(in_channels=4,out_channels=8,kernel_size=(3,3))
        self.conv3=nn.Conv2d(in_channels=8,out_channels=16,kernel_size=(2,2))

        self.max=nn.MaxPool2d(kernel_size=(2,2))
        self.func=nn.ReLU()
        
        self.fc1=nn.Linear(in_features=1065360,out_features=30)
        self.fc2=nn.Linear(in_features=30,out_features=30)
        self.fc3=nn.Linear(in_features=30,out_features=4)
        
    def forward(self,x):
        
        x=self.conv1(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv2(x)
        x=self.func(x)
        
        x=self.max(x)
        
        x=self.conv3(x)
        x=self.func(x)


        x = x.view(x.size(0),1065360) 
        
        x=self.fc1(x)
        x=self.func(x)
        x=self.fc2(x)
        x=self.func(x)
        x=self.fc3(x)
        return x    ```</code></pre>

ptrblck: <p>Your change reflects my first suggestion of changing the <code>in_features</code> of <code>self.fc1</code> to <code>1065360</code> and thus works.<br>
I’m unsure what “but i am not sure of its accuracy” means. Are you concerned you’ve changed the wrong value or is accuracy related to the model accuracy?</p>

Yasar_Niyazoglu: <p>I’m not sure of the model accuracy. When I changed fc1 it worked and this table. if there is no problem with model accuracy. I will try methods such as data augmentation - optimization<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/c/0c68c0d0db1062a148e378972e683d2acd74b631.png" data-download-href="https://discuss.pytorch.org/uploads/default/0c68c0d0db1062a148e378972e683d2acd74b631" title="los"><img src="https://discuss.pytorch.org/uploads/default/original/3X/0/c/0c68c0d0db1062a148e378972e683d2acd74b631.png" alt="los" data-base62-sha1="1LMa3VMEdFGTyL4FXcs1RCNjiPn" width="690" height="497" data-dominant-color="E2E2E2"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">los</span><span class="informations">988×713 49.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>