活性化関数
1:ステップ関数 サカズキ(赤犬)
特徴
閾値を越えたらオン、そうでなければオフ。白黒はっきり。
import numpy as np
import matplotlib.pyplot as plt
###### ステップ関数
def step_function(x):
# 0 以下の場合は 0 を返し、それ以外は 1を返す
return np.where(x<=0, 0, 1)
### 実行
x = np.linspace(-5, 5)
y = step_function(x)
plt.plot(x, y)
plt.show()
2:シグモイド関数 チョッパー
特徴
0〜1の間でなめらかに変化。弱い入力でもちょっと反応する。
import numpy as np
import matplotlib.pyplot as plt
###### シグモイド関数
def sigmoid_function(x):
return 1/(1+np.exp(x)) # exp => ネイピア数の累乗を表す
### 実行 ###
x = np.linspace(-5, 5)
y = sigmoid_function(x)
plt.plot(x, y)
plt.show()
3:tanh(双曲線正接)ゾロ
特徴
-1〜1の間で出力。シグモイドを強調したような「プラスにもマイナスにも反応」する。
極端。
import numpy as np
import matplotlib.pyplot as plt
####### tanh
### -1 から 1 の間を滑らかに変化する関数
def tanh_function(x):
return np.tanh(x)
### 実行 ###
x = np.linspace(-5, 5)
y = tanh_function(x)
plt.plot(x, y)
plt.show()
4:ReLU(Rectified Linear Unit) ルフィ
特徴
0以下は全部0、0を超えたら直線的に伸びる。シンプルで強力。
import numpy as np
import matplotlib.pyplot as plt
def relu_function(x):
return np.where(x <= 0, 0, x)
### 実行 ###
x = np.linspace(-5, 5)
y = relu_function(x)
plt.plot(x, y)
plt.show()
5:恒等関数(Identity Function) ブルック
特徴
入力 = 出力。加工なし。
import numpy as np
import matplotlib.pyplot as plt
def relu_function(x):
return np.where(x <= 0, 0, x)
### 実行 ###
x = np.linspace(-5, 5)
y = relu_function(x)
plt.plot(x, y)
plt.show()
6:ソフトマックス関数(Softmax) ナミ
特徴
: 出力を「確率」に変換し、全部で1になるように分配。多クラス分類で必須。
import numpy as np
def softmax_function(x):
return np.exp(x) / np.sum(np.exp(x)) # ソフトマックス関数
### 実行
y = softmax_function(np.array([1,2,3]))
print(y)
出力結果
.txt
[0.09003057 0.24472847 0.66524096]