获取错误:只能将一个元素张量转换为Python标量
你能帮我解决这个问题吗。基本上,我试图研究Pytorch张量函数数据,它是向量而不是标量。X1和X2基本上是CSV文件中包含许多字符串的列。 如何迭代x1和x2中的每个数据,而不仅仅是解析整个向量。我也是Python和Pytork的新手获取错误:只能将一个元素张量转换为Python标量,python,vector,pytorch,xor,scalar,Python,Vector,Pytorch,Xor,Scalar,你能帮我解决这个问题吗。基本上,我试图研究Pytorch张量函数数据,它是向量而不是标量。X1和X2基本上是CSV文件中包含许多字符串的列。 如何迭代x1和x2中的每个数据,而不仅仅是解析整个向量。我也是Python和Pytork的新手 import torch import random import pandas data = pandas.read_csv('train/train.tsv', sep='\t') learningrate = torch.tensor(0.01) W
import torch
import random
import pandas
data = pandas.read_csv('train/train.tsv', sep='\t')
learningrate = torch.tensor(0.01)
W = torch.rand([2, 2], dtype=torch.float, requires_grad=True)
b = torch.rand(2, dtype=torch.float, requires_grad=True)
U = torch.rand(2, dtype=torch.float, requires_grad=True)
c = torch.rand(1, dtype=torch.float, requires_grad=True)
def get_item():
x1 = torch.tensor(data['Powierzchnia w m2'],
dtype=torch.float, requires_grad=True)
x2 = torch.tensor(data['Liczba pokoi'],
dtype=torch.float, requires_grad=True)
x = torch.tensor([x1, x2], dtype=torch.float)
yexpected = torch.tensor(data['cena'].values, dtype=torch.float)
return x, yexpected
for _ in range(100000):
x, yexpected = get_item()
h = torch.sigmoid(W @ x+b)
print(x)
print(yexpected)
print(h)
y = torch.sigmoid(U@h+c)
loss = (y-yexpected)**2
print(loss)
loss.backward()
with torch.no_grad():
W -= learningrate * W.grad
b -= learningrate * b.grad
c -= learningrate * c.grad
U -= learningrate * U.grad
b.grad.zero_()
W.grad.zero_()
c.grad.zero_()
U.grad.zero_()