Lösung: Gradientenabstieg mit mehreren Ein- und Ausgaben
Completion requirements
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import numpy as np #toes %win fans weights = np.array([[0.1 ,0.1, -0.3], # hurt? [0.1, 0.2, 0], # win? [0.0, 1.3, 0.1]]).T # sad? toes = np.array([0.85, 0.95, 0.9, 0.9]) # normalized data wlrec = np.array([0.65, 0.8, 0.8, 0.9]) nfans = np.array([1.2, 1.3, 0.5, 1.0]) # Input is here only the first game of the season. input = np.array([toes[0],wlrec[0],nfans[0]]) hurt = [0.1, 0.0, 0.0, 0.1] win = [1, 1, 0, 1] sad = [0.1, 0.0, 0.1, 0.2] # Check here only the first game true = [hurt[0], win[0], sad[0]] alpha = 0.1 # learning rate def neural_network(input, weights): pred = input.dot(weights) # vector matrix product -> 3 dot product (dt. Skalarprodukt) return pred for i in range(3): # 3 iterations pred = neural_network(input,weights) delta = pred - true # one delta per output node weight_deltas = input * delta # but three weight_deltas (weighted by input) print(f'{i}. loop: weights={np.array2string(weights, precision=4)} pred={np.array2string(pred, precision=4)} delta={np.array2string(delta, precision=4)} -> weight_deltas={np.array2string(weight_deltas, precision=5)}') weights = weights - alpha * weight_deltas # 3 new weights |
Ausgabe (etwas übersichtlicher dargestellt):
0. loop: weights=[[ 0.1 0.1 0. ] [ 0.1 0.2 1.3] [-0.3 0. 0.1]] pred=[-0.21 0.215 0.965] delta=[-0.31 -0.785 0.865] -> weight_deltas=[-0.2635 -0.51025 1.038 ] 1. loop: weights=[[ 0.1264 0.151 -0.1038] [ 0.1264 0.251 1.1962] [-0.2737 0.051 -0.0038]] pred=[-0.1389 0.3528 0.6847] delta=[-0.2389 -0.6472 0.5847] -> weight_deltas=[-0.20303 -0.4207 0.70169] 2. loop: weights=[[ 0.1467 0.1931 -0.174 ] [ 0.1467 0.2931 1.126 ] [-0.2533 0.0931 -0.074 ]] pred=[-0.084 0.4664 0.4953] delta=[-0.184 -0.5336 0.3953] -> weight_deltas=[-0.15643 -0.34687 0.47434]
Last modified: Tuesday, 1 November 2022, 1:00 PM