4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

シグモイド関数を調べる(仮)

Last updated at Posted at 2018-06-18

 Pythonでシグモイド関数
$$ f(x) = \frac{1}{1+\exp (-x)} $$
について調べます。

 はじめにNumPyで実装しプロットしてみます。

%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

def sigmoid(x):
    return 1 / (1 + np.exp(-x))

x = np.linspace(-5.0, 5.0, 100)
y = sigmoid(x)
plt.plot(x,y)
plt.show()

output_3_0.png

0から1の間で単調に増加する形状になります。

SymPyでシグモイド関数を調べる

 関数の形を理解するためには微分をする必要があります。SymPyを使うと解析解を求めつつ簡単にグラフ化できます。

%matplotlib inline
import matplotlib.pyplot as plt
import sympy as sym
sym.init_printing()

Jupyter notebookで数式を表示させるために、sym.init_printing()を実行します。シグモイド関数をプロット後、1次導関数、2次導関数を求めます。

x, y, dy, ddy = sym.symbols('x y dy ddy')
y = 1 / (1 + sym.exp(-x))

sym.plotting.plot(y, (x, -5, 5))

output_9_0.png

##1次導関数

dy = sym.diff(y, x, 1)
dy

$$\frac{e^{- x}}{\left(1 + e^{- x}\right)^{2}}$$

sym.plotting.plot(dy, (x, -5, 5))

output_11_0.png

##2次導関数

ddy = sym.diff(y, x, 2)
ddy

$$\frac{e^{- x}}{\left(1 + e^{- x}\right)^{2}} \left(-1 + \frac{2 e^{- x}}{1 + e^{- x}}\right)$$

sym.plotting.plot(ddy, (x, -5, 5))

output_13_0.png

4
3
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
4
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?