Mehrere Gewichte gleichzeitig "erlernen"
Completion requirements
Generalisierung des Gradientenabstiegs
Der Gradientenabstieg wird auf folgendes Szenario mit mehreren Eingabepunkten übertragen:

Mehrere Eingaben: Deltas der Gewichte berechnen und Vorhersagen machen
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 |
import numpy as np weights = np.array([0.1, 0.2, -0.1]) def neural_network(input, weights): pred = input.dot(weights) # replaces w_sum(a,b) return pred # first 4 games of season toes = np.array([8.5, 9.5, 9.9, 9.0]) wlrec = np.array([0.65, 0.8, 0.8, 0.9]) nfans = np.array([1.2, 1.3, 0.5, 1.0]) # Input is the first game of the season. input = np.array([toes[0],wlrec[0],nfans[0]]) # first game: 1 => win win_or_lose_binary = [1, 1, 0, 1] alpha = 0.01 # Reduce learning rate to avoid overshooting the target for i in range(3): pred = neural_network(input,weights) delta = pred - win_or_lose_binary[0] # one delta weight_deltas = input * delta # but three weight_deltas print(f'{i}. loop: weights={np.array2string(weights, precision=4)} pred={pred:.4} delta={delta:.4} -> weight_deltas={np.array2string(weight_deltas, precision=5)}') weights = weights - alpha * weight_deltas # 3 new weights |
Ausgabe (etwas übersichtlicher eingerückt):
0. loop: weights=[ 0.1 0.2 -0.1] pred=0.86 delta=-0.14 -> weight_deltas=[-1.19 -0.091 -0.168]
1. loop: weights=[ 0.1119 0.2009 -0.0983] pred=0.9638 delta=-0.03624 -> weight_deltas=[-0.30806 -0.02356 -0.04349]
2. loop: weights=[ 0.115 0.2011 -0.0979] pred=0.9906 delta=-0.009382 -> weight_deltas=[-0.07975 -0.0061 -0.01126]
Iterativ wird die Differenz delta aus Voraussage und erwartetem Wert
delta = pred - win_or_lose_binary[0] # one deltamit jedem
input multipliziert,
um diese Differenzen unterschiedlich zu gewichten:weight_deltas = input * delta # but three weight_deltas (input is a numpy array)
Damit werden dann die Gewichte weights entsprechend individuell korrigiert:
weights = weights - alpha * weight_deltas # 3 new weight
Dabei reduziert alpha die Lernrate, um nicht über das Ziel hinauszuschießen.
Last modified: Monday, 3 October 2022, 2:49 PM