从张量转换为CPU以获取字典值
我有一个具有以下值的字典,我正在尝试将“ 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可以完成
.cpu()
- 移至CPU,然后通过.Item()
获得张量的值。如果dict看起来如下:
然后,以下代码可以修改dict:
It can be done
.cpu()
- moving to cpu then get the value of the tensor by.item()
.If the dict looks like below:
Then, the below code can modify the dict: