質問編集履歴
1
修正
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,6 +24,28 @@
|
|
24
24
|
|
25
25
|
```python
|
26
26
|
|
27
|
+
def leaky_relu(x, alpha=0.2):
|
28
|
+
|
29
|
+
with tf.variable_scope('LeakyReLU'):
|
30
|
+
|
31
|
+
alpha = tf.constant(alpha, dtype=x.dtype, name='alpha')
|
32
|
+
|
33
|
+
@tf.custom_gradient
|
34
|
+
|
35
|
+
def func(x):
|
36
|
+
|
37
|
+
y = tf.maximum(x, x * alpha)
|
38
|
+
|
39
|
+
@tf.custom_gradient
|
40
|
+
|
41
|
+
def grad(dy):
|
42
|
+
|
27
|
-
dx = tf.where(y >= 0, dy, dy * alpha)
|
43
|
+
dx = tf.where(y >= 0, dy, dy * alpha)
|
44
|
+
|
45
|
+
return dx, lambda ddx: tf.where(y >= 0, ddx, ddx * alpha)
|
46
|
+
|
47
|
+
return y, grad
|
48
|
+
|
49
|
+
return func(x)
|
28
50
|
|
29
51
|
```
|