Convert from tensor to cpu for dictionary values
I have a dictionary which has the following values and I am trying to convert my tensors in 'train_acc' to a list of float values like the rest so that I can use it to plot graph but I have no idea how to do it.
defaultdict(list,
            {'train_acc': [tensor(0.9889, device='cuda:0', dtype=torch.float64),
              tensor(0.9909, device='cuda:0', dtype=torch.float64),
              tensor(0.9912, device='cuda:0', dtype=torch.float64)],
             'train_loss': [0.049552333343110315,
              0.040933397413570306,
              0.04100083970214572],
             'val_acc': [0.9779669504256384,
              0.9779669504256384,
              0.9779669504256384],
             'val_loss': [0.11118546511442401,
              0.11118546511442401,
              0.11118546511442401]})


It can be done .cpu() - moving to cpu then get the value of the tensor by .item().
If the dict looks like below:
dict = {

'train_acc': [tensor(0.9889, device='cuda:0', dtype=torch.float64),
              tensor(0.9909, device='cuda:0', dtype=torch.float64),
              tensor(0.9912, device='cuda:0', dtype=torch.float64)],
 
'train_loss':[0.049552333343110315,
              0.040933397413570306,
              0.04100083970214572],

 'val_acc':   [0.9779669504256384,
              0.9779669504256384,
              0.9779669504256384],

 'val_loss':  [0.11118546511442401,
              0.11118546511442401,
              0.11118546511442401]
}


Then, the below code can modify the dict:
dict['train_acc'] = [x.cpu().item() for x in dict['train_acc']]

