▼ 下記ページを理解していること。
[Python] [3] ニューラルネットワークの活性化関数と実装サンプル
▼ Python3.6、NumPyがインストールされていること。
このページでは、venvの仮想環境(Python3.6)上にNumPyをインストールした環境で、Python対話モード(Pythonインタプリタ)にて実装サンプルを記載している。
※ Python対話モード、NumPy、Matplotlibについては下記を参考。
Python対話モード:[Python] 対話モード (インタプリタ) の使用方法
NumPy:[Python] [NumPy] インストールとnumpy.ndarrayの使用方法
Matplotlib:[Python] [Matplotlib] インストールとmatplotlib.pyplot の使用方法
$ python
>>> import numpy as np
>>> import matplotlib.pylab as plt
>>> def step_func(x): # ステップ関数の定義
... return np.array(x > 0, dtype=np.int)
...
>>> x = np.arange(-5.0, 5.0, 0.1) # 区間を-5~5 まで、描画制度を 0.1 刻みに設定
>>> y = step_func(x) # ステップ関数をコール
>>> plt.title("step_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y") # グラフタイトルを設定
Text(0.5, 1.0, 'step_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y')
>>> plt.ylim(-0.1, 1.1) # y軸の範囲を設定
(-0.1, 1.1)
>>> plt.xlabel("x") # x軸のラベルを設定
Text(0.5, 0, 'x')
>>> plt.ylabel("y") # y軸のラベルを設定
Text(0, 0.5, 'y')
>>> plt.plot(x, y) # グラフの描画
[<matplotlib.lines.Line2D object at 0x7fc13041e278>]
>>> plt.savefig('/var/www/vops/ops/macuos/static/macuos/img/b_id41_1.png')) # グラフの出力
$ python
>>> import numpy as np
>>> import matplotlib.pylab as plt
>>> def sigmoid_func(x): # シグモイド関数の定義
... return 1 / (1 + np.exp(-x)) # 自然対数の低 (e) の -x 乗
...
>>> x = np.arange(-5.0, 5.0, 0.1) # 区間を-5~5 まで、描画制度を 0.1 刻みに設定
>>> y = sigmoid_func(x) # シグモイド関数をコール
>>> plt.title("sigmoid_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y") # グラフタイトルを設定
Text(0.5, 1.0, 'sigmoid_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y')
>>> plt.ylim(-0.1, 1.1) # y軸の範囲を設定
(-0.1, 1.1)
>>> plt.xlabel("x") # x軸のラベルを設定
Text(0.5, 0, 'x')
>>> plt.ylabel("y") # y軸のラベルを設定
Text(0, 0.5, 'y')
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x7f727fbc1be0>]
>>> plt.savefig('/var/www/vops/ops/macuos/static/macuos/img/b_id41_2.png') # グラフの出力
$ python
>>> import numpy as np
>>> import matplotlib.pylab as plt
>>> def relu_func(x): # ReLU関数の定義
... return np.maximum(0, x)
...
>>> x = np.arange(-5.0, 5.0, 0.1) # 区間を-5~5 まで、描画制度を 0.1 刻みに設定
>>> y = relu_func(x) # ReLU関数をコール
>>> plt.title("relu_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y") # グラフタイトルを設定
Text(0.5, 1.0, 'relu_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y')
>>> plt.xlabel("x") # x軸のラベルを設定
Text(0.5, 0, 'x')
>>> plt.ylabel("y") # y軸のラベルを設定
Text(0, 0.5, 'y')
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x7fbf1cfeecc0>]
>>> plt.savefig('/var/www/vops/ops/macuos/static/macuos/img/b_id41_3.png')
>>>
$ python
>>> import numpy as np
>>> import matplotlib.pylab as plt
>>> def koutou_func(x): # 恒等関数の定義
... return x
...
>>> x = np.arange(-5.0, 5.0, 0.1) # 区間を-5~5 まで、描画制度を 0.1 刻みに設定
>>> y = koutou_func(x) # 恒等関数をコール
>>> plt.title("koutou_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y") # グラフタイトルを設定
Text(0.5, 1.0, 'koutou_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y')
>>> plt.xlabel("x") # x軸のラベルを設定
Text(0.5, 0, 'x')
>>> plt.ylabel("y") # y軸のラベルを設定
Text(0, 0.5, 'y')
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x7fba4d928f60>]
>>> plt.savefig('/var/www/vops/ops/macuos/static/macuos/img/b_id41_4.png')
>>>
$ python
>>> def softmax_func(x): # ソフトマックス関数の定義
... exp_x = np.exp(x)
... sum_exp_x = np.sum(exp_x)
... y = exp_x / sum_exp_x
... return y
...
>>> x = np.arange(-5.0, 5.0, 0.1) # 区間を-5~5 まで、描画制度を 0.1 刻みに設定
>>> y = koutou_func(x) # ソフトマックス関数をコール
>>> plt.title("softmax_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y") # グラフタイトルを設定
Text(0.5, 1.0, 'softmax_func\n# arange:-5.0, 5.0, 0.1, xlabel:x, ylabel:y')
>>> plt.xlabel("x") # x軸のラベルを設定
Text(0.5, 0, 'x')
>>> plt.ylabel("y") # y軸のラベルを設定
Text(0, 0.5, 'y')
>>> plt.plot(x, y)
[<matplotlib.lines.Line2D object at 0x7ff04a9c4d68>]
>>> plt.savefig('/var/www/vops/ops/macuos/static/macuos/img/b_id41_5.png')
>>>