def trainNN(X,T,W1i,W2i,Niter,eta): trainData = np.concatenate((X,T),axis=1) W1 = W1i W2 = W2i print("Orignal weight matrices: " + str(W1) + '\n' + str(W2)) for i in range(Niter): for data in trainData: dEn_w1, dEn_w2, y = myBackprop(data[0,0:2].T,np.matrix(data[0,2]),W1,W2) W1 = W1 - eta * dEn_w1 W2 = W2 - eta * dEn_w2 print("Weight matrices after " + str(Niter) + " iterations of training: " + str(W1) + '\n' + str(W2)) return [W1, W2]
def trainNN(X, T, W1i, W2i, Niter, eta): trainData = np.concatenate((X, T), axis=1) W1 = W1i W2 = W2i print("Orignal weight matrices: " + str(W1) + '\n' + str(W2)) for i in range(Niter): for data in trainData: dEn_w1, dEn_w2, y = myBackprop(data[0, 0:2].T, np.matrix(data[0, 2]), W1, W2) W1 = W1 - eta * dEn_w1 W2 = W2 - eta * dEn_w2 print("Weight matrices after " + str(Niter) + " iterations of training: " + str(W1) + '\n' + str(W2)) return [W1, W2]
return np.matrix(array) USAGE = """USAGE: tester.py "input pattern" "weight matrix 1" "weight matrix 2" Example: tester.py x W1 W2 Where x is a Dx1 dimensional vector W1 is Dx(Mh+1) dimensional matrix W2 is (Mh+1)xMo dimensional matrix """ if len(sys.argv) != 4: #print(USAGE) exit(1) script, x_input, W1_input, W2_input = sys.argv x = np.matrix(np.loadtxt(x_input)) W1 = np.matrix(np.loadtxt(W1_input)) W2 = np.matrix(np.loadtxt(W2_input)) y,z,a = ffnn(x.T,W1,W2) print("Network output: " + str(y) +'\n'+ "Hidden layer input: " + str(a) + '\n'+"Hidden layer output: " + str(z)) print("This configuration has " + str(np.shape(W2)[1]) + " neurons in the hidden layer") t = 1 # Synthesizing the target value so that we get some error when running the script :) dEn_w1, dEn_w2, y = myBackprop(x.T,t,W1,W2)
return np.matrix(array) USAGE = """USAGE: tester.py "input pattern" "weight matrix 1" "weight matrix 2" Example: tester.py x W1 W2 Where x is a Dx1 dimensional vector W1 is Dx(Mh+1) dimensional matrix W2 is (Mh+1)xMo dimensional matrix """ if len(sys.argv) != 4: #print(USAGE) exit(1) script, x_input, W1_input, W2_input = sys.argv x = np.matrix(np.loadtxt(x_input)) W1 = np.matrix(np.loadtxt(W1_input)) W2 = np.matrix(np.loadtxt(W2_input)) y, z, a = ffnn(x.T, W1, W2) print("Network output: " + str(y) + '\n' + "Hidden layer input: " + str(a) + '\n' + "Hidden layer output: " + str(z)) print("This configuration has " + str(np.shape(W2)[1]) + " neurons in the hidden layer") t = 1 # Synthesizing the target value so that we get some error when running the script :) dEn_w1, dEn_w2, y = myBackprop(x.T, t, W1, W2)