3
0

More than 1 year has passed since last update.

活性化関数まとめ

Last updated at Posted at 2023-07-02
  • ステップ関数

数式 $$f(x)=
\begin{cases}
0, (x < 0) \
1, (x\geqq0)\
\end{cases}
$$

実装とグラフ

import numpy as np
import matplotlib.pyplot as plt

# ステップ関数
def step_function(x):
  return np.array(x > 0, dtype=np.int64)
  
x = np.arange(-5.0, 5.0, 0.1)
y = step_function(x)
  
plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()

Pasted image 20230702214418.png

  • シグモイド関数

数式
$$f(x)=\frac{1}{1+\exp(-x)}$$

実装とグラフ

# シグモイド関数
def sigmoid(x):
 return 1 / (1 + np.exp(-x))


x = np.arange(-5.0, 5.0, 0.1)
y = sigmoid(x)

plt.plot(x, y)
plt.ylim(-0.1, 1.1)
plt.show()

Pasted image 20230702221824.png

  • ReLU関数(ランプ関数)

数式
$$
f(x)=
\begin{cases}
0, (x \leqq 0) \
x, (x > 0)
\end{cases}
$$

実装とグラフ

# ReLU関数
def relu(x):
 return np.maximum(0, x)


x = np.arange(-5.0, 5.0, 0.1)
y = relu(x)

plt.plot(x, y)
plt.ylim(-0.1, 5.0)
plt.show()

Pasted image 20230702222601.png

  • 恒等関数

あらゆる入力値を、全く同じ数値に変換して(=そのまま)出力する関数。

$$f(x)=x$$
実装とグラフ

# 恒等関数
def identity_function(x):
 return x

x = np.arange(-5.0, 5.0, 0.1)
y = identity_function(x)

plt.plot(x, y)
plt.ylim(-5.0, 5.0)
plt.show()

Pasted image 20230702223824.png

  • ソフトマックス関数


$$f_i(x)=\frac{exp({x_i})}{\sum _{k=1}^n exp({x_k})}$$
実装(グラフは省略)

def softmax(a):
 c = np.max(a)
 exp_a = np.exp(a-c)
 sum_exp_a = np.sum(exp_a)
 y = exp_a / sum_exp_a
 return y
3
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
3
0