tau-2020-pytorch-tutorial/pytorch4.py

30 lines
594 B
Python
Raw Normal View History

2020-12-09 10:12:35 +01:00
#!/usr/bin/python3
import torch
import pandas
data = pandas.read_csv('mieszkania.tsv', sep='\t')
x1 = torch.tensor(data['powierzchnia'], dtype=torch.float)
x0 = torch.ones(x1.size(0))
x = torch.stack((x0, x1)).transpose(0, 1)
y = torch.tensor(data['cena'], dtype=torch.float)
w = torch.rand(2, requires_grad=True)
learning_rate = torch.tensor(0.000002)
for _ in range(400000):
ypredicted = x @ w
cost = torch.sum((ypredicted - y) ** 2)
print(w, " => ", cost)
cost.backward()
with torch.no_grad():
w = w - learning_rate * w.grad
w.requires_grad_(True)