我有一个变量a
,它是一组张量,如下所示:
[tensor([0.0014, 0.0021, 0.0015, 0.0007, 0.0012, 0.0024, 0.0021, 0.0019, 0.0010,
0.0010])]
[tensor([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])]
....
当我想将此作为代码的一部分时:
x = torch.tensor(a, dtype=torch.float)
我得到了这个错误:
ValueError: only one element tensors can be converted to Python scalars
我假设可能我需要像这样转换a
中的每个张量:
[tensor([[0.0014], [0.0021], [0.0015], [0.0007], [0.0012], [0.0024], [0.0021], [0.0019], [0.0010],
[0.0010]])]
[tensor([[0], [0], [0], [0], [0], [0], [0], [0], [0], [0]])]
我的想法对吗?或者我需要什么来避免上述错误
需要帮忙吗
简单,只要重塑它。假设您的张量/张量存储在名为
tlist
的列表中:在pytorch中,可以使用view()方法重塑张量。 准确地说,请使用此代码:
t2 = t1.view(t1.shape[0],-1)
其中t1是要重塑的张量工作代码:
相关问题 更多 >
编程相关推荐