#!/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) optimizer = torch.optim.SGD([w], lr=0.005) for _ in range(3000): optimizer.zero_grad() ypredicted = torch.nn.functional.sigmoid(x @ w) 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() optimizer.step()