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
Y = XOR(X)
print(Y)
import matplotlib.pyplot as plt
import seaborn as sns
plt.style.use('seaborn-whitegrid')
sns.set_style("white")
plt.figure(figsize=(5,3))
plt.title("NOR GATE",fontsize=16)
plt.scatter(input_2[:,1],input_2[:,2],c=Y,cmap="rainbow")
plt.xlim(-1,3)
plt.ylim(-1,3)
plt.grid(alpha=.4,axis="y")
plt.gca().spines["top"].set_alpha(.0)
plt.gca().spines["right"].set_alpha(.0);