Skip to content

Instantly share code, notes, and snippets.

@mikezucc
Created October 17, 2017 10:32
Show Gist options
  • Save mikezucc/e0e3ddcb89dcbbdd34d29266698bb20b to your computer and use it in GitHub Desktop.
Save mikezucc/e0e3ddcb89dcbbdd34d29266698bb20b to your computer and use it in GitHub Desktop.
import numpy as np
# ** DATASET **
# https://archive.ics.uci.edu/ml/machine-learning-databases/servo/
# ** DATASET **
# sigmoid function
def nonlin(x,deriv=False):
if(deriv==True):
return x*(1-x)
return 1/(1+np.exp(-x))
inputDat = []
outputDat = []
with open('servo.data.txt','r') as neural_dat:
print "opened the thing"
rowsRaw = neural_dat.read().split('\n')
for rowRaw in rowsRaw:
netInputAndOutput = rowRaw.split(' ')
if len(netInputAndOutput) == 2:
netInputValues = netInputAndOutput[0]
netOutputValues = float(netInputAndOutput[1])/5
colsRaw = np.array([nonlin((ord(x)-65)/5,False) for x in netInputValues.split(',')[0:-1]]) #theres a leftover string I think, hence -1
inputDat.append(colsRaw)
outputDat.append(netOutputValues)
row = 0
for inputSet in inputDat:
print "{}".format(inputSet) + " -> " + "{}".format(outputDat[row])
row = row + 1
print " - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - "
# input dataset
# X = np.array([ [1,0,1],
# [1,1,1],
# [0,0,1],
# [0,1,1]
# ])
#
# # output dataset
# y = np.array([[1,1,0,0]]).T
# seed random numbers to make calculation
# deterministic (just a good practice)
np.random.seed(1)
X = np.array(inputDat)
y = np.array([outputDat]).T
# initialize weights randomly with mean 0
syn0 = 2*np.random.random((4,1)) - 1
for iter in xrange(10000):
# print iter
# forward propagation
l0 = X
l1 = nonlin(np.dot(l0,syn0))
# how much did we miss?
l1_error = y - l1
# multiply how much we missed by the
# slope of the sigmoid at the values in l1
l1_delta = l1_error * nonlin(l1,True)
# update weights
syn0 += np.dot(l0.T,l1_delta)
print "Output After Training:"
# print l1
print syn0
print "{}".format(X[5]) + " should -> " + "{}".format(y[5])
print "{}".format(X[8]) + " feed forward -> " + "{}".format(nonlin(np.dot(X[8],syn0))) # should be around [ 0.5 0.5 0.04742587 0.04742587] -> **0.0712502961**
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment