ValueError: Expected input batch_size (324) to match target batch_size (4)

Hi @ptrblck ! I have a similar issue but I couldn’t fix it based on the answers above. Could you help me?

model.train()

loss_per_10_steps=[]
for epoch in range(1,num_of_epochs+1):
  print('Running epoch: {}'.format(epoch))
  
  running_loss=0

  out = display(progress(1, num_of_batches+1), display_id=True)
  for i in range(num_of_batches):
    inputbatch=[]
    labelbatch=[]
    new_df=train_df[i*batch_size:i*batch_size+batch_size]
    for indx,row in new_df.iterrows():
      input = 'DART: '+row['input_text']+'</s>' 
      labels = row['target_text']+'</s>'   
      inputbatch.append(input)
      labelbatch.append(labels)
    
    inputbatch=tokenizer.batch_encode_plus(inputbatch,padding=True,max_length=400,return_tensors='pt')["input_ids"]
    labelbatch=tokenizer.batch_encode_plus(labelbatch,padding=True,max_length=400,return_tensors="pt") ["input_ids"]
    inputbatch=inputbatch.to(dev)
    labelbatch=labelbatch.to(dev)


    # clear out the gradients of all Variables 
    optimizer.zero_grad()

    # Forward propogation
    outputs = model(input_ids=inputbatch, labels=labelbatch)
    loss = outputs.loss
    loss_num=loss.item()
    logits = outputs.logits
    running_loss+=loss_num
    if i%10 ==0:      
      loss_per_10_steps.append(loss_num)
    out.update(progress(loss_num,i, num_of_batches+1))

    # calculating the gradients
    loss.backward()

    #updating the params
    optimizer.step()
    
  running_loss=running_loss/int(num_of_batches)
  print('Epoch: {} , Running loss: {}'.format(epoch,running_loss))
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-fe1d555f5070> in <module>()
     29 
     30     # Forward propogation
---> 31     outputs = model(input_ids=inputbatch, labels=labelbatch)
     32     loss = outputs.loss
     33     loss_num=loss.item()

4 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
   2844     if size_average is not None or reduce is not None:
   2845         reduction = _Reduction.legacy_get_string(size_average, reduce)
-> 2846     return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
   2847 
   2848 

ValueError: Expected input batch_size (560) to match target batch_size (264).

It’s unclear from your code snippet with operation causes the shape mismatch.
Based on the error message the model output has a batch size of 560 while the target uses 264. Which batch size are you expecting (I guess 264)? Check the model’s forward method and make sure you are keeping the batch size equal without flattening it.

Thanks a lot for your answers! I am currently trying to write a custom dataset in PyTorch but am somehow running into similar errors I met when using h5py like “not enough values to unpack (expected 2, got 1)”.

That’s why I am now also trying to use the model with another dataset made of png instead. It is going pretty well as I don’t run into errors like not enough values to unpack, needing to permute or even transforming into float to make gradient possible. However I run again into this error of batch size. And if I fix it by replacing out = out.view(-1, self.in_planes) by x.view(x.size(0), -1) I run into an error " mat1 and mat2 shapes cannot be multiplied (1x1368 and 342x10)". I think this is linked to in_planes which does not take the right value. I am using Densenet3 and I see the value in_planes change according to growth rate and then be used in the forward function as follows:

class DenseNet3(nn.Module):
def init(self, depth, num_classes, growth_rate=12,
reduction=0.5, bottleneck=True, dropRate=0.0):
super(DenseNet3, self).init()
in_planes = 2 * growth_rate
n = (depth - 4) / 3
if bottleneck == True:
n = n/2
block = BottleneckBlock
else:
block = BasicBlock
# 1st conv before any dense block
self.conv1 = nn.Conv2d(3, in_planes, kernel_size=3, stride=1,
padding=1, bias=False)
# 1st block
self.block1 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
in_planes = int(in_planes+ngrowth_rate)
self.trans1 = TransitionBlock(in_planes, int(math.floor(in_planes
reduction)), dropRate=dropRate)
in_planes = int(math.floor(in_planesreduction))
# 2nd block
self.block2 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
in_planes = int(in_planes+n
growth_rate)
self.trans2 = TransitionBlock(in_planes, int(math.floor(in_planesreduction)), dropRate=dropRate)
in_planes = int(math.floor(in_planes
reduction))
# 3rd block
self.block3 = DenseBlock(n, in_planes, growth_rate, block, dropRate)
in_planes = int(in_planes+n*growth_rate)
# global average pooling and classifier
self.bn1 = nn.BatchNorm2d(in_planes)
self.relu = nn.ReLU(inplace=True)
self.fc = nn.Linear(in_planes, num_classes)
self.in_planes = in_planes

