unit 1.2 - Binary net in PyTorch with manual weights

Open In Colab

Now we will learn what is a β€œproper” way to define this super simple AND neural network in PyTorch

[1]:
import torch
from torch import nn
import torch.nn.functional as F
[2]:
# professional way to define model in pytorch:

class Logic_net(nn.Module):
    def __init__(self):
        super(Logic_net, self).__init__()
        self.l1 = nn.Linear(2, 2)
        self.l2 = nn.Linear(2, 1)

    def forward(self, x):
        x = F.relu(self.l1(x))
        x = self.l2(x)
        return x

model = Logic_net()

# AND model weights:
model.l1.weight = nn.Parameter(torch.Tensor([[0,0],[0.45,0.45]]))
model.l1.bias = nn.Parameter(torch.Tensor([0,0]))
model.l2.weight = nn.Parameter(torch.Tensor([[0,1]]))
model.l2.bias = nn.Parameter(torch.Tensor([0]))
[3]:
# AND model test:
i1 = torch.Tensor([0,0])
o1 = model(i1)
i2 = torch.Tensor([0,1])
o2 = model(i2)
i3 = torch.Tensor([1,0])
o3 = model(i3)
i4 = torch.Tensor([1,1])
o4 = model(i4)

print("out:", o1.item(),o2.item(),o3.item(),o4.item())
out: 0.0 0.44999998807907104 0.44999998807907104 0.8999999761581421
[4]:
# another easier way to define model:

model = torch.nn.Sequential(
    torch.nn.Linear(2, 2),
    torch.nn.ReLU(),
    torch.nn.Linear(2, 1),
)

# print(model)
# print(model[0].weight)

# a AND model weights:
model[0].weight = nn.Parameter(torch.Tensor([[0.3,-0.2],[1,1]]))
model[0].bias = nn.Parameter(torch.Tensor([-0.5,-0.5]))
model[2].weight = nn.Parameter(torch.Tensor([[-0.2,0.9]]))
model[2].bias = nn.Parameter(torch.Tensor([-0.2]))
[5]:
# AND model test:
i1 = torch.Tensor([0,0])
o1 = model(i1)
i2 = torch.Tensor([0,1])
o2 = model(i2)
i3 = torch.Tensor([1,0])
o3 = model(i3)
i4 = torch.Tensor([1,1])
o4 = model(i4)

print("out:", o1.item(),o2.item(),o3.item(),o4.item())
out: -0.20000000298023224 0.2499999850988388 0.2499999850988388 1.1499998569488525

HOMEWORK

Try to make the XOR or XNOR neural network on your own.

Tip: think about decomposing the XOR function: XOR(x1,x2) = \(x1*x2 + NOT(x1)*NOT(+x2)\)

Note that \(x1*x2\) = AND(x1, x2)

Can it be solved with 1 neuron?