import torch
X = torch.tensor([[1,0,0],[1,1,0],[1,0,1,],[1,1,1]] ,dtype = torch.float32)
orgate = torch.tensor([0,1,1,1],dtype = torch.float32)
def OR(X):
w = torch.tensor([-0.5,1,1] ,dtype = torch.float32)
zhat = torch.mv(X,w)
yhat = torch.tensor([int(x) for x in zhat>=0],dtype = torch.float32)
return yhat
sigma_or = OR(X)
import torch
X = torch.tensor([[1,0,0],[1,1,0],[1,0,1,],[1,1,1]] ,dtype = torch.float32)
nandgate = torch.tensor([1,1,1,0],dtype = torch.float32)
def NAND(X):
w = torch.tensor([0.7,-0.5,-0.5] ,dtype = torch.float32)
zhat = torch.mv(X,w)
yhat = torch.tensor([int(x) for x in zhat>=0],dtype = torch.float32)
return yhat
sigma_nand = NAND(X)
x0 = torch.tensor([1,1,1,1],dtype = torch.float32)
input_2 = torch.cat((x0.view(4,1),sigma_nand.view(4,1),sigma_or.view(4,1)),dim=1)
def AND(X):
w = torch.tensor([-0.7,0.5,0.5] ,dtype = torch.float32)
zhat = torch.mv(X,w)
yhat = torch.tensor([int(x) for x in zhat>=0],dtype = torch.float32)
return yhat
sigma_and = AND(input_2)
def XOR(X):
input_1 = X
sigma_nand = NAND(input_1)
sigma_or = OR(input_1)
x0 = torch.tensor([1,1,1,1],dtype = torch.float32)
input_2 = torch.cat((x0.view(4,1),sigma_nand.view(4,1),sigma_or.view(4,1)),dim=1)
y_and = AND(input_2)
return y_and