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 network layer
n_input = 2 # Number of input units, must match number of input features
n_hidden1 = 4 # Number of hidden units
n_hidden2 = 3 # Number of hidden units
n_hidden3 = 2 # Number of hidden units
n_output = 1 # Number of output units
w1 = torch.randn(n_input, n_hidden1, dtype=torch.float, requires_grad=True)
w2 = torch.randn(n_hidden1, n_hidden2, dtype=torch.float, requires_grad=True)
w3 = torch.randn(n_hidden2, n_hidden3, dtype=torch.float, requires_grad=True)
w4 = torch.randn(n_hidden3, n_output, dtype=torch.float, requires_grad=True)
b1 = torch.randn(1, n_hidden1, dtype=torch.float, requires_grad=True)
b2 = torch.randn(1, n_hidden2, dtype=torch.float, requires_grad=True)
b3 = torch.randn(1, n_hidden3, dtype=torch.float, requires_grad=True)
b4 = torch.randn(1, n_output, dtype=torch.float, requires_grad=True)
o = torch.nn.Sigmoid()(torch.mm(x,w1)+(b1))
o = torch.nn.Sigmoid()(torch.mm(o,w2)+(b2))
o = torch.nn.Sigmoid()(torch.mm(o,w3)+(b3))
o = torch.nn.Sigmoid()(torch.mm(o,w4)+(b4))
print(w1.shape); print("w1\n", w1)
print(w2.shape); print("w2\n",w2)
print(w3.shape); print("w3\n",w3)
print(w4.shape); print("w4\n",w4)
print("b1\n",b1); print("b2\n",b2); print("b3\n",b3); print("b5\n",b4)
#custom values
w1 = torch.tensor([[0.1, 0.1, 0.1, 0.1],
[0.2, 0.2, 0.2, 0.2]], dtype=torch.float)
w2 = torch.tensor([[0.1, 0.1, 0.1],
[0.2, 0.2, 0.2],
[0.3, 0.3, 0.3],
[0.4, 0.4, 0.4]])
w3 = torch.tensor([[0.1, 0.1],
[0.2, 0.2],
[0.3, 0.3]])
w4 = torch.tensor([[0.1],
[0.2]])
b1 = torch.tensor([[0.1, 0.2, 0.3, 0.4]])
b2 = torch.tensor([[0.1, 0.2, 0.3]])
b3 = torch.tensor([[0.1, 0.2]])
b4 = torch.tensor([[0.1]])
o = torch.nn.Sigmoid()(torch.mm(x,w1)+(b1))
o = torch.nn.Sigmoid()(torch.mm(o,w2)+(b2))
o = torch.nn.Sigmoid()(torch.mm(o,w3)+(b3))
o = torch.nn.Sigmoid()(torch.mm(o,w4)+(b4))
print(colored(o, 'red'))
torch.Size([2, 4])
w1
tensor([[-0.9466, -0.3500, -0.5411, -0.1657],
[-0.5217, -2.1662, 0.5265, -1.7769]], requires_grad=True)
torch.Size([4, 3])
w2
tensor([[ 0.8806, -0.6195, -0.7553],
[ 0.0374, -0.2625, -1.4905],
[ 0.1984, 0.8237, -0.1078],
[-0.8962, 0.1353, 0.2844]], requires_grad=True)
torch.Size([3, 2])
w3
tensor([[ 0.8005, -0.9527],
[-0.1611, -1.7626],
[ 0.6495, -0.1336]], requires_grad=True)
torch.Size([2, 1])
w4
tensor([[ 0.1101],
[-0.7226]], requires_grad=True)
b1
tensor([[ 2.2042, -0.7801, 1.1333, 1.7157]], requires_grad=True)
b2
tensor([[ 0.2742, 0.6487, -0.8070]], requires_grad=True)
b3
tensor([[ 0.3954, -0.6365]], requires_grad=True)
b5
tensor([[0.2616]], requires_grad=True)
tensor([[0.5733],
[0.5733],
[0.5729],
[0.5724],
[0.5732]])
class NN:
def __init__(self, n_input, n_hidden1, n_hidden2, n_hidden3, n_output):
self.w1 = torch.randn(n_input, n_hidden1, dtype=torch.float, requires_grad=True)
self.w2 = torch.randn(n_hidden1, n_hidden2, dtype=torch.float, requires_grad=True)
self.w3 = torch.randn(n_hidden2, n_hidden3, dtype=torch.float, requires_grad=True)
self.w4 = torch.randn(n_hidden3, n_output, dtype=torch.float, requires_grad=True)
self.b1 = torch.randn(1, n_hidden1, dtype=torch.float, requires_grad=True)
self.b2 = torch.randn(1, n_hidden2, dtype=torch.float, requires_grad=True)
self.b3 = torch.randn(1, n_hidden3, dtype=torch.float, requires_grad=True)
self.b4 = torch.randn(1, n_output, dtype=torch.float, requires_grad=True)
def forward(self,x):
o = torch.nn.Sigmoid()(torch.mm(x,w1)+(b1))
o = torch.nn.Sigmoid()(torch.mm(o,w2)+(b2))
o = torch.nn.Sigmoid()(torch.mm(o,w3)+(b3))
o = torch.nn.Sigmoid()(torch.mm(o,w4)+(b4))
return o
net = NN(2,4,3,2,1)
#custom weights
net.w1 = w1; net.w2 = w2; net.w3 = w3; net.w4 = w4
net.b1 = b1; net.b2 = b2; net.b3 = b3; net.b4 = b4
print(colored(net.forward(x), 'red'))
tensor([[0.5733],
[0.5733],
[0.5729],
[0.5724],
[0.5732]])
class Network(nn.Module):
def __init__(self):
super().__init__()
self.linear1 = nn.Linear(2,4)
self.linear2 = nn.Linear(4,3)
self.linear3 = nn.Linear(3,2)
self.linear4 = nn.Linear(2,1)
self.activation = nn.Sigmoid()
def forward(self,x):
o = torch.nn.Sigmoid()(self.linear1(x))
o = torch.nn.Sigmoid()(self.linear2(o))
o = torch.nn.Sigmoid()(self.linear3(o))
o = torch.nn.Sigmoid()(self.linear4(o))
return o
net = Network()
print(net)
#custom weights
net.linear1.load_state_dict( {'weight': w1.T, 'bias': b1[0]} )
net.linear2.load_state_dict( {'weight': w2.T, 'bias': b2[0]} )
net.linear3.load_state_dict( {'weight': w3.T, 'bias': b3[0]} )
net.linear4.load_state_dict( {'weight': w4.T, 'bias': b4[0]} )
#or
net.load_state_dict( {
'linear1.weight': w1.T, 'linear1.bias': b1[0],
'linear2.weight': w2.T, 'linear2.bias': b2[0],
'linear3.weight': w3.T, 'linear3.bias': b3[0],
'linear4.weight': w4.T, 'linear4.bias': b4[0],
} )
print(net.state_dict())
print(colored(net(x), 'red'))
Network(
(linear1): Linear(in_features=2, out_features=4, bias=True)
(linear2): Linear(in_features=4, out_features=3, bias=True)
(linear3): Linear(in_features=3, out_features=2, bias=True)
(linear4): Linear(in_features=2, out_features=1, bias=True)
(activation): Sigmoid()
)
OrderedDict([('linear1.weight', tensor([[0.1000, 0.2000],
[0.1000, 0.2000],
[0.1000, 0.2000],
[0.1000, 0.2000]])), ('linear1.bias', tensor([0.1000, 0.2000, 0.3000, 0.4000])), ('linear2.weight', tensor([[0.1000, 0.2000, 0.3000, 0.4000],
[0.1000, 0.2000, 0.3000, 0.4000],
[0.1000, 0.2000, 0.3000, 0.4000]])), ('linear2.bias', tensor([0.1000, 0.2000, 0.3000])), ('linear3.weight', tensor([[0.1000, 0.2000, 0.3000],
[0.1000, 0.2000, 0.3000]])), ('linear3.bias', tensor([0.1000, 0.2000])), ('linear4.weight', tensor([[0.1000, 0.2000]])), ('linear4.bias', tensor([0.1000]))])
tensor([[0.5733],
[0.5733],
[0.5729],
[0.5724],
[0.5732]], grad_fn=<SigmoidBackward>)
net = nn.Sequential(nn.Linear(2, 4),
nn.Sigmoid(),
nn.Linear(4, 3),
nn.Sigmoid(),
nn.Linear(3, 2),
nn.Sigmoid(),
nn.Linear(2, 1),
nn.Sigmoid()
)
print(net)
#custom weights
net[0].load_state_dict( {'weight': w1.T, 'bias': b1[0]} )
net[2].load_state_dict( {'weight': w2.T, 'bias': b2[0]} )
net[4].load_state_dict( {'weight': w3.T, 'bias': b3[0]} )
net[6].load_state_dict( {'weight': w4.T, 'bias': b4[0]} )
#or
net.load_state_dict( {
'0.weight': w1.T, '0.bias': b1[0],
'2.weight': w2.T, '2.bias': b2[0],
'4.weight': w3.T, '4.bias': b3[0],
'6.weight': w4.T, '6.bias': b4[0],
} )
print(net.state_dict())
print(colored(net(x), 'red'))
Sequential(
(0): Linear(in_features=2, out_features=4, bias=True)
(1): Sigmoid()
(2): Linear(in_features=4, out_features=3, bias=True)
(3): Sigmoid()
(4): Linear(in_features=3, out_features=2, bias=True)
(5): Sigmoid()
(6): Linear(in_features=2, out_features=1, bias=True)
(7): Sigmoid()
)
OrderedDict([('0.weight', tensor([[0.1000, 0.2000],
[0.1000, 0.2000],
[0.1000, 0.2000],
[0.1000, 0.2000]])), ('0.bias', tensor([0.1000, 0.2000, 0.3000, 0.4000])), ('2.weight', tensor([[0.1000, 0.2000, 0.3000, 0.4000],
[0.1000, 0.2000, 0.3000, 0.4000],
[0.1000, 0.2000, 0.3000, 0.4000]])), ('2.bias', tensor([0.1000, 0.2000, 0.3000])), ('4.weight', tensor([[0.1000, 0.2000, 0.3000],
[0.1000, 0.2000, 0.3000]])), ('4.bias', tensor([0.1000, 0.2000])), ('6.weight', tensor([[0.1000, 0.2000]])), ('6.bias', tensor([0.1000]))])
tensor([[0.5733],
[0.5733],
[0.5729],
[0.5724],
[0.5732]], grad_fn=<SigmoidBackward>)