Posts about Perceptron

Draw Perceptron graph by graphviz

Goal

This post aims to introduce how to draw a diagram for perceptron.

Reference

Libraries

In [1]:
from graphviz import Digraph

Create a node list and dictionary for the edges

In [12]:
# List of nodes
l_nodes = ['1', 'x0', 'x1', 'y']

# Dictionary mapping from label name to the edge between two nodes
d_edges = {'b': ('1', 'y'), 
           'w0': ('x0', 'y'), 
           'w1': ('x1', 'y')}

Visualize a graph for perceptron

In [13]:
# Create Digraph object
dot = Digraph()
dot.attr(rankdir='LR')

# Add nodes
for n in l_nodes:
    dot.node(n)        

# Add edges
for label, edges in d_edges.items(): 
    dot.edge(edges[0], edges[1], label=label)

# Fill node 1 by gray
dot.node('1', style='filled')
    
# Visualize the graph
dot
Out[13]:
%3 1 1y y1->y bx0 x0x0->y w0x1 x1x1->y w1

Implement Perceptron

Goal

This post aims to introduce how to implement Perceptron, which is the foundation of neural network and a simple gate function returning 0 (no signal) or 1 (signal) given a certain input.

In this post, the following fate functions are implemented:

  • AND
  • NAND
  • OR
  • XOR
$$ y = f(\mathbf{x})=\begin{cases} 0 & (b + \mathbf{wx} \le 0)\\ 1 &(b + \mathbf{wx} \gt 0) \end{cases}$$

Implement AND gate

In [10]:
def AND(x0, x1, w0=0.5, w1=0.5, b=0.6):
    return ((x0 * w0 + x1 * w1) > b) * 1.0
In [11]:
for x0, x1 in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    print(f"AND(x0={x0}, x1={x1}) = {AND(x0, x1)}")
AND(x0=0, x1=0) = 0.0
AND(x0=0, x1=1) = 0.0
AND(x0=1, x1=0) = 0.0
AND(x0=1, x1=1) = 1.0

Implement NAND gate

In [24]:
def NAND(x0, x1, w0=-0.5, w1=-0.5, b=-0.6):
    return ((x0 * w0 + x1 * w1) > b) * 1.0
In [25]:
for x0, x1 in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    print(f"NAND(x0={x0}, x1={x1}) = {NAND(x0, x1)}")
NAND(x0=0, x1=0) = 1.0
NAND(x0=0, x1=1) = 1.0
NAND(x0=1, x1=0) = 1.0
NAND(x0=1, x1=1) = 0.0

Implement OR gate

In [34]:
def OR(x0, x1, w0=0.5, w1=0.5, b=0.2):
    return ((x0 * w0 + x1 * w1) > b) * 1.0
In [35]:
for x0, x1 in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    print(f"OR(x0={x0}, x1={x1}) = {OR(x0, x1)}")
OR(x0=0, x1=0) = 0.0
OR(x0=0, x1=1) = 1.0
OR(x0=1, x1=0) = 1.0
OR(x0=1, x1=1) = 1.0

Implement XOR gate

In [36]:
def XOR(x0, x1):
    n0 = NAND(x0, x1)
    n1 = OR(x0, x1)
    return AND(n0, n1)
In [37]:
for x0, x1 in [(0, 0), (0, 1), (1, 0), (1, 1)]:
    print(f"XOR(x0={x0}, x1={x1}) = {XOR(x0, x1)}")
    
XOR(x0=0, x1=0) = 0.0
XOR(x0=0, x1=1) = 1.0
XOR(x0=1, x1=0) = 1.0
XOR(x0=1, x1=1) = 0.0