[PYTHON] 4 [/] Vier Arithmetik durch maschinelles Lernen

english_la/b.png

Ich dachte über das Perzeptron der Teilung in Form von tiefem Lernen nach. I considered a division perceptron in the form of deep learning.

# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

#Initial value
#number of learning
N = 1000
#layer
layer = [2, 2, 1]
#bias
#bias = [0.0, 0.0]
#learning rate
η = [0.001, 0.001]
#η = [0.000001, 0.000001]
#number of middle layers
H = len(η) - 1
#teacher value
t = [None for _ in range(N)]
#function output value
f_out = [[None for _ in range(H + 1)] for _ in range(N)]
#function input value
f_in = [[None for _ in range(H + 1)] for _ in range(N)]
#weight
w = [[None for _ in range(H + 1)] for _ in range(N + 1)]
for h in range(H + 1):   
    w[0][h] = np.random.uniform(-1.0, 1.0, (layer[h + 1], layer[h]))

for h in range(H + 1):
    print(w[0][h])
    
#squared error
dE = [None for _ in range(N)]
#∂E/∂IN
δ = [[None for _ in range(H + 1)] for _ in range(N)]

#Learning
for n in range(N):

    #input value
    f_out[n][0] = np.random.uniform(-10.0, 10.0, (layer[0]))
    
    #teacher value
    t[n] = f_out[n][0][0] / f_out[n][0][1]
    
    #order propagation
    f_in[n][0] = np.dot(w[n][0], f_out[n][0])
    f_out[n][1] = np.log(f_in[n][0]*f_in[n][0])
    f_in[n][1] = np.dot(w[n][1], f_out[n][1])

    #output value
    div = np.exp(f_in[n][1])

    #squared error
    dE[n] = div - t[n]#value after squared error differentiation due to omission of calculation

    #δ
    δ[n][1] = div * dE[n]
    δ[n][0] = (2.0 / f_in[n][0]) * np.dot(w[n][1].T, δ[n][1])
    
    #back propagation
    for h in range(H + 1):
        w[n + 1][h] = w[n][h] - η[h] * np.real(δ[n][h].reshape(len(δ[n][h]), 1) * f_out[n][h])
        

#Output
#Weight
for h in range(H + 1):
    print(w[N][h])
#figure
#area height
py = np.amax(layer)
#area width
px = (H + 1) * 2
#area size
plt.figure(figsize = (16, 9))
#horizontal axis
x = np.arange(0, N + 1, 1)
#drawing
for h in range(H + 1):
    for l in range(layer[h + 1]):
        #area matrix
        plt.subplot(py, px, px * l + h * 2 + 1)
        for m in range(layer[h]):                       
            #line
            plt.plot(x, np.array([w[n][h][l, m] for n in range(N + 1)]), label = "w[" + str(h) + "][" + str(l) + "," + str(m) + "]")        
        #grid line
        plt.grid(True)
        #legend
        plt.legend(bbox_to_anchor = (1, 1), loc = 'upper left', borderaxespad = 0, fontsize = 10)

#save
plt.savefig('graph_div.png') 
#show
plt.show()

Zeigt das Konzept der Gewichte.\\
I\ indicate\ the\ concept\ of\ weight.\\
w[0]=
\begin{pmatrix}
△ & □\\
▲ & ■
\end{pmatrix},
w[1]=
\begin{pmatrix}
〇 & ●
\end{pmatrix}\\
 \\
Eingabewert und w[0]Produkt von\\
multiplication\ of\ input\ value\ and\ w[0]\\
\begin{pmatrix}
△ & □\\
▲ & ■
\end{pmatrix}
\begin{pmatrix}
a\\
b
\end{pmatrix}\\
=
\begin{pmatrix}
△a+□b\\
▲a+■b
\end{pmatrix}\\
 \\
Eingabe der ersten Ebene\\
enter\ in\ the\ first\ layer\\
\begin{pmatrix}
log(△a+□b)^2\\
log(▲a+■b)^2
\end{pmatrix}\\
 \\
Die wahre Zahl wird quadriert, um der negativen Zahl zu entsprechen.\\
The\ exact\ number\ is\ squared\ to\ accommodate\ negative\ numbers.\\
 \\
1. Schicht Ausgabe und w[1]Produkt von\\
product\ of\ first\ layer\ output\ and\ w[1]\\
\begin{align}
\begin{pmatrix}
〇 & ●
\end{pmatrix}
\begin{pmatrix}
log(△a+□b)^2\\
log(▲a+■b)^2
\end{pmatrix}
=&〇log(△a+□b)^2+●log(▲a+■b)^2\\
=&log(△a+□b)^{2〇}-log(▲a+■b)^{-2●}\\
=&log\frac{(△a+□b)^{2〇}}{(▲a+■b)^{-2●}}\\
\end{align}\\
 \\
