# vprint(i, nn, suffix='a', quit=True) # vprint(i, nn.dropout_masks[1], suffix='m', quit=True) # nn.train(labels[batch_start:batch_end]) # vprint(i, nn, stage='b', quit=True) error += np.sum((labels[batch_start:batch_end] - prediction) ** 2) for k in range(batch_size): correct_cnt += int( np.argmax(prediction[k:k + 1]) == np.argmax(labels[batch_start + k:batch_start + k + 1])) if (j % 10 == 0): test_error = 0.0 test_correct_cnt = 0 for i in range(len(test_images)): tprediction = nn.fire(test_images[i:i + 1]) test_error += np.sum((test_labels[i:i + 1] - tprediction) ** 2) test_correct_cnt += int(np.argmax(tprediction) == np.argmax(test_labels[i:i + 1])) sys.stdout.write( "\n" + "I:" + str(j) + " Test-Err:" + str(test_error / float(len(test_images)))[0:5] + " Test-Acc:" + str(test_correct_cnt / float(len(test_images))) + " Train-Err:" + str(error / float(len(images)))[0:5] + " Train-Acc:" + str(correct_cnt / float(len(images))) )
nn.checkup(inputData, targetData) verbosePrint.vIteration = -1 verbosePrint.stage = '' cycles = 1000 report = cycles / 10 for iteration in range(cycles + 1): vprint(iteration, '~~~~~~~~~~~ Iteration %d ~~~~~~~~~~~' % iteration) combinedError = 0 for row_index in range(len(targetTraining)): datain = inputTraining[row_index:row_index + 1] goal_prediction = targetTraining[row_index:row_index + 1] prediction = nn.fire(datain) # print('Prediction:' + str(prediction)) vprint(iteration, nn) error = (goal_prediction - prediction)**2 combinedError += error nn.learn(datain, goal_prediction) if iteration % report == 0: print('Iteration: %d Error: %s' % (iteration, str(combinedError))) vprint(iteration, '') vprint(iteration, '~~~~~~~~~~~~~~~ End ~~~~~~~~~~~~~~~~') vprint(iteration, nn, quit=True) print()
import numpy as np from nnet import NNet nn = NNet([[[0.1], [0.2], [-0.1]]]) nn.setAlpha(0.01) nn.setVerbose(True) datain = [[8.5, 0.65, 1.2]] goal = [[1]] for i in range(4): output = nn.fire(datain) print('Goal: ' + str(goal)) print(nn) nn.learn(datain, goal)
verbosePrint.vIteration = 0 verbosePrint.stage = '' vp() if demo == 4: # weights = [[[0.1], # [0.2], # [-0.1]]] nn = NNet([[[0.1], [0.2], [-0.1]]]) nn.setAlpha(0.01) datain = np.array([[8.5, 0.65, 1.2]]) goal = np.array([[1]]) for i in range(4): output = nn.fire(datain) print('Goal: ' + str(goal)) print(nn) nn.learn(datain, goal) if demo == 6: nn = NNet([[[0.5], [0.48], [-0.7]]]) nn.setAlpha(0.1) streetlights = np.array([[1, 0, 1], [0, 1, 1], [0, 0, 1], [1, 1, 1], [0, 1, 1], [1, 0, 1]]) walk_vs_stop = np.array([[0, 1, 0, 1, 1, 0]]).T datain = streetlights[0] # [1,0,1] goal_prediction = walk_vs_stop[0] # equals 0... i.e. "stop"
import numpy as np import sys from netExamples.grokking.mnist import \ images, labels, test_images, test_labels from nnet import NNet np.random.seed(1) iterations = 351 nn = NNet(sizes=[784, 40, 10]) nn.setActivations(['relu', 'linear']) nn.setAlpha(0.005) nn.scale(0.1) nn.fire(images[0:1]) nn.checkup(images[0:1], labels[0:1]) # vprint(0, nn) for j in range(iterations): error, correct_cnt = (0.0, 0) for i in range(len(images)): # vprint(i, nn, quit=True) prediction = nn.learn(images[i:i + 1], labels[i:i + 1]) # vprint(i, nn, suffix='b', quit=True) error += np.sum((labels[i:i + 1] - prediction)**2) correct_cnt += int(np.argmax(prediction) == \ np.argmax(labels[i:i + 1])) sys.stdout.write("\r I:" + str(j) + \
[-2.3609116, -2.09156277, -2.44053594, 3.30822731], [-2.53829866, -2.14236091, 0.08922379, -2.74865442], [3.31944907, 0.59780841, -0.14390473, -2.84930608], [0.31776421, -0.58653336, 0.38737669, 0.84336997], [0.05284319, -3.18366088, -3.62821349, -2.60397406], [1.23561288, -2.20991739, -1.82335855, 0.7110058], [-3.77706054, 4.17151034, 2.51407694, 0.5453451], [0.53870221, 0.91373462, 0.71841289, -0.43130623], [-1.39230794, -0.59656183, -3.06075963, -2.33341487], [1.20933192, 1.17955314, -3.29600719, -1.80380998], [-1.96890573, -4.10730626, 3.33177446, 1.91903193], [-0.8858574, 0.05881214, 0.33295324, 0.47227012], [1.68368021, -2.16028071, -0.49761511, -2.0199788], [-1.20030509, 0.26128166, 2.88815594, -1.60763975], [-2.27999708, -1.43537472, -4.40890801, 2.78249789], [-0.54611297, -0.38348355, 0.6507966, -0.13915858], [-0.43032027, 1.99742958, 0.19046502, -1.75774803], [-0.85919565, -1.99212299, 1.90655059, 0.97828721], [0.04083744, -2.04338197, -3.88257489, -1.77983968], [-2.87640294, -2.36045511, -2.19081092, -1.82161738]] wm = WeightMatrix(weights_1_2, bias=True) nn.replaceLayer(1, wm) nn.setActivations(['softMax', 'sigmoid']) inputData = [[0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]] targetData = [[0, 0, 1, 0]] nn.fire(inputData) nn.checkup(inputData, targetData)
" Test-Err:" + str(error / float(len(test_images)))[0:5] + " Test-Acc:" + str(correct_cnt / float(len(test_images))) ) print() if demo == 82: np.random.seed(1) iterations = 351 nn = NNet(sizes=[784, 40, 10]) nn.setActivations(['brelu', 'linear']) nn.setAlpha(0.005) nn.scale(0.1) nn.fire(images[0:1]) vprint(0, nn) for j in range(iterations): error, correct_cnt = (0.0, 0) for i in range(len(images)): vprint(i, nn, quit=True) prediction = nn.learn(images[i:i+1], labels[i:i+1]) vprint(i, nn, suffix='b', quit=True) error += np.sum((labels[i:i+1] - prediction) ** 2) correct_cnt += int(np.argmax(prediction) == \ np.argmax(test_labels[i:i + 1])) sys.stdout.write("\r I:" + str(j) + \ " Train-Err:" + str(error / float(len(images)))[0:5] + \