如何在 Pytorch 中将一维 IntTensor 转换为 int [英] How to cast a 1-d IntTensor to int in Pytorch

查看:231
本文介绍了如何在 Pytorch 中将一维 IntTensor 转换为 int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到一个 1-D IntTensor,但我想将它转换为整数.我用这个方法试试:

I get a 1-D IntTensor,but i want to convert it to a integer. I try it by this method:

print(dictionary[IntTensor.int()])

但出现错误:

KeyError: Variable containing:
 423
[torch.IntTensor of size 1]

谢谢~

推荐答案

您可以使用:

print(dictionary[IntTensor.data[0]])

您使用的键是 autograd.Variable 类型的对象..data 给出张量,索引 0 可用于访问元素.

The key you're using is an object of type autograd.Variable. .data gives the tensor and the index 0 can be used to access the element.

这篇关于如何在 Pytorch 中将一维 IntTensor 转换为 int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