从张量转换为CPU以获取字典值

发布于 2025-02-03 02:19:25 字数 698 浏览 3 评论 0原文

我有一个具有以下值的字典,我正在尝试将“ train_acc”中的张量转换为像其他值一样的浮点值列表,以便我可以使用它来绘制图形,但我不知道该怎么做。

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]})

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]})

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

红衣飘飘貌似仙 2025-02-10 02:19:25

可以完成.cpu() - 移至CPU,然后通过.Item()获得张量的值。
如果dict看起来如下:

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]
}

然后,以下代码可以修改dict:

dict['train_acc'] = [x.cpu().item() for x in dict['train_acc']]

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']]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文