Skip to content

Instantly share code, notes, and snippets.

@Nydhal
Created November 13, 2017 00:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Nydhal/1985b1d04bc23cb725a3c8eef52a5670 to your computer and use it in GitHub Desktop.
Save Nydhal/1985b1d04bc23cb725a3c8eef52a5670 to your computer and use it in GitHub Desktop.
Simple perceptron neural net to test boolean functions (CSE471).
import math
def sig(x):
return 1 / (1 + math.exp(-x))
b = [0,1]
# INPUT WEIGHTS #
w1 = [ 2, 1,-3,-4,-4, 4]
w2 = [-2,-3, 1, 4, 5,-4]
w3 = [ 3, 1,-3,-4,-3, 5]
w4 = [-4,-2,-2,-4,-4,-3]
W = [w1,w2,w3,w4]
def nn(X1,X2,w):
H1 = sig(w[0]*X1+w[2]*X2+0.5)
H2 = sig(w[1]*X1+w[3]*X2+0.5)
Y = sig(w[4]*H1+w[5]*H2+0.5)
return round(Y)
print('┌──┐ w0 ┌──┐')
print('│X1├─────>│H1│')
print('└──┘ └──┘')
print(' \ ^ \\ w4')
print(' w1 \ / \\')
print(' \ / >┌───┐')
print(' X │ Y │')
print(' / \ >└───┘')
print(' w2 / \ / ')
print(' / v / w5')
print('┌──┐ w3 ┌──┐')
print('│X2├─────>│H2│')
print('└──┘ └──┘')
print('Weights = ',W)
print("┌────┬────┬────┬────┬────┬────┬────┐")
print("│X1 │X2 │AND │OR │NAND│NOR │ NN │")
for v in W:
print("├────┼────┼────┼────┼────┼────┼────┤")
for i in b:
for j in b:
print("│ "+str(i),j,i and j,i or j, int(not(i and j)), int(not(i or j)),str(nn(i,j,v))+" │ ",sep=" │ ")
print("└────┴────┴────┴────┴────┴────┴────┘")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment