ステップ関数

ステップ関数の数式

ここではステップ関数についてお伝えします。
ステップ関数は入力が未満だったら0。
入力が0以上であれば1を返す関数です。
以下の数式をご覧ください。
これをプログラム化するイメージ湧きますか?
私は全然イメージできませんでしたw

ステップ関数のサンプルプログラム

安心してください。
世の中には沢山のサンプルがありました。
その中で、3つのサンプルプログラムを用意しました。
どれも結果は同じステップ関数になります。

今後のことを考えると、NumPyに対応した。
3番目の実装が良いと思われます。

def  step_function(x):
    if x > 0:
        return  1
    else
        return  0
def  step_function(x):
    return  1  *  (x  >  0)
import  numpy  as  np
def  step_function(x):
    y  =  x  >  0
    return  y.astype(np.int)

ステップ関数のグラフ

最後にグラフがどのように表示されるのかをご紹介します。
このグラフは、以下のプログラムを実行した結果です。

前述したように、0以上は1を返していることがわかります。
プログラムはトリッキーですが・・・
このサンプルプログラムは
有名な技術書からの引用になります。
詳細な説明は本を買ってくださいw
(良書でした。とても解りやすいです)

import  matplotlib.pyplot  as  plt
import  numpy  as  np
def  step_function(x):
    return  np.array(x  >  0,  dtype=np.int)
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()
引用元

オライリーJapan 斎藤康毅 著 「セロから作るDeep Learning」

シェアする

  • このエントリーをはてなブックマークに追加

フォローする

This website stores cookies on your computer. These cookies are used to provide a more personalized experience and to track your whereabouts around our website in compliance with the European General Data Protection Regulation. If you decide to to opt-out of any future tracking, a cookie will be setup in your browser to remember this choice for one year.

Accept or Deny