tau-2020-pytorch-tutorial/pytorch5.py
2020-12-09 10:12:35 +01:00

33 lines
896 B
Python
Executable File

#!/usr/bin/python3
import torch
import pandas as pd
data = pd.read_csv('iris.data',sep = ',', header = None)
data[5] = data[4].apply(lambda x: 1 if x == 'Iris-versicolor' else 0)
x1 = torch.tensor(data[0], dtype=torch.float)
x0 = torch.ones(x1.size(0))
x = torch.stack((x0, x1)).transpose(0, 1)
y = torch.tensor(data[5], dtype=torch.float)
w = torch.rand(2, requires_grad=True)
learning_rate = torch.tensor(0.005)
for _ in range(3000):
ypredicted = torch.nn.functional.sigmoid(x @ w)
# cost = torch.sum((ypredicted - y) ** 2)
cost = - (torch.sum(y*torch.log(ypredicted) + (torch.ones_like(y) - y) * torch.log(1- ypredicted)))
accuracy = 100 * sum((ypredicted > 0.5) == y).item() / len(ypredicted)
print(w, " => ", cost, " => ", accuracy, '% accuracy')
cost.backward()
with torch.no_grad():
w = w - learning_rate * w.grad
w.requires_grad_(True)