遇到错误:只能将一个元素张量转换为Python标量

时间:2018-11-27 17:34:18

标签: python vector pytorch xor scalar

请帮助您解决此问题。基本上,我试图进入向量不是标量的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_()

0 个答案:

没有答案