LoginSignup
0
1

More than 3 years have passed since last update.

Pythonで数式を書く_01(シグモイド関数)

Last updated at Posted at 2021-02-20

数式

以下がシグモイド関数(sigmoid function)です。

y\ = \frac{1}{1+e^{-ax}} \ \ (a>0)

a = 1 のときは標準シグモイド関数(standard sigmoid function)と呼びます。
今回はPythonで a = 1 のときのシグモイド関数を記述します。
※ちなみに、本やネットを見ていると、y は h(x) やς(x) と表記していたりもします。

Pythonで記述

import numpy as np

# シグモイド関数
y = 1 / (1 + np.exp(-x))

これがPythonで記述したときのシグモイド関数です。
x = 1 のときは以下のように出力されます。

import numpy as np

x = 1
y = 1 / (1 + np.exp(-x))
print(y)
x=1の出力結果
0.7310585786300049

コードを省略しますが、x = -1 や x = 0 のときは以下のように出力されます。

x=-1の出力結果
0.5
x=0の出力結果
0.2689414213699951

シグモイド関数を視覚化

先ほどの入力と出力を表にまとめると次のようになります。

x = -1 x = 0 x = 1
出力結果 y 0.27 0.5 0.73

表だけだとわかりにくいですが、
グラフにするとシグモイド関数の出力は 0 ~ 1 の間になることがわかります。
シグモイド関数.jpeg

ちなみに、上のグラフは以下のコードで描くことができます。

import matplotlib.pyplot as plt
import numpy as np

x = np.arange(-15, 15, 0.1)
y = 1 / (1 + np.exp(-x))

plt.plot(x, y)
plt.title("standard sigmoid function")
plt.xlabel("x")
plt.ylabel("y")
plt.show()

シグモイド関数はニューラルネットワークの活性化関数としても利用されますので、
その方面に興味のある方はシグモイド関数の数式とPythonでの記述方法を覚えておいてもよいかもしれません。

0
1
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
0
1