def forward(self, x):
    out = self.conv1(x)
    out = self.trans1(self.block1(out))
    out = self.trans2(self.block2(out))
    out = self.block3(out)
    out = self.relu(self.bn1(out))
    out = F.avg_pool2d(out, 8)
    out = out.view(-1, self.in_planes)
    return self.fc(out) 

However it is not really clear to me what is happening exactly and how I could (or even if I should) change in_planes to solve batch size and mat shape issues. I am also wondering why this works perfectly fine when using datasets like Imagenet but not when I change it myself. I suspect this could be linked to how my dataset is loaded as I am using:

testsetout = torchvision.datasets.ImageFolder("…/data/{}".format(dataName), transform=transform)
testloaderOut = torch.utils.data.DataLoader(testsetout, batch_size=1, shuffle=False, num_workers=2)

maybe something wrong is done with the batch_size?

Replacing out = out.view(-1, self.in_planes) with out = out.view(out.size(0), -1) is the right approach, as it would keep the batch size equal. I don’t think the batch size is wrong, but would guess that your input images do not have the same shape as e.g. the standard ImageNet samples, which are usually resized to 224x224. You could thus also use transforms.Resize and make sure the inputs have the same spatial size as before.

Thank you so much! I used transforms.Resize(32) in my transform function and it now seems to be working with the dataset of pngs. However since I am not using the transform function to load my hdf5 dataset, is there an equivalent to resize it? When I print the shape of each tensor supposed to represent my images it gives out: torch.Size([1, 96, 96, 3]) should I then try to resize this tensor or rather the whole dataset through the intermediary array for instance before it is loaded onto pytorch?

You could transform the tensors after creating them from the hdf5 dataset, as torchvision.transforms also accept tensors in newer versions.
Resizing the entire dataset might also be a valid approach in case you don’t want to use the larger files anymore in future use cases.

Thank you for all your answers, I actually found a GitHub converting my hdf5 dataset to png and now everything works! I’m very grateful for the help, thanks again!!

Hello Mr. @ptrblck, I have same problem,
This is my program
import torch
import numpy as np
import torch.nn as nn
import torch.utils.data as Data
import matplotlib.pyplot as plt
import sklearn.metrics as sm
from torchstat import stat
torch.cuda.set_device(1)
n_gpu = torch.cuda.device_count()
print(n_gpu)

train_x = np.load(‘Thermal_data/np/np_thermal_3224_train_x.npy’)
train_y = np.load(‘Thermal_data/np/np_thermal_3224_train_y.npy’)
test_x = np.load(‘Thermal_data/np/np_thermal_3224_test_x.npy’)
test_y = np.load(‘Thermal_data/np/np_thermal_3224_test_y.npy’)

print("\nShape of train_x:",train_x.shape,
“\nShape of train_y:”,train_y.shape,
“\nShape of test_x:”,test_x.shape,
“\nShape of test_y:”,test_y.shape,)

train_x = np.reshape(train_x, [-1, 1, 768, 1])
test_x = np.reshape(test_x, [-1, 1, 768, 1])
train_x = torch.from_numpy(train_x)
train_y = torch.from_numpy(train_y)
test_x = torch.from_numpy(test_x)
test_y = torch.from_numpy(test_y)
print("\nShape of train_x:",train_x.shape,
“\nShape of train_y:”,train_y.shape,
“\nShape of test_x:”,test_x.shape,
“\nShape of test_y:”,test_y.shape,)

batchSize = 64
torch_dataset = Data.TensorDataset(train_x,train_y)
train_loader = Data.DataLoader(dataset=torch_dataset,batch_size=batchSize,shuffle=True,num_workers=0)

class Stacked_CNN_Net(nn.Module):
def init(self):
super(Stacked_CNN_Net, self).init()
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels=1, out_channels=64, kernel_size=(6, 1)),
nn.BatchNorm2d(64),
nn.ReLU(True),
)
self.layer2 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=64, kernel_size=(3, 1)),
nn.BatchNorm2d(64),
nn.ReLU(True),
)
self.layer3 = nn.Sequential(
nn.Conv2d(in_channels=64, out_channels=128, kernel_size=(6, 1)),
nn.BatchNorm2d(128),
nn.ReLU(True),
)
self.layer4 = nn.Sequential(
nn.Conv2d(in_channels=128, out_channels=128, kernel_size=(3, 1)),
nn.BatchNorm2d(128),
nn.ReLU(True),
)
self.fc = nn.Sequential(
nn.Linear(96512, 14)
)

def forward(self, x):
    x = self.layer1(x)
    x = self.layer2(x)
    x = self.layer3(x)
    x = self.layer4(x)
    print(x.shape)
    x = x.view(x.size(0), -1)
    print(x.shape)
    x = self.fc(x)
    return x

lr_list = []
LR = 0.0001
net = Stacked_CNN_Net().cuda(2)
opt = torch.optim.Adam(net.parameters(),lr=LR)
loss_func = nn.CrossEntropyLoss().cuda(2)
params = list(net.parameters())
k = 0
epoch_list = []
accuracy_list = []
loss_list = []
def flat(data):
data=np.argmax(data,axis=-1)
return data
for epoch in range(50):
net.train()
for step,(x,y) in enumerate(train_loader):
x = x.type(torch.FloatTensor)
x,y=x.cuda(2),y
output = net(x)
y = flat(y).cuda(2)
loss = loss_func(output,y.long())
net.zero_grad()
opt.zero_grad()
loss.backward()
opt.step()
if epoch%1==0:
net.eval()
test_x = test_x.type(torch.FloatTensor)
test_out = net(test_x.cuda(2))
pred_y = torch.max(test_out,1)[1].data.squeeze().cuda(2)
lr_list.append(opt.state_dict()[‘param_groups’][0][‘lr’])
accuracy = (torch.sum(pred_y == flat(test_y.float()).cuda(2)).type(torch.FloatTensor) / test_y.size(0)).cuda(2)
print(‘Epoch: ‘, epoch, ‘| test accuracy: %.4f’ % accuracy,’|loss:%.4f’%loss,’| params:’,str(k))

epoch_list.append(epoch)
accuracy_list.append(accuracy.item())
loss_list.append(loss.item())

print(‘Epoch_list:’,epoch_list,‘Accuracy_list:’,accuracy_list,‘Loss_list:’,loss_list)

And this is my error

3

Shape of train_x: (10232, 768)
Shape of train_y: (10232,)
Shape of test_x: (4386, 768)
Shape of test_y: (4386,)

Shape of train_x: torch.Size([10232, 1, 768, 1])
Shape of train_y: torch.Size([10232])
Shape of test_x: torch.Size([4386, 1, 768, 1])
Shape of test_y: torch.Size([4386])
torch.Size([64, 128, 754, 1])
torch.Size([64, 96512])


ValueError Traceback (most recent call last)
/tmp/ipykernel_783793/3164811453.py in
93 output = net(x)
94 y = flat(y).cuda(2)
—> 95 loss = loss_func(output,y.long())
96 net.zero_grad()
97 opt.zero_grad()

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

/usr/local/lib/python3.8/dist-packages/torch/nn/modules/loss.py in forward(self, input, target)
1148
1149 def forward(self, input: Tensor, target: Tensor) → Tensor:
→ 1150 return F.cross_entropy(input, target, weight=self.weight,
1151 ignore_index=self.ignore_index, reduction=self.reduction,
1152 label_smoothing=self.label_smoothing)

/usr/local/lib/python3.8/dist-packages/torch/nn/functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
2844 if size_average is not None or reduce is not None:
2845 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 2846 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
2847
2848

ValueError: Expected input batch_size (64) to match target batch_size (0).

How to solve this error?
Thank you

I guess y = flat(y).cuda(2) might be causing the issue. What does flat do and why would you need it here? Print the shape of y before and after the operation and check if the batch size was changed.

I am getting simialr error…pls help!!
import torch.nn.functional as F

class Mnist_CNN(nn.Module):
def init(self):
super().init()
self.conv1 = nn.Conv2d(1, 16, kernel_size=3, stride=2, padding=1)
self.conv2 = nn.Conv2d(16, 16, kernel_size=3, stride=2, padding=1)
self.conv3 = nn.Conv2d(16, 10, kernel_size=3, stride=2, padding=1)

def forward(self, xb):
    #xb = xb[:,0]
    #xb = xb.view(xb.size(0), -1)
    xb = xb.view(-1,1,67*50,32)
    xb = F.relu(self.conv1(xb.float()))
    xb = F.relu(self.conv2(xb.float()))
    xb = F.relu(self.conv3(xb.float()))
    xb = F.avg_pool2d(xb, 4)
    return xb.view(-1, xb.size(1))

lr = 0.1

model, opt = get_model()
epochs=10
for epoch in range(epochs):
model.train()
for xb, yb in train_dataloader:
#print(xb," = ",yb)
print(xb.size())
pred = model(xb)
print(pred.size(),pred)
print(yb.size(),yb)
loss = loss_func(pred, yb)

    loss.backward()
    opt.step()
    opt.zero_grad()

model.eval()
with torch.no_grad():
    valid_loss = sum(loss_func(model(xb), yb) for xb, yb in val_dataloader)

print(epoch, valid_loss / len(val_dataloader))

ERROR IS------------->
torch.Size([32, 67, 50])
torch.Size([104, 10]) tensor([[ 0.0000, 0.7313, 0.0469, …, 0.0000, 0.3872, 0.0352],
[ 0.0000, 1.0703, 0.0469, …, 0.0317, 0.7710, 0.1534],
[ 0.0000, 0.0393, 0.0352, …, 0.0000, 0.0352, 0.0117],
…,
[ 0.0628, 5.8010, 9.4890, …, 2.6361, 9.2534, 7.0893],
[ 0.0628, 10.2655, 4.8131, …, 3.7205, 7.2097, 0.0628],
[ 1.3616, 10.3618, 2.0948, …, 5.1171, 9.5665, 0.3604]],
grad_fn=)
torch.Size([32]) tensor([1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 0., 0., 0., 0.])

ValueError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1372/327752771.py in
9 print(pred.size(),pred)
10 print(yb.size(),yb)
—> 11 loss = loss_func(pred, yb)
12
13 loss.backward()

~\anaconda3\envs\for_CharBert\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction, label_smoothing)
2844 if size_average is not None or reduce is not None:
2845 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 2846 return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)
2847
2848

ValueError: Expected input batch_size (104) to match target batch_size (32).

Replace these view operations:

xb = xb.view(-1,1,67*50,32)
...
xb.view(-1, xb.size(1))

with xb = xb.view(xb.size(0), -1) as described in the previous posts as the latter would keep the batch size equal while the former often yields to shape mismatches if you didn’t carefully calculate the feature shape.

Thank you for replying…
i tried this but getting new error
model, opt = get_model()
epochs=10
for epoch in range(epochs):
model.train()
for xb, yb in train_dataloader:
#print(xb," = ",yb)
print(xb.size())
pred = model(xb)
print(pred.size(),pred)
print(yb.size(),yb)
loss = loss_func(pred, yb)

    loss.backward()
    opt.step()
    opt.zero_grad()

model.eval()
with torch.no_grad():
    valid_loss = sum(loss_func(model(xb), yb) for xb, yb in val_dataloader)

print(epoch, valid_loss / len(val_dataloader))

torch.Size([32, 67, 50])

RuntimeError Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_1372/327752771.py in
6 #print(xb," = ",yb)
7 print(xb.size())
----> 8 pred = model(xb)
9 print(pred.size(),pred)
10 print(yb.size(),yb)

~\anaconda3\envs\for_CharBert\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

~\AppData\Local\Temp/ipykernel_1372/854298954.py in forward(self, xb)
13 xb=xb.view(-1, xb.size(1))
14 #xb = xb.view(-1,1,67*50,32)
—> 15 xb = F.relu(self.conv1(xb.float()))
16 xb = F.relu(self.conv2(xb.float()))
17 xb = F.relu(self.conv3(xb.float()))

~\anaconda3\envs\for_CharBert\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
1100 if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks
1101 or _global_forward_hooks or _global_forward_pre_hooks):
→ 1102 return forward_call(*input, **kwargs)
1103 # Do not call functions when jit is used
1104 full_backward_hooks, non_full_backward_hooks = [], []

~\anaconda3\envs\for_CharBert\lib\site-packages\torch\nn\modules\conv.py in forward(self, input)
444
445 def forward(self, input: Tensor) → Tensor:
→ 446 return self._conv_forward(input, self.weight, self.bias)
447
448 class Conv3d(_ConvNd):

~\anaconda3\envs\for_CharBert\lib\site-packages\torch\nn\modules\conv.py in _conv_forward(self, input, weight, bias)
440 weight, bias, self.stride,
441 _pair(0), self.dilation, self.groups)
→ 442 return F.conv2d(input, weight, bias, self.stride,
443 self.padding, self.dilation, self.groups)
444

RuntimeError: Expected 4-dimensional input for 4-dimensional weight [16, 1, 3, 3], but got 2-dimensional input of size [1600, 67] instead

