1
0

More than 1 year has passed since last update.

【深層学習】活性化関数ReLUについて

Last updated at Posted at 2021-10-13

深層学習モデルを構築する時、うまく活性化関数を選ぶことは大事です。
その中で、よく使われている活性化関数ReLUについて紹介したいと思います。

ReLUとは?

ReLUはRectified Linear Unitの頭文字から取ってきたものです。
数式を書くと

f(x) = max(x, 0)

数式により、$x$が負数の場合であれば、
$f(x)$は$0$のままということがわかります。

Pythonで書いてみると

def relu(x) :
    return max(x, 0)

ReLUの微分

  • $x$が正数の場合であれば:
\frac{\mathrm{d} f(x)}{\mathrm{d} x}=\frac{\mathrm{d} x}{\mathrm{d} x}=1
  • $x$が$0$以下の場合であれば:
\frac{\mathrm{d} f(x)}{\mathrm{d} x}=\frac{\mathrm{d} 0}{\mathrm{d} x}=0

Pythonのif文で書いてみると

def relu_derivative(x):
    if x > 0:
        return 1
    if x <= 0:
        return 0

ReLUのグラフを作成

import numpy as np
import matplotlib.pyplot as plt

# inputデータ作成
X = np.linspace(-10, 10, 100)

plt.figure(figsize=(8,6))
plt.plot(x, list(map(lambda x: relu(x), X)), label="ReLU")
plt.plot(x, list(map(lambda x: der_relu(x), X)), label="Derivative of ReLU")
plt.title("ReLU")
plt.legend()
plt.show()

截圖 2021-10-13 01.02.14.png
オレンジの線を見ると、
$x>0$の時、微分の数値は$1$で、
$x\le 0$の時、微分の数値は$0$です!

以上、簡単にメモしました。

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0