Thank you for very quick response.
But after modifying my code, I am getting an another error.
%%time
loss_arr = []
loss_epoch_arr = []
max_epochs = 30
for epoch in range(max_epochs):
for i, data in enumerate(trainloader, 0):

    inputs, labels = data
    inputs, labels = inputs.to(cuda0), labels.to(cuda0)
    

    opt.zero_grad()
    
    outputs = model(inputs)
    pred = torch.nn.functional.softmax(out, dim=1 )
    # _,labels = torch.max(labels, dim=1)
    # _, pred = torch.max(outputs, dim=1)
    loss = soft_dice_loss(pred, labels)
    loss.backward()
    opt.step()
    
    loss_arr.append(loss.item())
    
loss_epoch_arr.append(loss.item())
print('Epoch: %d/%d, valid Dloss: %0.2f, Train DLoss: %0.2f' % (epoch, max_epochs, evaluation(validloader, model), evaluation(trainloader, model)))

plt.plot(loss_epoch_arr)
plt.show()
“RuntimeError: Trying to backward through the graph a second time, but the saved intermediate results have already been freed. Specify retain_graph=True when calling backward the first time.”