我想在数据加载器中返回一个列表。 但是要返回它,它需要成为张量吗? 因此,我进行了转换,但是在此过程中信息丢失了,还有另一种方法吗?
pt_tensor_from_list = torch.tensor(pose_transform)
pt_tensor_from_list = torch.FloatTensor(pose_transform)
我期望输出:
([[-0.0003000000142492354,-0.0008999999845400453, 0.00039999998989515007,0],[0.0010000000474974513,-0.00019999999494757503,0.0003000000142492354,0],[0.00019999999494757503,-0.0005000000237487257, -0.0008999999845400453,0],[5.484399795532227,-24.28619956970215,117.5000991821289,1])
但这是:
([[--0.0003,-0.0009,0.0004,0.0000], [0.0010,-0.0002,0.0003,0.0000], [0.0002,-0.0005,-0.0009,0.0000], [5.4844,-24.2862,117.5001,1.0000]])
答案 0 :(得分:0)
在这种转换过程中,您不会丢失任何信息。之所以看起来更紧凑,是因为您在打印张量时会调用__str__()
或__repr__()
方法,从而使张量看起来更漂亮。如您所见,here torch.Tensor
使用了一种称为_tensor_str
的内部张量格式器。如果查看代码link,您会发现默认情况下参数precision
设置为4:
precision:浮点输出的精度位数(默认= 4)。
这就是为什么打印张量时张量值只有4位数字的原因。但是实际上,张量中存储的值与原始列表中的值相同。
这里有个小例子,可以帮助您:
代码:
import torch
test_list = ([[-0.0003000000142492354, -0.0008999999845400453, 0.00039999998989515007, 0],
[0.0010000000474974513, -0.00019999999494757503, 0.0003000000142492354, 0],
[0.00019999999494757503, -0.0005000000237487257, -0.0008999999845400453, 0],
[5.484399795532227, -24.28619956970215, 117.5000991821289, 1]])
print('Original values:')
for i in test_list:
for j in i:
print(j)
pt_tensor_from_list = torch.FloatTensor(test_list)
print('When printing FloatTensor:')
print(pt_tensor_from_list.dtype, pt_tensor_from_list, sep='\n')
print('When printing each value separately:')
for i in pt_tensor_from_list:
for j in i:
print(j.item())
输出:
Original values:
-0.0003000000142492354
-0.0008999999845400453
0.00039999998989515007
0
0.0010000000474974513
-0.00019999999494757503
0.0003000000142492354
0
0.00019999999494757503
-0.0005000000237487257
-0.0008999999845400453
0
5.484399795532227
-24.28619956970215
117.5000991821289
1
When printing FloatTensor:
torch.float32
tensor([[-3.0000e-04, -9.0000e-04, 4.0000e-04, 0.0000e+00],
[ 1.0000e-03, -2.0000e-04, 3.0000e-04, 0.0000e+00],
[ 2.0000e-04, -5.0000e-04, -9.0000e-04, 0.0000e+00],
[ 5.4844e+00, -2.4286e+01, 1.1750e+02, 1.0000e+00]])
When printing each value separately:
-0.0003000000142492354
-0.0008999999845400453
0.00039999998989515007
0.0
0.0010000000474974513
-0.00019999999494757503
0.0003000000142492354
0.0
0.00019999999494757503
-0.0005000000237487257
-0.0008999999845400453
0.0
5.484399795532227
-24.28619956970215
117.5000991821289
1.0
如您所见,分别打印每个值时,我们得到的值是相同的。
但,如果您选择了错误的张量类型,例如HalfTensor
而不是FloatTensor
,则可能会丢失一些信息。这是一个示例:
代码:
pt_tensor_from_list = torch.HalfTensor(test_list)
print('When printing HalfTensor:')
print(pt_tensor_from_list.dtype, pt_tensor_from_list, sep='\n')
print('When printing each value separately:')
for i in pt_tensor_from_list:
for j in i:
print(j.item())
输出:
When printing HalfTensor:
torch.float16
tensor([[-2.9993e-04, -8.9979e-04, 4.0007e-04, 0.0000e+00],
[ 1.0004e-03, -2.0003e-04, 2.9993e-04, 0.0000e+00],
[ 2.0003e-04, -5.0020e-04, -8.9979e-04, 0.0000e+00],
[ 5.4844e+00, -2.4281e+01, 1.1750e+02, 1.0000e+00]],
dtype=torch.float16)
When printing each value separately:
-0.0002999305725097656
-0.0008997917175292969
0.0004000663757324219
0.0
0.0010004043579101562
-0.00020003318786621094
0.0002999305725097656
0.0
0.00020003318786621094
-0.0005002021789550781
-0.0008997917175292969
0.0
5.484375
-24.28125
117.5
1.0
您现在可以注意到,这些值(略有不同)不同。访问pytorch tensor docs,以了解有关不同类型的torch.tensor
的更多信息。