본문 바로가기
AI

AI Study : Logic Gates in Linear Perceptron Algorithm(3D graphs) / 선형 퍼셉트론 회로

by 재르미온느 2024. 4. 7.

Let's understand Logic Gates in Linear Perceptron Algorithm by plotting. We can learn how to visualize these gates by 3D graphs.

3D graph를 그려서 선형 퍼셉트론 회로들을 이해해보고자 한다.

 

Linear Perceptron : OR , AND gates

 

OR gate

def OR(x1, x2):
    a1, a2, b=0.3, 0.3, 0.4
    delta=0.5
    y=a1*x1+a2*x2+b
    if y< delta:
        return 0
    else:
        return 1

 

* Let's explore this logic further with the help of a plot.

 

OR GATE _ 3D

 

CODE

import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

x1_values = np.linspace(0, 1, 100)
x2_values = np.linspace(0, 1, 100)
X1, X2 = np.meshgrid(x1_values, x2_values)

Y = np.array([[OR(x1, x2) for x1, x2 in zip(x1_row, x2_row)] for x1_row, x2_row in zip(X1, X2)])

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.plot_surface(X1, X2, Y, cmap='viridis')

X1_below_delta, X2_below_delta = np.where(Y < 0.5)
ax.scatter(X1[X1_below_delta, X2_below_delta], X2[X1_below_delta, X2_below_delta], Y[X1_below_delta, X2_below_delta], color='red')

ax.text(0.5, 0.5, 0, "FALSE", color='red', fontsize=12)
ax.text(0.5, 0.5, 1, "TRUE", color='blue', fontsize=12)

ax.set_xlabel('x1')
ax.set_ylabel('x2')
ax.set_zlabel('y')
plt.title('OR Gate')

plt.show()

 

 

AND gate

def AND(x1, x2):
    a1, a2, b=0.3,0.3,0.4
    delta=0.9
    y=a1*x1+a2*x2+b
    if y< delta:
        return 0
    else:
        return 1

 

 

NAND gate

def NAND(x1, x2):
    a1, a2, b=0.3, 0.3, 0.4
    delta=0.8
    y=a1*x1+a2*x2+b
    if y> delta:
        return 0
    else:
        return 1

 

 

ref : https://www.kaggle.com/code/goen01/chapter1-or-and-xor

 

chapter1_OR_AND_XOR

Explore and run machine learning code with Kaggle Notebooks | Using data from No attached data sources

www.kaggle.com