【发布时间】:2020-09-19 02:30:39
【问题描述】:
我正在尝试通过组装通过基本数学函数计算的向量的维度来创建一些测试torch 张量。作为先驱:从原始 python arrays 组装张量确实有效:
import torch
import numpy as np
torch.Tensor([[1.0, 0.8, 0.6],[0.0, 0.5, 0.75]])
>> tensor([[1.0000, 0.8000, 0.6000],
[0.0000, 0.5000, 0.7500]])
此外,我们可以从 numpy 数组中组装张量
https://pytorch.org/docs/stable/tensors.html:
torch.tensor(np.array([[1, 2, 3], [4, 5, 6]]))
tensor([[ 1, 2, 3],
[ 4, 5, 6]])
但是,我无法从计算的向量中进行组装。以下是一些尝试:
X = torch.arange(0,6.28)
x = X
torch.Tensor([[torch.cos(X),torch.tan(x)]])
torch.Tensor([torch.cos(X),torch.tan(x)])
torch.Tensor([np.cos(X),np.tan(x)])
torch.Tensor([[np.cos(X),np.tan(x)]])
torch.Tensor(np.array([np.cos(X),np.tan(x)]))
以上都有以下错误:
ValueError: 只有一个元素张量可以转换为 Python 标量
正确的语法是什么?
更新请求显示x / X 的评论。它们实际上设置为相同(我在中途改变了主意)
In [56]: x == X
Out[56]: tensor([True, True, True, True, True, True, True])
In [51]: x
Out[51]: tensor([0., 1., 2., 3., 4., 5., 6.])
In [52]: X
Out[52]: tensor([0., 1., 2., 3., 4., 5., 6.])
【问题讨论】:
-
请告诉我们您在从计算向量组装时使用的
x和X的值。您可能在某个地方有错字。 -
@SVPraveen 为问题添加了
x/X值