Skip to content

[Machine Learning] ReLU 函式介紹與程式實作

ReLU

ReLU (Rectified Linear Unit) 函式,常被翻譯為『修正線性單元』,是一種神經網路當中常用的 Activation function。被認為具有一定程度的生物原理。(雖然我並不清楚是什麼原理。)

以下是 ReLU 的公式:

為了驗證這個公式,我寫了個 Python 的小程式來畫圖。

# coding: utf-8
import math
import matplotlib.pyplot as plt


def relu(x):
    if x < 0: return 0
    else: return x


def plot(px, py):
    plt.plot(px, py)
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data',0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data',0))
    plt.show()


def main():
    # Init
    x = []
    dx = -20
    while dx <= 20:
        x.append(dx)
        dx += 0.1

    # px and py
    px = [xv for xv in x]
    py = [relu(xv) for xv in x]

    # Plot
    plot(px, py)


if __name__ == "__main__":
    main()


Output:

可以任意調整 x 輸入的範圍。
我們可以看到,在 x < 0 的部份,輸出一直都是 0。


Leaky ReLU

Leaky ReLU function 是一種 ReLU 的變種。如果說 ReLU function 是將所有的負值設為 0,那麼 Leaky ReLU 便是將負值乘上一個大於 0 的斜率。(其實也有小於 0 的情況?雖然我沒看過就是了。)

公式:

以下我再次寫了個小程式, a 值我固定為 0.07 討個幸運:

# coding: utf-8
import math
import matplotlib.pyplot as plt


def leaky_relu(x, a):
    if x < 0: return a*x
    else: return x


def plot(px, py):
    plt.plot(px, py)
    ax = plt.gca()
    ax.spines['right'].set_color('none')
    ax.spines['top'].set_color('none')
    ax.xaxis.set_ticks_position('bottom')
    ax.spines['bottom'].set_position(('data',0))
    ax.yaxis.set_ticks_position('left')
    ax.spines['left'].set_position(('data',0))
    plt.show()


def main():
    # Init
    a = 0.07
    x = []
    dx = -20
    while dx <= 20:
        x.append(dx)
        dx += 0.1

    # px and py
    px = [xv for xv in x]
    py = [leaky_relu(xv, a) for xv in x]

    # Plot
    plot(px, py)


if __name__ == "__main__":
    main()



Output:

可以看到與原本的 ReLU 不同的左側。


應用

  • Keras 和 PyTorch 裡頭都有這個函數可以直接調用。
  • 由於線性導致計算速度很快
  • 收斂速度很快
  • 輸入是負數時,若是 Learning Rate 過大,可能會有問題

References


Read More

Leave a Reply