Ausgabeebene\\
enter\ in\ the\ output\ layer\\
e^{log\frac{(△a+□b)^{2〇}}{(▲a+■b)^{-2●}}}=\frac{(△a+□b)^{2〇}}{(▲a+■b)^{-2●}}\\
 \\
\left\{
\begin{array}{l}
△=1,□=0,〇=0.5 \\
▲=0,■=1,●=-0.5
\end{array}
\right.\\
 \\
\frac{a}{b}\\
 \\
Im einfachsten Fall, wenn die obigen Bedingungen erfüllt sind, ist der Quotient a/Sie können b ausgeben.\\
In\ the\ simplest\ case,\ if\ the\ above\ conditions\ are\ met,\ quotient\ a/b\ can\ be\ output.\\


Der Anfangswert ist zufällig(-1.0~1.0)Nachdem ich mich entschieden hatte, versuchte ich zu sehen, ob es zum Zielwert konvergieren würde, wenn das Lernen wiederholt würde.\\
After\ deciding\ the\ initial\ value\ between\ random\ numbers\ (-1.0~1.0),\\
I\ tried\ to\ repeat\ the\ learning\ to\ converge\ to\ the\ target\ value.\\
 \\
Zielwert\\
Target\ value\\
w[0]=
\begin{pmatrix}
△ & □\\
▲ & ■
\end{pmatrix}
,w[1]=
\begin{pmatrix}
○ & ●
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=1,□=0,〇=0.5 \\
▲=0,■=1,●=-0.5
\end{array}
\right.\\
 \\
Ursprünglicher Wert\\
Initial\ value\\
w[0]=
\begin{pmatrix}
-0.18845444 & -0.56031414\\
-0.48188658 & 0.6470921
\end{pmatrix}
,w[1]=
\begin{pmatrix}
0.80395641 & 0.80365676
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=-0.18845444,□=-0.56031414,〇=0.80395641 \\
▲=-0.48188658,■=0.6470921,●=0.80365676
\end{array}
\right.\\
 \\
Berechnete Werte\\
Calculated\ value\\
w[0]=
\begin{pmatrix}
14601870.60282903 & -14866110.02378938\\
13556781.27758209 & -13802110.45958244
\end{pmatrix}
,w[1]=
\begin{pmatrix}
-1522732.53915774 & -6080851.59710287
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=14601870.60282903,□=-14866110.02378938,〇=-1522732.53915774 \\
▲=13556781.27758209,■=-13802110.45958244,●=-6080851.59710287
\end{array}
\right.\\

graph_div.png


Es ist ein Misserfolg. Egal wie oft Sie es tun, das Gewicht wird zu einem lächerlichen Wert abweichen.\\
Ich habe nach der Ursache gesucht.\\
It\ is\ a\ failure.\\
No\ matter\ how\ many\ times\ I\ do,\ the\ weights\ will\ diverge\ to\ ridiculous\ values.\\
I\ investigated\ the\ cause.\\
 \\
Mit der Kettenregel der Fehlerrückausbreitung\\
In\ chain\ rule\ of\ the\ backpropagation\\
(log(x^2))'=\frac{2}{x}\\
\lim_{x \to ±∞} \frac{2}{x}=0\\
 \\
(e^x)'=e^x\\
\lim_{x \to -∞} e^x=0\\
Es stellte sich heraus, dass bei Verwendung eines extrem großen Werts der Gradient verschwindet.\\
It\ was\ found\ that\ such\ an\ extremely\ large\ value\ would\ cause\ the\ gradient\ to\ disappear.\\
 \\
Ich überlegte.\\
I reconsidered.

商ver2.png

# coding=utf-8
import numpy as np
import matplotlib.pyplot as plt

#Initial value
#number of learning
N = 200000
#layer
layer = [2, 2, 1]
#bias
#bias = [0.0, 0.0]
#learning rate
η = [0.1, 0.1]
#η = [0.000001, 0.000001]
#clip value
#clip = 709
clip = 700
#number of middle layers
H = len(η) - 1
#teacher value
t = [None for _ in range(N)]
#function output value
f_out = [[None for _ in range(H + 1)] for _ in range(N)]
#function input value
f_in = [[None for _ in range(H + 1)] for _ in range(N)]
#weight
w = [[None for _ in range(H + 1)] for _ in range(N + 1)]
for h in range(H):   
    w[0][h] = np.random.uniform(-1.0, 1.0, (layer[h + 1], layer[h]))
w[0][H] = np.zeros((layer[H + 1], layer[H]))

for h in range(H + 1):
    print(w[0][h])
    
#squared error
dE = [None for _ in range(N)]
#∂E/∂IN
δ = [[None for _ in range(H + 1)] for _ in range(N)]

#Learning
for n in range(N):

    #input value
    t[n] = clip
    while np.abs(t[n]) > np.log(np.log(clip)):#Gradient vanishing problem Measure
        f_out[n][0] = np.random.uniform(0.0, 10.0, (layer[0]))
        f_out[n][0] = np.array(f_out[n][0], dtype=np.complex)
    
        #teacher value
        t[n] = f_out[n][0][0] / f_out[n][0][1]
    
    #order propagation
    f_in[n][0] = np.dot(w[n][0], f_out[n][0])    
    f_out[n][1] = np.log(f_in[n][0])    
    f_in[n][1] = np.dot(w[n][1], f_out[n][1])
    
    #output value
    div = np.exp(f_in[n][1])
    
    #squared error
    dE[n] = np.real(div - t[n])#value after squared error differentiation due to omission of calculation
    dE[n] = np.clip(dE[n], -clip, clip)
    dE[n] = np.nan_to_num(dE[n])

    #δ
    δ[n][1] = np.real(div * dE[n])
    δ[n][1] = np.clip(δ[n][1], -clip, clip)
    δ[n][1] = np.nan_to_num(δ[n][1])
    
    
    δ[n][0] = np.real((1.0 / f_in[n][0]) * np.dot(w[n][1].T, δ[n][1]))
    δ[n][0] = np.clip(δ[n][0], -clip, clip)  
    δ[n][0] = np.nan_to_num(δ[n][0]) 
    
    #back propagation
    for h in range(H + 1):
        #Gradient vanishing problem Measure
        # a*10^b a part only
        w10_u = np.real(δ[n][h].reshape(len(δ[n][h]), 1) * f_out[n][h])
        w10_u = np.clip(w10_u, -clip, clip)  
        w10_u = np.nan_to_num(w10_u)        
        w10_d = np.where(
            w10_u != 0.0,
            np.modf(np.log10(np.abs(w10_u)))[1],
            0.0
        )
        #Decimal not supported
        w10_d = np.clip(w10_d, 0.0, clip)
        
        w[n + 1][h] = w[n][h] - η[h] * (w10_u / np.power(10.0, w10_d))

#Output
#Weight
for h in range(H + 1):
    print(w[N][h])
#figure
#area height
py = np.amax(layer)
#area width
px = (H + 1) * 2
#area size
plt.figure(figsize = (16, 9))
#horizontal axis
x = np.arange(0, N + 1, 1) #0 bis N.+Bis zu 1 in 1 Schritten
#drawing
for h in range(H + 1):
    for l in range(layer[h + 1]):
        #area matrix
        plt.subplot(py, px, px * l + h * 2 + 1)
        for m in range(layer[h]):                       
            #line
            plt.plot(x, np.array([w[n][h][l, m] for n in range(N + 1)]), label = "w[" + str(h) + "][" + str(l) + "," + str(m) + "]")        
        #grid line
        plt.grid(True)
        #legend
        plt.legend(bbox_to_anchor = (1, 1), loc = 'upper left', borderaxespad = 0, fontsize = 10)

#save
plt.savefig('graph_div.png') 
#show
plt.show()

Als Gegenmaßnahme

graph_div.png


Zielwert\\
Target\ value\\
w[0]=
\begin{pmatrix}
△ & □\\
▲ & ■
\end{pmatrix}
,w[1]=
\begin{pmatrix}
○ & ●
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=1,□=0,〇=1 \\
▲=0,■=1,●=-1
\end{array}
\right.\\
 \\
Ursprünglicher Wert\\
Initial value\\
w[0]=
\begin{pmatrix}
-0.12716087 & 0.34977234\\
0.85436489 & 0.65970844
\end{pmatrix}
,w[1]=
\begin{pmatrix}
0.0 & 0.0
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=-0.12716087,□=0.34977234,〇=0.0 \\
▲=0.85436489,■=0.65970844,●=0.0
\end{array}
\right.\\
 \\
Berechnete Werte\\
Calculated\ value\\
w[0]=
\begin{pmatrix}
-1.71228449e-08 & 1.00525062e+00\\
1.00525061e+00 & -4.72288257e-09
\end{pmatrix}
,w[1]=
\begin{pmatrix}
-0.99999998 & 0.99999998
\end{pmatrix}\\
\left\{
\begin{array}{l}
△=-1.71228449e-08,□=1.00525062e+00,〇=-0.99999998\\
▲=1.00525061e+00,■=-4.72288257e-09,●=0.99999998
\end{array}
\right.\\
 \\
Erfolgreich. Die Werte von △ □ und ▲ ■ sind umgekehrt.\\
Ich mag es nicht auf eine Weise, die die richtige Antwort hat und nah dran ist.\\
Trotzdem loggen Sie sich höchstens ein, um die Teilung zu lehren,exp,Mit komplexen Zahlen\\
Ich war in Schwierigkeiten, weil ich mich auf die Mathematik der High School ausdehnen musste.\\
 \\
Succeeded.\ The\ values\ of\ △□\ and\ ▲■\ are\ reversed.\\
I\ don't\ like\ it\ in\ the\ way\ that\ I\ get\ it\ right.\\
Even\ so,\ at\ the\ very\ least\ trying\ to\ teach\ division\\
log,\ exp,\ complex\ numbers\\
I\ had\ trouble\ expanding\ to\ high\ school\ mathematics.\\

Recommended Posts

4 [/] Vier Arithmetik durch maschinelles Lernen
Maschinelles Lernen
Zusammenfassung des maschinellen Lernens von Python-Anfängern
Kontrolle eines Sandwich-Mannes durch maschinelles Lernen ver4
[Fehler] Finde Maki Horikita durch maschinelles Lernen
[Memo] Maschinelles Lernen
Python & maschinelles Lernen Lernnotiz Machine: Maschinelles Lernen durch Rückausbreitung
Beispiel für maschinelles Lernen
Klassifizierung von Gitarrenbildern durch maschinelles Lernen Teil 1
Klassifizieren Sie Informationen zum maschinellen Lernen nach Themenmodellen
Analyse der gemeinsamen Raumnutzung durch maschinelles Lernen
[Übersetzung] scikit-learn 0.18 Einführung in maschinelles Lernen durch Tutorial scikit-learn
Angemessene Preisschätzung von Mercari durch maschinelles Lernen
Klassifizierung von Gitarrenbildern durch maschinelles Lernen Teil 2
Geschichte rund um die Datenanalyse durch maschinelles Lernen
Zusammenfassung des Lernprogramms für maschinelles Lernen
Maschinelles Lernen ⑤ AdaBoost-Zusammenfassung
Maschinelles Lernen: Betreut --AdaBoost
Maschinelles Lernen unterstützt Vektormaschine
Machine Sommelier von Keras-
Maschinelles Lernen studieren ~ matplotlib ~
Lineare Regression des maschinellen Lernens
Memo zum Kurs für maschinelles Lernen
Bibliothek für maschinelles Lernen dlib
Maschinelles Lernen (TensorFlow) + Lotto 6
Lerne irgendwie maschinelles Lernen
Bibliothek für maschinelles Lernen Shogun
Maschinelles Lernen Kaninchen Herausforderung
Einführung in das maschinelle Lernen
Maschinelles Lernen: k-Nächste Nachbarn
Was ist maschinelles Lernen?
Python-Lernnotiz für maschinelles Lernen von Chainer aus Kapitel 2
Python-Lernnotiz für maschinelles Lernen von Chainer Kapitel 1 und 2
Modell des maschinellen Lernens unter Berücksichtigung der Wartbarkeit
Japanische Vorverarbeitung für maschinelles Lernen
Maschinelles Lernen in Delemas (Praxis)
Vier Regeln für Python
Eine Einführung in das maschinelle Lernen
Techniken im Zusammenhang mit maschinellem Lernen / Klassifizierung
Maschinelles Lernen: Überwacht - Lineare Regression
Grundlagen des maschinellen Lernens (Denkmal)
Anfänger des maschinellen Lernens versuchten RBM
Vorhersage des Vorhandenseins oder Nichtvorhandenseins von Untreue durch maschinelles Lernen
[Lernen stärken] Tracking durch Multi-Agent
[Maschinelles Lernen] Zufällige Gesamtstruktur verstehen
Maschinelles Lernen mit Python! Vorbereitung
Maschinelles Lernen ② Naive Bayes Zusammenfassung
Verstehe maschinelles Lernen ~ Ridge Regression ~.
Über maschinelles Lernen gemischte Matrix
Maschinelles Lernen: Überwacht - Zufälliger Wald
Python-Lernnotiz für maschinelles Lernen von Chainer Kapitel 7 Regressionsanalyse
Praktisches Memo zum maschinellen Lernsystem
Maschinelles Lernen Minesweeper mit PyTorch
Erstellen Sie eine maschinelle Lernumgebung
Python Machine Learning Programming> Schlüsselwörter
Algorithmus für maschinelles Lernen (einfaches Perzeptron)
Identifizieren Sie t + pazolite-Songs durch maschinelles Lernen (NNC-Challenge-Entwicklung)
Wird in EDA für maschinelles Lernen verwendet
Bedeutung von Datensätzen für maschinelles Lernen