This is the actual and full code that i’m using
data:
import torch
import torchvision
import torchvision.transforms as transforms

transform = transforms.Compose(
    [transforms.ToTensor(),
     transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
                                          shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root='./data', train=False,
                                       download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
                                         shuffle=False, num_workers=2)


optimizer and loss
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)

model:
model_ft = models.resnet18(pretrained=False)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 10)
net = model_ft

train:
net.cuda()
for epoch in range(15):  # loop over the dataset multiple times
    acc= 0
    train_acc=0
    running_loss = 0.0
    for i, data in enumerate(trainloader, 0):
        # get the inputs; data is a list of [inputs, labels]
        inputs, labels = data
        
        inputs, labels = inputs.cuda(), labels.cuda()
        # zero the parameter gradients
        optimizer.zero_grad()
        labels = labels.squeeze()
        # forward + backward + optimize
        outputs = net(inputs)
        
#         outputs = outputs.view(outputs.shape[0],outputs.shape[1])
#         labels = labels.squeeze()
#         print(outputs.shape,labels.shape)
        loss = criterion(outputs, labels)
        loss.backward()
        optimizer.step()
        for i in range(len(labels)):
            if labels[i] == torch.argmax(outputs[i]):
                acc+=1
        # print statistics
        running_loss += loss.item()
        ps = torch.exp(outputs)
        top_p, top_class = ps.topk(1, dim=1)
        equals = top_class == labels.view(*top_class.shape)
        train_acc += torch.mean(equals.type(torch.FloatTensor))
        
#         if i % 2000 == 1999:    # print every 2000 mini-batches
    print('[%d, %5d] loss: %.3f acc: %.3f ' %
          (epoch + 1, i + 1, running_loss,train_acc ))
    print(acc)
    acc=0
    running_loss = 0.0
    train_acc = 0
            
print('Finished Training')