你能帮忙解决这个问题吗。基本上,我试图得到Pytorch张量函数的数据,它是向量而不是标量。X1和X2基本上是CSV文件中包含许多字符串的列。 如何迭代x1和x2的每一个数据,而不仅仅是解析整个向量。我也是Python和Pytorch的新手。在
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_()
目前没有回答
相关问题 更多 >
编程相关推荐