import torch
from torch import nn
import numpy as np
from termcolor import colored
#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],
])
#features
x = torch.from_numpy(data[:, [0,1]]).float()
#target/labels
y = torch.from_numpy(data[:, [2]]).float()
# Define the size of each layer in our network
n_input = 2 # Number of input units, must match number of input features
n_hidden = 1 # Number of hidden units
n_output = 1 # Number of output units
# Weights for inputs to hidden layer
w = torch.randn(n_input, n_hidden, dtype=torch.float, requires_grad=True)
# and bias terms for hidden and output layers
b = torch.randn(1, n_hidden, dtype=torch.float, requires_grad=True)
print(w.shape,b.shape)
w = torch.tensor([[0.1],
[0.2]], dtype=torch.float)
b = torch.tensor([[ 1]], dtype=torch.float)
print(w.shape,b.shape)
print(x)
print(colored(w, 'red'))
print(colored(b, 'red'))
out = torch.nn.Sigmoid()(torch.mm(x,w)+(b))
print(out)
torch.Size([2, 1]) torch.Size([1, 1])
torch.Size([2, 1]) torch.Size([1, 1])
tensor([[ 1.0000, 10.0000],
[ 3.0000, 10.0000],
[ 1.8000, 2.0000],
[-1.0000, -1.0000],
[-2.0000, 10.0000]])
tensor([[0.1000],
[0.2000]])
tensor([[1.]])
tensor([[0.9569],
[0.9644],
[0.8292],
[0.6682],
[0.9427]])
class NN:
def __init__(self, inputUnits, hiddenUnits, outputUnits):
# Define the size of each layer in our network
n_input = inputUnits # Number of input units, must match number of input features
n_hidden = hiddenUnits # Number of hidden units
n_output = outputUnits # Number of output units
# Weights for inputs to hidden layer
self.w1 = torch.randn(n_input, n_hidden, dtype=torch.float, requires_grad=True)
# and bias terms for hidden and output layers
self.b1 = torch.randn(1, n_hidden, dtype=torch.float, requires_grad=True)
self.activation = torch.nn.Sigmoid()
def forward(self,x):
o = self.activation(torch.mm(x,self.w1)+(self.b1))
return o
net = NN(2,1,1)
net.w1 = torch.tensor([[0.1],
[0.2]], dtype=torch.float)
net.b1 = torch.tensor([[ 1]], dtype=torch.float)
print(x)
print(colored(net.w1, 'red'))
print(colored(net.b1, 'red'))
print(net.forward(x))
tensor([[ 1.0000, 10.0000],
[ 3.0000, 10.0000],
[ 1.8000, 2.0000],
[-1.0000, -1.0000],
[-2.0000, 10.0000]])
tensor([[0.1000],
[0.2000]])
tensor([[1.]])
tensor([[0.9569],
[0.9644],
[0.8292],
[0.6682],
[0.9427]])
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()
w = torch.tensor([[0.1],
[0.2]], dtype=torch.float)
b = torch.tensor([[ 1]], dtype=torch.float)
net.linear.load_state_dict( {'weight': w.T, 'bias': b[0]})
print(net.state_dict())
print(x)
print(colored(net.linear.weight.data, 'red'))
print(colored(net.linear.bias.data, 'red'))
print(net(x))
OrderedDict([('linear.weight', tensor([[0.1000, 0.2000]])), ('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]])
tensor([[0.1000, 0.2000]])
tensor([1.])
tensor([[0.9569],
[0.9644],
[0.8292],
[0.6682],
[0.9427]], grad_fn=<SigmoidBackward>)
net = nn.Sequential(nn.Linear(2, 1),
nn.Sigmoid(),
)
w = torch.tensor([[0.1],
[0.2]], dtype=torch.float)
b = torch.tensor([[ 1]], dtype=torch.float)
net.load_state_dict( {'0.weight': w.T, '0.bias': b[0]} )
print(net.state_dict())
print(x)
print(colored(net[0].weight.data, 'red'))
print(colored(net[0].bias.data, 'red'))
print(net(x))
OrderedDict([('0.weight', tensor([[0.1000, 0.2000]])), ('0.bias', tensor([1.]))])
tensor([[ 1.0000, 10.0000],
[ 3.0000, 10.0000],
[ 1.8000, 2.0000],
[-1.0000, -1.0000],
[-2.0000, 10.0000]])
tensor([[0.1000, 0.2000]])
tensor([1.])
tensor([[0.9569],
[0.9644],
[0.8292],
[0.6682],
[0.9427]], grad_fn=<SigmoidBackward>)