AttributeError: 'int' object has no attribute 'detach'

Anyone got a tip for this? This is the code and error it gives:

# Training
trg_loss = [] 
with torch.set_grad_enabled(True):
    for data_library['100MHz'], data_library['250MHz'] in training_generator:
            new_loss = train(data_library[prior],data_library[target])
        # Parallel(n_jobs=core_num)
        # print(data_library['20MHz'].shape)
        # delayed(train)(data_library[prior],data_library[target])
#print("\nTraining Loss: ", sum(trg_loss).detach().numpy().max()/len(trg_loss))
FILE.write("\nEpoch "+str(epoch)+": Training Loss = " +str(sum(trg_loss).detach().numpy().max()/len(trg_loss)))

with the error being:

AttributeError: ‘int’ object has no attribute 'detach’

Do you have any idea how I can get it to print properly though?

Could you check the type and shape of sum(trg_loss)?
If trg_loss contains tensors, sum(trg_loss) should return another tensor, which shouldn’t raise the error on calling detach().
However, if new_loss is an int, then this won’t work and you would have to create a tensor first or use the list entries directly. Note that creating a tensor of Ints, will not contain any Autograd history, so that you wouldn’t need to call detach() at all.