βshortの自堕落Diary

web関係や、プログラミングなどを扱う予定です。プランなど立てていないので、不定期投稿になります。

ステップ関数とシグモイド関数

ステップ関数

入力が0を超えたら1を出力し、それ以外は0を出力する関数。
単純に

def step_function(x):
    if x > 0:
        return 1
    else:
        return 0

と記述できる。

シグモイド関数


h(x)=\frac{1}{1+e^{-x}}

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

それぞれのグラフ比較

import numpy as np
import matplotlib.pylab as plt

#ステップ関数
def step_function(x):
    return np.array(x > 0, dtype = np.int)

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

x = np.arange(-5.0, 5.0, 0.1)
y_step = step_function(x)
y_sigmoid = sigmoid(x)

#グラフの生成
plt.plot(x, y_step, linestyle = "--", label = "step")
plt.plot(x, y_sigmoid, label = "sigmoid")
plt.ylim(-0.1, 1.1)
plt.xlabel("x")
plt.ylabel("y")
plt.title('step & sigmoid')
plt.legend()
plt.show()

f:id:weblog2016it:20170715224612p:plain

参考


Pythonからはじめる数学入門


ゼロから作るDeep Learning ―Pythonで学ぶディープラーニングの理論と実装