def train_CNN(model, optimizer, train_dataloader, epochs, run_number,
val_dataloader=None, save_run=None, return_progress_dict = None, hide_text = None):
# Tracking lowest validation loss
lowest_val_loss = float('inf')
if return_progress_dict == 'Yes':
progress_dict = {run_number: {'Epoch':[], 'Avg_Training_Loss':[], 'Validation_Loss':[], 'Validation_Accuracy':[]} }
# Start training loop
if hide_text != "Yes":
print("Start training...\n")
print(f"{'Epoch':^7} | {'Train Loss':^12} | {'Val Loss':^10} | {'Val Acc':^9} | {'Elapsed':^9}")
print("-"*60)
for epoch_i in range(epochs):
# =======================================
# Training
# =======================================
# Tracking time and loss
t0_epoch = time.time()
total_loss = 0
# Put the model into the training mode
model.train()
for step, batch in enumerate(train_dataloader):
# Load batch to CPU
data, labels = tuple(t.to(device) for t in batch)
#labels = labels.type(torch.LongTensor) #23Feb2022 casting to long as per https://stackoverflow.com/questions/69742930/runtimeerror-nll-loss-forward-reduce-cuda-kernel-2d-index-not-implemented-for
#labels.to(device)
# Zero out any previously calculated gradients
optimizer.zero_grad()
# Perform a forward pass. This will return logits.
probs = model(data)
# Compute loss and accumulate the loss values
print("------------")
print ("probs[0].dtype:")
print(probs[0].dtype)
print ("probs[0]:")
print (probs[0])
#print (probs)
probs=probs.type(torch.cuda.FloatTensor)
#probs=probs.type(torch.cuda.LongTensor)
print ("probs[0].dtype:")
print(probs[0].dtype)
print("------------")
labels = labels.type(torch.cuda.FloatTensor)
#labels=labels.type(torch.cuda.LongTensor)
#labels = labels.type(torch.cuda.DoubleTensor)
#x_cuda = Variable(x, requires_grad=True).cuda()
print("labels:")
print (labels)
print("labels.dtype:")
print (labels.dtype)
print("labels[0].dtype:")
print(labels[0].dtype)
print("------------")
loss = F.cross_entropy(probs, labels)
total_loss += loss.item()