In [1]:
import numpy as np
import math
import FitVec as fv

In [2]:
X = np.array([[0,0,1],[0,1,1],[1,0,1],[1,1,1]])
y = np.array([[0, 1, 1, 0]]).T
init_theta = 10*(np.random.random((13,1)) - 0.5) 

def runForward(X, theta):
	theta1 = np.array(theta[:9]).reshape(3,3)
	theta2 = np.array(theta[9:]).reshape(4,1)
	h1 = sigmoid(np.dot(X, theta1)) 
	h1_bias = np.insert(h1, 3, [1,1,1,1], axis=1)
	output = sigmoid(np.dot(h1_bias, theta2)) 
	return output
	#4x3 * 3x1 = 4x1
def costFunction(X, y, theta):
	m = float(len(X))
	hThetaX = np.array(runForward(X, theta))
	return np.sum(np.abs(y - hThetaX))
def sigmoid(x): return 1 / (1 + np.exp(- x))

def demoRun():
	print("Random theta: \n%s\n" % (np.round(runForward(X, init_theta), 2)))
	print("Cost: %s\n" % (costFunction(X,y, init_theta)))
	optimal_theta = fv.evolveParams(costFunction, 13, (100,0.01,50), X, y)
	print("Optimal theta: \n%s\n" % (np.round(runForward(X, optimal_theta.reshape(13,1)), 2)))
	print("Cost: %s\n" % (costFunction(X, y, optimal_theta.reshape(13,1))))
demoRun()


Random theta: 
[[ 0.95]
 [ 0.69]
 [ 0.95]
 [ 0.87]]

Cost: 2.18251894031

Optimal theta: 
[[ 0.]
 [ 1.]
 [ 1.]
 [ 0.]]

Cost: 0.00075230763025


In [ ]: