from IPython.display import Image
Image(filename = "nn_img/Python_Pytorch_nn_Sequential_i2_o1_sigmoid_01a.png", width=500)
from IPython.display import Image
Image(filename = "nn_img/Python_Pytorch_nn_Sequential_i2_o1_sigmoid_01.png", width=500)
import torch
from torch import nn
from torch import optim
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from draw import display_solution
#predictive
torch.manual_seed(1);
device = torch.device('cpu')
if torch.cuda.is_available():
device = torch.device('cuda')
#sigmoid explicit
def sigmoid(x):
return 1/(1+torch.exp(-x))
#sigmoid pre-defined
activation = torch.nn.Sigmoid()
# Loss (Binary Cross Entropy) error function, explicit def
def bce_err(output, target):
return -target * torch.log(output) - (1-target) * torch.log(1-output)
#sigmoid + BCELoss (Binary Cross Entropy)
criterion = torch.nn.BCEWithLogitsLoss()
def state(interval,i,loss,out,y):
if(i%interval == 0):
print("\n========== Epoch", i,"==========")
print("loss=",loss)
accuracy = np.mean( ((out > 0.5)==y).numpy() )
print("accuracy=",accuracy)
#array data points: x1, x2
data = np.array([
[1,10,1],
[3,10,0],
[1.8,2.0,0],
[-1,-1,1],
[-2,10,1],
])
#df = pd.read_csv('test.csv', header=None)
#df = pd.read_csv('data2.csv', header=None)
#data = df.to_numpy()
# nn.Linear(input,neurons)
linear = nn.Linear(2, 1)
linear.weight
Parameter containing: tensor([[ 0.3643, -0.3121]], requires_grad=True)
#features
x = torch.from_numpy(data[:, [0,1]]).float()
#target/labels
y = torch.from_numpy(data[:, [2]]).float()
class Network(nn.Module):
def __init__(self):
super().__init__()
self.linear = nn.Linear(2,1)
self.activation = nn.Sigmoid()
def forward(self,x):
o = self.linear(x)
o = self.activation(o)
return o
net = Network()
#Stochastic/Batch gradient descent
optimizer = optim.SGD(net.parameters(), lr=0.1)
#learn rate
alpha = 0.1
#iterations
epochs = 20
#display state
fv = 5
lossHistory = []
predictionHistory = []
w = net.linear.weight.data[0].numpy()
b = net.linear.bias.data.numpy()[0]
predictionHistory.append([w[0],w[1],b])
for i in range(epochs):
#forward
out = net(x)
#error function
loss = torch.mean(bce_err(out,y))
#loss = torch.mean(nn.BCELoss(out,y))
#loss = criterion(out, y)
lossHistory.append(loss)
optimizer.zero_grad()
#process gradients
loss.backward()
#update weights
optimizer.step()
w = net.linear.weight.data[0].numpy()
b = net.linear.bias.data.numpy()[0]
predictionHistory.append([w[0],w[1],b])
state(fv,i,loss,out,y)
print("")
print("Final result:", w[0],w[1], b)
display_solution(data,[w[0],w[1], b],predictionHistory,lossHistory,epochs,ylim=None)
========== Epoch 0 ========== loss= tensor(2.7482, grad_fn=<MeanBackward0>) accuracy= 0.6 ========== Epoch 5 ========== loss= tensor(0.6552, grad_fn=<MeanBackward0>) accuracy= 0.6 ========== Epoch 10 ========== loss= tensor(0.4662, grad_fn=<MeanBackward0>) accuracy= 1.0 ========== Epoch 15 ========== loss= tensor(0.3693, grad_fn=<MeanBackward0>) accuracy= 1.0 Final result: -0.7267321 0.08352548 0.48246527
(100,) (100,)
w = torch.tensor([[1],[-1]], dtype=torch.float64)
b = torch.tensor([[-1]], dtype=torch.float64)
net.linear.load_state_dict( {'weight': w.T, 'bias': b[0]})
print(net.state_dict())
print(x)
print(net.linear.weight)
print(net.linear.bias)
out = net(x)
print("---\n",out,"\n---")
OrderedDict([('linear.weight', tensor([[ 1., -1.]])), ('linear.bias', tensor([-1.]))])
tensor([[ 1.0000, 10.0000],
[ 3.0000, 10.0000],
[ 1.8000, 2.0000],
[-1.0000, -1.0000],
[-2.0000, 10.0000]])
Parameter containing:
tensor([[ 1., -1.]], requires_grad=True)
Parameter containing:
tensor([-1.], requires_grad=True)
---
tensor([[4.5398e-05],
[3.3535e-04],
[2.3148e-01],
[2.6894e-01],
[2.2603e-06]], grad_fn=<SigmoidBackward>)
---
net.linear.weight.data.fill_(1)
net.linear.bias.data.fill_(1)
print(net.state_dict())
print(x)
print(net.linear.weight)
print(net.linear.bias)
out = net(x)
print("---\n",out,"\n---")
OrderedDict([('linear.weight', tensor([[1., 1.]])), ('linear.bias', tensor([1.]))])
tensor([[ 1.0000, 10.0000],
[ 3.0000, 10.0000],
[ 1.8000, 2.0000],
[-1.0000, -1.0000],
[-2.0000, 10.0000]])
Parameter containing:
tensor([[1., 1.]], requires_grad=True)
Parameter containing:
tensor([1.], requires_grad=True)
---
tensor([[1.0000],
[1.0000],
[0.9918],
[0.2689],
[0.9999]], grad_fn=<SigmoidBackward>)
---