def testLogReg(model, data, activation = sigmoid().h): ''' testing Linear regression''' h = predictLogReg(model, data.X, activation) print 'Testing the hypothesis', list(np.transpose(model).flat) print 'y\t->\tp' for i in range(data.m): print('%d -> %d')%(int(data.y[i]), int(h[i])) cost, _ = logRegCost(data, model, 0.0, activation) print 'total cost is :', cost
def logRegCost(data, theta, regLambda, activation = sigmoid().h): ''' returns the cost and gradient terms of Linear Regression''' theta = np.matrix(theta) h = activation(np.transpose(data.X) * theta) theta2 = np.vstack(([[0]],theta[1:])) J = -1.0 / data.m * (np.transpose(data.y)* np.log(h) + \ (1 - np.transpose(data.y)) * np.log(1 - h)) + \ 0.5 * regLambda / data.m * np.transpose(theta2) * theta2 grad = 1.0 / data.m * (data.X * (h - data.y)) + \ (regLambda / data.m * theta2) return float(J[0][0]), grad
def trainOneVsAllGD(data, act = sigmoid().h, epochs = 10000, lr = 0.5): data.addBiasRow() theta_init = np.matrix(np.zeros((data.n, 1))) from functools import partial from copy import deepcopy model= np.matrix(np.zeros((data.n, data.K))) J = np.matrix(np.zeros((data.K, epochs))) for k in range(data.K): d2 = deepcopy(data) d2.y = (data.y == k) cost = partial(logRegCost,data = d2,theta = theta_init, \ regLambda = 0.001, activation = act) J[k, :], model[:, k] = gradDesc(cost, theta_init, epochs, lr) return model, J
def __init__(self, arch, activation = sigmoid()): # define activation and derivative of activation functions self.h, self.dh = activation.h, activation.dh # Architecture, i.e. number of units/neurons in each layer self.arch = arch # Number of Layers in the Network self.L = len(arch) # List of Weight Matrices, initialize each with small random numbers Wt = [] for l in range(1, self.L): #Wl = np.matrix([[gauss(0, math.sqrt(6.0 / (arch[l] * arch[l - 1]))) \ # for i in xrange(arch[l])] for j in range(arch[l - 1] + 1)]) epsilon = math.sqrt(6.0 / (arch[l] * arch[l - 1])) Wl = np.random.random((arch[l - 1] + 1, arch[l])) - 0.5 Wt.append(epsilon * Wl) self.W = self.unrollWt(Wt)
def linear_activation_forward(A_prev, W, b, activation, annealing): """ A_prev -- activations from previous layer or input data -- (size of previous layer, number of examples) W -- weights matrix -- (size of l, size of l-1) b -- bias vector -- (size of l, 1) activation -- the activation to be used in this layer, either "sigmoid" or "relu" A -- the output of the activation function cache -- a tuple containing "linear_cache" and "activation_cache" for backprop """ if activation == AcvtivationFunc.sigmoid: Z, linear_cache = linear_forward(A_prev, W, b) A, activation_cache = sigmoid(Z, annealing) elif activation == AcvtivationFunc.relu: Z, linear_cache = linear_forward(A_prev, W, b) A, activation_cache = relu(Z) assert (A.shape == (W.shape[0], A_prev.shape[1])) cache = (linear_cache, activation_cache) return A, cache
def predictMultiple(model, X, act = sigmoid().h): return np.argmax(act(np.transpose(X) * model), axis = 1)
def predictLogReg(model, X, activation = sigmoid().h): return activation(np.transpose(X) * model) > 0.5
[[1, 1, 0], [3]], [[1, 1, 1], [2]], ] pat1 = [ [[0, 0, 0], [0]], [[0, 0, 1], [1]], [[0, 1, 0], [1]], [[0, 1, 1], [0]], [[1, 0, 0], [1]], [[1, 0, 1], [0]], [[1, 1, 0], [0]], [[1, 1, 1], [1]], ] ActSig = sigmoid(4) # ActSig.view() d1 = Data() d1.loadList(pat1) d2 = Data() d2.loadList(pat2, 4) n2 = ANN([3, 3, 4], ActSig) # n2.displaySynpWt() arch1 = [3, 4, 1] n1 = ANN(arch1, ActSig) # n1.displaySynpWt() ##########################################################
# and function of 3 variables pat1 = [[[0, 0, 0], [0]], [[0, 0, 1], [1]], [[0, 1, 0], [1]], [[0, 1, 1], [1]], [[1, 0, 0], [2]], [[1, 0, 1], [3]], [[1, 1, 0], [3]], [[1, 1, 1], [3]], ] d1 = Data() d1.loadList(pat1, numClasses = 4) #print d1.y act = sigmoid().h # our activation function is simgmoid model, J = trainOneVsAllGD(d1, act,epochs = 5000, lr = 0.25) #print d1.y plt.plot(np.transpose(J)) plt.show() print predictMultiple(model, d1.X, act) # d1.addBiasRow() # theta_init = np.matrix(np.zeros((d1.n, 1))) # # cost = partial(logRegCost,data = d1,theta = theta_init, \ # regLambda = 0.001, activation = act)