There are a few issues as you are still not using the described view syntax, but moved xb=xb.view(-1, xb.size(1)) above the first conv layer.
Flatten the activation before feeding it to the first linear layer using my described approach and make sure the in_features are set to the expected value.
PS: you can post code snippets by wrapping them into three backticks ```.

hi.
Error resolved but i am getting only 0 as output.
my model is:
‘’'class Net(nn.Module):

def __init__(self):
    super(Net, self).__init__()
    self.conv1 = nn.Conv2d(1, 10, kernel_size=5)
    self.conv2 = nn.Conv2d(10, 20, kernel_size=5)
    self.mp = nn.MaxPool2d(2)
    self.fc = nn.Linear(2340, 2)

def forward(self, x):
    in_size = x.size(0)
    x = F.relu(self.mp(self.conv1(x.float())))
    x = F.relu(self.mp(self.conv2(x.float())))
    x = x.view(in_size, -1)  # flatten the tensor
    x = self.fc(x)
    return F.log_softmax(x,dim=1)

I don’t think this could happen as the probabilities for both classes have to sum to 1.. Both log probabilities thus cannot be zero as seen here:

model = Net()
x = torch.randn(1, 1, 67, 50)
out = model(x)
print(out)
# > tensor([[-0.5469, -0.8645]], grad_fn=<LogSoftmaxBackward0>)
print(torch.exp(out))
# > tensor([[0.5787, 0.4213]], grad_fn=<ExpBackward0>)

yes and during testing i am getting only zeros as output(output class can be 0 or 1)
‘’'preds = []
with torch.no_grad():
correct = 0
total = 0
for i, batch in enumerate(test_loader):
#batch = tuple(t.to(device) for t in batch)
batch = tuple(batch)
b_input_ids, b_labels = batch

outputs = model(b_input_ids[None, ...])
prediction = torch.argmax(outputs,dim=1)
print (prediction," correct=",b_labels)

preds.append(prediction)
total += b_labels.size(0)
correct+=(prediction==b_labels).sum().item()

it is printing-

tensor([0]) correct= tensor([1])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([1])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([1])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([0])
tensor([0]) correct= tensor([1])
tensor([0]) correct= tensor([0])…

Your model seems to be overfitting to the majority class so you could use a weighted loss or e.g. WeightedRandomSampler to balance the data.

Hi sir, please help me with this error:

runcell(0, ‘C:/Users/Ioana PMEC/Documents/TIDAIM/lab6/cnn2 (1).py’)
cpu
Traceback (most recent call last):

File “C:\Users\Ioana PMEC\Documents\TIDAIM\lab6\cnn2 (1).py”, line 136, in
loss = criterion(outputs, targets)

File “C:\Anaconda\lib\site-packages\torch\nn\modules\module.py”, line 1102, in _call_impl
return forward_call(*input, **kwargs)

File “C:\Anaconda\lib\site-packages\torch\nn\modules\loss.py”, line 1150, in forward
return F.cross_entropy(input, target, weight=self.weight,

File “C:\Anaconda\lib\site-packages\torch\nn\functional.py”, line 2846, in cross_entropy
return torch._C._nn.cross_entropy_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index, label_smoothing)

ValueError: Expected input batch_size (30) to match target batch_size (10).

The code I am using is:

if __name__ == '__main__':
  import torch
  import torchvision
  import torchvision.transforms as transforms
  import matplotlib.pyplot as plt
  import numpy as np
  import torch.nn as nn
  import torch.nn as nn
  import torchvision.transforms as ToTensor 
  from torch.utils.data import DataLoader
  from torch import optim
  from torch.utils.data import Dataset
  from torchvision import datasets
  from torch.autograd import Variable
  import torch.nn.functional as F
  import torch.optim as optim
  import pandas as pd
  import os 
  import csv
  from csv import DictReader
  from sklearn.utils import shuffle
  import matplotlib.pyplot as plt
  from torch.utils.data import Dataset, DataLoader
  from sklearn.model_selection import train_test_split
  import torch.utils.data as data
  
  
#path = r"E:\Andreea\an 2\tidaim\tidaim\tidaim\Benign\data\train"
#filedirectory = []
#for files in os.listdir(path):
#    filedirectorys = filedirectory.append(os.path.join(path,files))
#filedirectory


#dataset = ImageFolder(''E:\Andreea\tidaim\Benign\data\train\')

  device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
  print(device)
  
  transforms_train = transforms.Compose([transforms.Resize((68, 68)),
                                         transforms.RandomRotation(10.),
                                         transforms.ToTensor()])

  transforms_test = transforms.Compose([transforms.Resize((68, 68)),
                                        transforms.ToTensor()])
            
  
  transforms_validation = transforms.Compose([transforms.Resize((68, 68)),
                                        transforms.ToTensor()])
            
         

          

  BATCH_SIZE = 10
  LEARNING_RATE = 0.1
  TRAIN_DATA_PATH = "./train"
  TEST_DATA_PATH = "./test"
  VALIDATION_DATA_PATH = "./validation"
  TRANSFORM_IMG = transforms.Compose([
      transforms.Resize(128),
      transforms.CenterCrop(256),
      transforms.ToTensor(),
      transforms.Normalize(mean=[0.485, 0.456, 0.406],
                           std=[0.229, 0.224, 0.225] )
      ])
#dataset = ImageFolder(''E:\Andreea\tidaim\Benign\data\train\')
#transform_train = transforms.Compose([
 #       transforms.ToTensor(),
  #  ])

#transform_test = transforms.Compose([
 #       transforms.ToTensor(),
  #  ])
                      
  train_data = torchvision.datasets.ImageFolder(root=TRAIN_DATA_PATH, transform=transforms_train)
  train_data_loader = data.DataLoader(train_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)
  test_data = torchvision.datasets.ImageFolder(root=TEST_DATA_PATH, transform=transforms_test)
  test_data_loader  = data.DataLoader(test_data, batch_size=BATCH_SIZE, shuffle=True, num_workers=4) 
  validation_data = torchvision.datasets.ImageFolder(root=VALIDATION_DATA_PATH, transform=transforms_validation)
  validation_loader = data.DataLoader(validation_data, batch_size=BATCH_SIZE, shuffle=True,  num_workers=4)

  net = nn.Sequential(nn.Linear(4624, 64),
                      nn.ReLU(),
                      nn.Linear(64, 128),
                      nn.ReLU(),
                      nn.Linear(128,3),
                      )

  class MLP(nn.Module):
     def __init__ (self,n_hidden):
         super(MLP, self).__init__()

         self.linear1 = torch.nn.Linear(4624, n_hidden)
         self.linear2 = torch.nn.Linear(n_hidden,3)

         self.activation = torch.nn.ReLU()


     def forward(self,x):
        
          a1 = self.activation(self.linear1(x))
          z2 = self.linear2(a1)
    

          return z2

  net = MLP(64)


  criterion = nn.CrossEntropyLoss()
  optimizer = optim.Adam(net.parameters(), lr=1e-3)
  num_epochs = 50     
        


  for epoch in range(num_epochs): 
      net.train()
      train_loss=0
      total= 0
      correct = 0
      
      for batch_idx, (inputs, targets) in enumerate(train_data_loader):
             correct =  0 
             
             inputs = inputs.view(-1, 4624)
             optimizer.zero_grad()
             outputs = net(inputs)

             loss = criterion(outputs, targets)
             loss.backward()
             optimizer.step()

             train_loss += loss.item()
             _, predicted = torch.max(outputs.data, 1)
             total += targets.size(0)
             #correct = correct + predicted.eq(targets.data).cpu().sum 

  print('Results after epoch %d' % (epoch + 1))
  
  print('Training loss: %.3f | Training Acc: %3.f%% )%d%d)'
          % (train_loss / (batch_idx + 1), 100. *float(correct) / total, correct, total))

  net.eval()

  valid_loss = 0
  correct = 0
  total = 0 

  for batch_idx, (inputs, targets) in enumerate(validation_loader):
      inputs, targets = Variable(inputs), Variable(targets)
      inputs = inputs.view(-1, 4624)
    
      outputs = net(inputs)
    
      loss = criterion(outputs, targets)
    
      valid_loss += loss.item()
      _, predicted = torch.max(outputs.data, 1)
      total += targets.size(0)
      correct += predicted.eq(targets.data).cpu().sum()
    
  print('Validation loss: %.3f | Validation Acc: %.3f%% (%d/%d)' 
        %(valid_loss / (batch_idx + 1), * float(correct)/ total, correct, total))
   "

I really don’t know how to solve this issue.

Could you print the shape of input before the view operation as I guess you might be changing the batch size by using view(-1, 4624). If you want to flatten the input tensor use input = input.view(input.size(0), -1) and check if you are running into shape mismatches in the first linear layer. In that case, adapt the in_features of this layer to match the activation shape.

Done it. Thanks a lot