Created
November 13, 2017 00:46
-
-
Save Nydhal/1985b1d04bc23cb725a3c8eef52a5670 to your computer and use it in GitHub Desktop.
Simple perceptron neural net to test boolean functions (CSE471).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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