know I am getting this error
class NN(nn.Module):
def init(self,num_classes):
super(NN,self).init()
self.l1=nn.Linear(3*75*75, 50)
self.relu= nn.ReLU()
self.l2 = nn.Linear(50,5)
def forward(self,x):
out= x.view(x.size(0), -1)
out =self.l1(x)
out = self.relu(out)
out=self.l2(out)
return out
model=NN(num_classes=5)
optimizer=Adam(model.parameters(),lr=0.001,weight_decay=0.0001)
loss_function = nn.CrossEntropyLoss()
num_epochs=10
train_count=len(glob.glob(train_path+’//*.jpg’))
test_count=len(glob.glob(test_path+’//*.jpg’))
print_every = 5
Initialize the loss variables
running_loss = 0
train_losses, test_losses = [], []
Track the current training step, start at 0
steps = 0
for epochs in range(num_epochs):
epochs += 1
model.train()
for i, (images,labels) in enumerate(train_loader):
steps += 1
images=images.view(images.size(0),-1)
optimizer.zero_grad()
print(images.shape)
output=model(images)
loss=loss_function(output,labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
# Every 5 steps, evaluate the model
if steps % print_every == 0:
# Initialize loss and accuracy
test_loss = 0
accuracy = 0
# Start the model evaluation
model.eval()
# Refine the accuracy of the prediction without updating the gradients
with torch.no_grad():
# Load in all of the image inputs and labels from the TEST loader
for i, (inputs, labels) in enumerate(test_loader):
# Load the inputs and labels to the already selected device
images, labels = images, labels
# images=images.reshape(images.shape[0],-1)
images=images.view(images.size(0),-1)
# Pass the images through the model, return the log probabilities of each label
output = model(images)
print(f"images: {images.shape}" )
# Run the log probabilities through the criterion to get the output graph
loss = loss_function(output, labels)
# Add the actual loss number to the running loss total for the test batch
test_loss += loss.item()
# Return a new tensor with the true probabilities
ps = torch.exp(output)
# Return the largest probability and class of the new tensor along a given dimension
top_p, top_class = ps.topk(1, dim=1)
# Reshape the tensor to match the same shape as the top class
equals = top_class == labels.view(*top_class.shape)
# Compute the accuracy and add it to the running accuracy count for the test batch
accuracy += torch.mean(equals.type(torch.FloatTensor)).item()
# Append the training and testing losses
train_losses.append(running_loss/len(train_loader))
test_losses.append(test_loss/len(test_loader))
# Display the accuracy of the prediction with 3 digits in the fractional part of the decimal
print(f"\n Epoch {epoch}/{epochs}: "
f"Train loss: {running_loss/print_every:.3f}.. "
f"Test loss: {test_loss/len(test_loader):.3f}.. "
f"Test accuracy: {accuracy/len(test_loader):.3f}\n")
# Train the model
running_loss = 0
model.train()
# After 5 training steps, start the next epoch
# Break here in case the trainloader has remaining data
break
..............................
output......................
torch.Size([14, 16875])
torch.Size([14, 16875])
torch.Size([14, 16875])
torch.Size([14, 16875])
torch.Size([14, 16875])
images: torch.Size([14, 16875])
images: torch.Size([14, 16875])
ValueError Traceback (most recent call last)
in
72 print(f"images: {images.shape}" )
73 # Run the log probabilities through the criterion to get the output graph
—> 74 loss = loss_function(output, labels)
75 # Add the actual loss number to the running loss total for the test batch
76 test_loss += loss.item()
~\Miniconda3\lib\site-packages\torch\nn\modules\module.py in _call_impl(self, *input, **kwargs)
725 result = self._slow_forward(*input, **kwargs)
726 else:
→ 727 result = self.forward(*input, **kwargs)
728 for hook in itertools.chain(
729 _global_forward_hooks.values(),
~\Miniconda3\lib\site-packages\torch\nn\modules\loss.py in forward(self, input, target)
959
960 def forward(self, input: Tensor, target: Tensor) → Tensor:
→ 961 return F.cross_entropy(input, target, weight=self.weight,
962 ignore_index=self.ignore_index, reduction=self.reduction)
963
~\Miniconda3\lib\site-packages\torch\nn\functional.py in cross_entropy(input, target, weight, size_average, ignore_index, reduce, reduction)
2466 if size_average is not None or reduce is not None:
2467 reduction = _Reduction.legacy_get_string(size_average, reduce)
→ 2468 return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
2469
2470
~\Miniconda3\lib\site-packages\torch\nn\functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
2259
2260 if input.size(0) != target.size(0):
→ 2261 raise ValueError(‘Expected input batch_size ({}) to match target batch_size ({}).’
2262 .format(input.size(0), target.size(0)))
2263 if dim == 2:
ValueError: Expected input batch_size (14) to match target batch_size (11).