我想将数据设置为 PyTorch 张量,作为 ML 算法的目标。如果我按原样检查列表,我会得到:
ydata = [1.39e-11, 1.5e-12, 4.7e-16]
我想在张量中包含这个列表,所以我尝试了多个函数,但每次我都会收到 TypeError :
torch.FloatTensor(ydata) -> TypeError('must be real number, not str')
但是,如果我检查 ydata 每个元素的类型,它是浮点数。我怎样才能将我想要的数据转换为张量?
非常感谢。
我只是尝试在我的机器上复制您的示例:
pip install torch torchvision torchaudio
然后:
import torch
ydata = ['1.39e-11', '1.5e-12', '4.7e-16']
ydata = [float(x) for x in ydata] # convert your string to float
torch.FloatTensor(ydata)
#output
tensor([1.3900e-11, 1.5000e-12, 4.7000e-16])