def bonus(): perceptron1 = Perceptron(1,1,1) perceptron1.network = [Neuron((0,0), 0, False)] print(testSaveAndLoad(perceptron1)) perceptron1.network = [Neuron((0,0), 0, True)] print(testSaveAndLoad(perceptron1)) perceptron1.network = [Neuron((0,0), 0, True)] print(testSaveAndLoad(perceptron1)) perceptron1.network = [Neuron((0,0), 1, True)] print(testSaveAndLoad(perceptron1)) perceptron2 = Perceptron(2,2,2) perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)] print(testSaveAndLoad(perceptron2)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, False)] print(testSaveAndLoad(perceptron2)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)] print(testSaveAndLoad(perceptron2)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)] print(testSaveAndLoad(perceptron2)) perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 2, True)] print(testSaveAndLoad(perceptron2)) perceptron2.network = [Neuron((0,0), 0, True), Neuron((1,1), 1, True)] print(testSaveAndLoad(perceptron2))
def exercise3(): print("\nExercise 3: Back propagation\n") perceptron1 = Perceptron(1,1,1) perceptron1.network = [Neuron((0,0), 0, False)] expectedNetwork = [Neuron((0,0), 0, False)] print(testBackProp(perceptron1, expectedNetwork, 1, 0)) perceptron1.network = [Neuron((0,0), 0, True)] expectedNetwork = [Neuron((0,0), 1, False)] print(testBackProp(perceptron1, expectedNetwork, 1, 0)) perceptron1.network = [Neuron((0,0), 0, True)] expectedNetwork = [Neuron((0,0), -1, False)] print(testBackProp(perceptron1, expectedNetwork, -1, 0)) perceptron1.network = [Neuron((0,0), 1, True)] expectedNetwork = [Neuron((0,0), 0, False)] print(testBackProp(perceptron1, expectedNetwork, -1, 1)) perceptron2 = Perceptron(2,2,2) perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)] expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)] print(testBackProp(perceptron2, expectedNetwork, -1, 0)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, False)] expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 1, False)] print(testBackProp(perceptron2, expectedNetwork, -1, -1)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)] expectedNetwork = [Neuron((0,0), -2, False), Neuron((1,1), 0, False)] print(testBackProp(perceptron2, expectedNetwork, -1, 0)) perceptron2.network = [Neuron((0,0), -1, True), Neuron((1,1), 1, True)] expectedNetwork = [Neuron((0,0), 0, False), Neuron((1,1), 2, False)] print(testBackProp(perceptron2, expectedNetwork, 1, 0)) perceptron2.network = [Neuron((0,0), -1, False), Neuron((1,1), 2, True)] expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 2, False)] print(testBackProp(perceptron2, expectedNetwork, 1, 1)) perceptron2.network = [Neuron((0,0), 0, True), Neuron((1,1), 1, True)] expectedNetwork = [Neuron((0,0), -1, False), Neuron((1,1), 0, False)] print(testBackProp(perceptron2, expectedNetwork, -1, 1))
def testForwardPass(network, image, expected): imageHeight, imageWidth = image.shape[:2] perceptron = Perceptron(len(network), imageWidth, imageHeight) perceptron.network = network result = perceptron.forwardPass(image) outString = ("Testing with image:\n{}\nnetwork={}: ".format(image, network)) if result != expected: outString += ("{}KO| Expected {} got {}{}").format(Fore.RED, expected, result, Fore.RESET) return outString outString += "{}OK{}".format(Fore.GREEN, Fore.RESET) return outString