Pythonで数式を書く_01(シグモイド関数)
数式
以下がシグモイド関数(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))
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 の間になることがわかります。
ちなみに、上のグラフは以下のコードで描くことができます。
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での記述方法を覚えておいてもよいかもしれません。
Author And Source
この問題について(Pythonで数式を書く_01(シグモイド関数)), 我々は、より多くの情報をここで見つけました https://qiita.com/DungBeetle/items/8b450c66494f321f164e著者帰属:元の著者の情報は、元のURLに含まれています。著作権は原作者に属する。
Content is automatically searched and collected through network algorithms . If there is a violation . Please contact us . We will adjust (correct author information ,or delete content ) as soon as possible .