ニューラルネットワークの活性化関数
ニューラルネットワークの活性化関数のメモです。
目次
ニューラルネットワークにおける活性化関数とは
ニューラルネットワークにおける活性化関数(activation function)とは、ニューロンへの入力の総和に対してそのニューロンがどのように活性化(発火)するかを決める関数です。
いくつか種類があります。
シグモイド関数
入力xに対して、下記のような出力をします。コードと図は標準シグモイド関数の例です。

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5.0, 5.0, 0.1)
y = 1 / (1 + np.exp(-x))
plt.plot(x, y)
plt.ylim(-0.1,1.1)
plt.show()

ステップ関数
入力が0を超えたら1を出力し、それ以外は0を出力します。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5.0, 5.0, 0.1)
y = np.array(x > 0, dtype=np.int)
plt.plot(x, y)
plt.ylim(-0.1,1.1)
plt.show()

ReLU(Rectified Linear Unit)関数
入力が0を超えたらその値を出力し、0以下ならば0を出力します。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5.0, 5.0, 0.1)
y = np.maximum(0, x)
plt.plot(x, y)
plt.ylim(-0.1,5.1)
plt.show()

恒等関数
入力をそのまま出力します。
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5.0, 5.0, 0.1)
y = x
plt.plot(x, y)
plt.ylim(-5.1,5.1)
plt.show()

ソフトマックス関数
その層の全てのニューロンの出力の総和が1になるようにして出力します。

import matplotlib.pyplot as plt
import numpy as np
x = np.arange(-5.0, 5.0, 0.1)
c = np.max(x)
upper = np.exp(x + c)
lower = np.sum(upper)
y = upper / lower
plt.plot(x, y)
plt.ylim(-0.1,0.2)
plt.show()
print(np.sum(y))

公開日
広告