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() nn.checkup(inputData, targetData)
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)
np.random.seed(1) batch_size = 100 iterations = 301 nn = NNet(sizes=[784, 100, 10], batch_size=batch_size) nn.setActivations(['brelu', 'linear']) nn.setMaskPr({1: 2}) nn.setAlpha(0.001) nn.scale(0.1) for j in range(iterations): error, correct_cnt = (0.0, 0) for i in range(int(len(images) / batch_size)): batch_start, batch_end = ((i * batch_size), ((i + 1) * batch_size)) prediction = nn.learn(images[batch_start:batch_end], labels[batch_start:batch_end]) # 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
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) + \ " Train-Err:" + str(error / float(len(images)))[0:5] + \ " Train-Acc:" + str(correct_cnt / float(len(images)))) if (j % 10 == 0): test_error = 0.0 test_correct_cnt = 0 for i in range(len(test_images)):