Since this the first time I am trying to convert the model to half precision, so I just followed the post below. And it was converting the model to float and half, back and forth, so I thought this is the correct way.

  
      
      kaggle.com
  
  
    

Carvana Image Masking Challenge 5

Automatically identify the boundaries of the car in an image


  
  
    
    
  
  


But I am getting error even on the first epoch if I remove don’t convert back the model back to float. The modified code looks like:
def train_classifier(classifier, train_loader, optimizer, criterion):
    classifier.half()
    classifier.train()
    loss = 0.0
    losses = []
    for i, (images, labels) in enumerate(train_loader):
        images, labels = images.to(device), labels.float().to(device)
        images = images.half()
        optimizer.zero_grad()
        logits = classifier(images)
        logits = logits.float()
        loss = criterion(logits, labels)
        loss = loss.float()
        loss.backward()
        optimizer.step()
        losses.append(loss)
    return torch.stack(losses).mean().item()

The error which I am getting is:
image1405×324 23.3 KB