Skip to content

Instantly share code, notes, and snippets.

@harujoh
Created March 19, 2016 15:57
Show Gist options
  • Save harujoh/65b2568acb011a8a9128 to your computer and use it in GitHub Desktop.
Save harujoh/65b2568acb011a8a9128 to your computer and use it in GitHub Desktop.
import numpy as np
class MLP (object):
"""
3 Layered Perceptron
"""
def __init__( self, n_input_units , n_hidden_units, n_output_units):
self.nin = n_input_units
self.nhid = n_hidden_units
self.nout = n_output_units
self.v = np .random.uniform(-1.0, 1.0, ( self.nhid, self .nin+1))
self.w = np .random.uniform(-1.0, 1.0, ( self.nout, self .nhid+1))
def fit( self, inputs , targets, learning_rate=0.8, epochs =10000):
inputs = self .__add_bias(inputs, axis=1)
targets = np .array(targets)
for loop_cnt in range(epochs):
# randomise the order of the inputs
p = np.random .randint(inputs.shape[0])
xp = inputs[p]
bkp = targets[p]
# forward phase
gjp = self.__sigmoid(np .dot(self.v, xp))
gjp = self.__add_bias(gjp)
gkp = self.__sigmoid(np .dot(self.w, gjp))
# backward phase(back prop)
eps2 = self.__sigmoid_deriv(gkp) * (gkp - bkp)
eps = self.__sigmoid_deriv(gjp) * np .dot(self.w.T, eps2)
gjp = np.atleast_2d(gjp)
eps2 = np.atleast_2d(eps2)
self.w = self .w - learning_rate * np.dot(eps2.T, gjp)
xp = np.atleast_2d(xp)
eps = np.atleast_2d(eps)
self.v = self .v - learning_rate * np.dot(eps.T, xp)[1:, :]
def __add_bias( self, x , axis=None):
return np .insert(x, 0, 1, axis= axis)
def __sigmoid( self, u ):
""" Sigmoid function(Activation function) """
return (1.0 / (1.0 + np .exp(-u)))
def __sigmoid_deriv( self, u ):
return (u * (1 - u))
def predict( self, i ):
i=self .__add_bias(i)
gjp = self.__sigmoid(np .dot(self.v, i))
gjp = self.__add_bias(gjp)
return self .__sigmoid(np.dot( self.w, gjp))
# initialize
mlp = MLP(n_input_units=2, n_hidden_units=2, n_output_units=1)
inputs = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
targets = np.array([0, 1, 1, 0])
# training
mlp.fit(inputs, targets)
# predict
print ("--- predict ---")
for i in [[0, 0], [0, 1], [1, 0], [1, 1]]:
print (i, mlp.predict(i))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment