Ich möchte ein einfaches neuronales Netzwerk erstellen und die ReLU-Funktion verwenden. Kann mir jemand eine Ahnung geben, wie ich die Funktion mit numpy implementieren kann ... Vielen Dank für Ihre Zeit!
Es gibt mehrere Möglichkeiten.
>>> x = np.random.random((3, 2)) - 0.5
>>> x
array([[-0.00590765, 0.18932873],
[-0.32396051, 0.25586596],
[ 0.22358098, 0.02217555]])
>>> np.maximum(x, 0)
array([[ 0. , 0.18932873],
[ 0. , 0.25586596],
[ 0.22358098, 0.02217555]])
>>> x * (x > 0)
array([[-0. , 0.18932873],
[-0. , 0.25586596],
[ 0.22358098, 0.02217555]])
>>> (abs(x) + x) / 2
array([[ 0. , 0.18932873],
[ 0. , 0.25586596],
[ 0.22358098, 0.02217555]])
Wenn die Ergebnisse mit dem folgenden Code zeitgesteuert werden:
import numpy as np
x = np.random.random((5000, 5000)) - 0.5
print("max method:")
%timeit -n10 np.maximum(x, 0)
print("multiplication method:")
%timeit -n10 x * (x > 0)
print("abs method:")
%timeit -n10 (abs(x) + x) / 2
Wir bekommen:
max method:
10 loops, best of 3: 239 ms per loop
multiplication method:
10 loops, best of 3: 145 ms per loop
abs method:
10 loops, best of 3: 288 ms per loop
Die Multiplikation scheint also die schnellste zu sein.
Wenn Sie nichts dagegen haben, dass x
geändert wird, verwenden Sie np.maximum(x, 0, x)
. Darauf wurde von Daniel S hingewiesen. Es ist viel schneller und weil die Leute es vielleicht übersehen, werde ich es als Antwort erneut veröffentlichen. Hier ist der Vergleich:
max method:
10 loops, best of 3: 238 ms per loop
multiplication method:
10 loops, best of 3: 128 ms per loop
abs method:
10 loops, best of 3: 311 ms per loop
in-place max method:
10 loops, best of 3: 38.4 ms per loop
Ich habe mit Numpy eine schnellere Methode für ReLU gefunden. Sie können auch die fantastische Indexfunktion von numpy verwenden.
phantasie-Index:
20,3 ms ± 272 µs pro Schleife (Mittelwert ± Standardabweichung von 7 Durchläufen, jeweils 10 Schleifen)
>>> x = np.random.random((5,5)) - 0.5
>>> x
array([[-0.21444316, -0.05676216, 0.43956365, -0.30788116, -0.19952038],
[-0.43062223, 0.12144647, -0.05698369, -0.32187085, 0.24901568],
[ 0.06785385, -0.43476031, -0.0735933 , 0.3736868 , 0.24832288],
[ 0.47085262, -0.06379623, 0.46904916, -0.29421609, -0.15091168],
[ 0.08381359, -0.25068492, -0.25733763, -0.1852205 , -0.42816953]])
>>> x[x<0]=0
>>> x
array([[ 0. , 0. , 0.43956365, 0. , 0. ],
[ 0. , 0.12144647, 0. , 0. , 0.24901568],
[ 0.06785385, 0. , 0. , 0.3736868 , 0.24832288],
[ 0.47085262, 0. , 0.46904916, 0. , 0. ],
[ 0.08381359, 0. , 0. , 0. , 0. ]])
Hier ist mein Maßstab:
import numpy as np
x = np.random.random((5000, 5000)) - 0.5
print("max method:")
%timeit -n10 np.maximum(x, 0)
print("max inplace method:")
%timeit -n10 np.maximum(x, 0,x)
print("multiplication method:")
%timeit -n10 x * (x > 0)
print("abs method:")
%timeit -n10 (abs(x) + x) / 2
print("fancy index:")
%timeit -n10 x[x<0] =0
max method:
241 ms ± 3.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
max inplace method:
38.5 ms ± 4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
multiplication method:
162 ms ± 3.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
abs method:
181 ms ± 4.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
fancy index:
20.3 ms ± 272 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
Sie können es viel einfacher und ohne Nummer machen:
def ReLU(x):
return x * (x > 0)
def dReLU(x):
return 1. * (x > 0)
Richard Möhns Vergleich ist nicht fair.
Wie Andrea Di Biagios Kommentar ändert die In-Place-Methode np.maximum(x, 0, x)
x in der ersten Schleife.
.__ Hier ist mein Benchmark:
import numpy as np
def baseline():
x = np.random.random((5000, 5000)) - 0.5
return x
def relu_mul():
x = np.random.random((5000, 5000)) - 0.5
out = x * (x > 0)
return out
def relu_max():
x = np.random.random((5000, 5000)) - 0.5
out = np.maximum(x, 0)
return out
def relu_max_inplace():
x = np.random.random((5000, 5000)) - 0.5
np.maximum(x, 0, x)
return x
Timing es:
print("baseline:")
%timeit -n10 baseline()
print("multiplication method:")
%timeit -n10 relu_mul()
print("max method:")
%timeit -n10 relu_max()
print("max inplace method:")
%timeit -n10 relu_max_inplace()
Holen Sie sich die Ergebnisse:
baseline:
10 loops, best of 3: 425 ms per loop
multiplication method:
10 loops, best of 3: 596 ms per loop
max method:
10 loops, best of 3: 682 ms per loop
max inplace method:
10 loops, best of 3: 602 ms per loop
In-Place Maximum-Methode ist nur ein bisschen schneller als die Maximum-Methode. Möglicherweise wird die Zuweisung der Variablen für 'out' weggelassen. Und es ist immer noch langsamer als die Multiplikationsmethode.
Und seitdem Sie die ReLU-Funktion implementieren. Möglicherweise müssen Sie das 'x' durch relu für backprop speichern. Z.B.:
def relu_backward(dout, cache):
x = cache
dx = np.where(x > 0, dout, 0)
return dx
Ich empfehle Ihnen, die Multiplikationsmethode zu verwenden.
numpy hatte nicht die Funktion von relu, aber Sie definieren es selbst wie folgt:
def relu(x):
return np.maximum(0, x)
zum Beispiel:
arr = np.array([[-1,2,3],[1,2,3]])
ret = relu(arr)
print(ret) # print [[0 2 3] [1 2 3]]
Dies ist eine genauere Implementierung:
def ReLU(x):
return abs(x) * (x > 0)
Wenn wir 3 Parameter (t0, a0, a1)
für Relu haben, wollen wir dies implementieren
if x > t0:
x = x * a1
else:
x = x * a0
Wir können den folgenden Code verwenden:
X = X * (X > t0) * a1 + X * (X < t0) * a0
X
gibt es eine Matrix.