目次
▼ 下記ページを理解していること。
[Python] [8] 損失関数 (2 乗和誤差、交差エントロピー誤差) と実装サンプル
▼ Python3.6、NumPyがインストールされていること。
このページでは、venvの仮想環境(Python3.6)上にNumPyをインストールした環境で、Python対話モード(Pythonインタプリタ)にて実装サンプルを記載している。
※ Python対話モード、NumPyについては下記を参考。
Python対話モード:[Python] 対話モード (インタプリタ) の使用方法
NumPy:[Python] [NumPy] インストールとnumpy.ndarrayの使用方法
$ cd gitlocalrep # ローカルのGitリポジトリに移動
$ cd deep-learning-from-scratch/ch03 # Git (deep-learning-from-scratch) のカレントディレクトに移動に移動
$ python
>>> import sys, os
>>> sys.path.append(os.pardir)
>>> import numpy as np
>>> from dataset.mnist import load_mnist
>>>
>>> (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)
>>>
>>> print(x_train.shape) # 詳細は下記 ※ 2 に記載
(60000, 784)
>>> print(t_train.shape) # 詳細は下記 ※ 3 に記載
(60000, 10)
>>>
※ 1 load_mnist関数の引数
引数 normalize は、入力画像を 0.0 ~ 1.0 に 正規化するかどうか をBool 値で設定する。
Falseの場合、入力画像のピクセルは 0 ~ 255 となる。
引数 flatten は、入力画像を 1次元にするかどうか をBool 値で設定する。
Falseの場合、入力画像は1 * 28 * 28 の3次元配列として格納され、Trueの場合、1次元配列(要素:784)として格納される。
引数 one_hot_labelは、ラベルを one_hot 表現で格納するかどうか をBool 値で設定する。
one_hot 表現の場合は、正解となるラベルのみ 1 でそれ以外は、0 の配列となる。
戻り値は、(訓練画像、訓練ラベル), (テスト画像, テストラベル) の形式でMNISTデータを返する。
※ 2 x_train.shape (形状)
784列(= 28 × 28)の画像データが学習用データセット数の 60,000枚 あることを表している。
※ 3 t_train.shape (形状)
10列(正解となるラベルのみ 1 でそれ以外は、0 の配列)の教師データが学習用データセット数の 60,000個 あることを表している。
$ cd gitlocalrep # ローカルのGitリポジトリに移動
$ cd deep-learning-from-scratch/ch03 # Git (deep-learning-from-scratch) のカレントディレクトに移動に移動
$ python
>>> import sys, os
>>> sys.path.append(os.pardir)
>>> import numpy as np
>>> import pickle
>>> from dataset.mnist import load_mnist
>>> from common.functions import sigmoid, softmax
>>>
>>> def init_network():
... with open("sample_weight.pkl", 'rb') as f:
... network = pickle.load(f)
... return network
...
>>> def predict(network, x):
... W1, W2, W3 = network['W1'], network['W2'], network['W3']
... b1, b2, b3 = network['b1'], network['b2'], network['b3']
... a1 = np.dot(x, W1) + b1
... z1 = sigmoid(a1)
... a2 = np.dot(z1, W2) + b2
... z2 = sigmoid(a2)
... a3 = np.dot(z2, W3) + b3
... y = softmax(a3)
... return y
...
>>>
>>> def cross_entropy_error(y, t):
... if y.ndim == 1: # 次元が 1 の場合
... t = t.reshape(1, t.size)
... y = y.reshape(1, y.size)
... batch_size = y.shape[0]
... return -np.sum(t * np.log(y + 1e-7)) / batch_size
...
>>>
>>> (x_train, t_train), (x_test, t_test) = load_mnist(normalize=True, one_hot_label=True)
>>>
>>> train_size = x_train.shape[0]
>>> batch_size = 100
>>> batch_mask = np.random.choice(train_size, batch_size)
>>> x_batch = x_train[batch_mask]
>>> t_batch = t_train[batch_mask]
>>>
>>> print(batch_mask) # np.random.choiceの結果
[ 2759 48331 20881 29315 30035 55711 47969 1338 54067 23424 14789 9722
38601 10138 24036 23811 284 43467 41042 39683 49572 20247 29728 23176
50987 4855 43468 7179 2815 29033 46578 25623 41615 34833 12651 35969
51498 34685 30303 57205 16641 39057 45010 35152 19620 34228 55637 44070
25063 14112 45717 32403 32209 26388 27572 53492 46367 15161 38462 26947
30193 45931 25658 24854 33528 41892 55989 32053 43699 22615 42090 3430
1568 57173 35969 11839 26384 16123 31217 30323 46844 37015 28731 46525
15412 19736 16773 12655 37365 52095 11550 46947 34077 31528 9691 44021
6473 41599 7001 4999]
>>>
>>> network = init_network()
>>> y_batch = predict(network, x_batch)
>>>
>>> cross_entropy_error(y_batch, t_batch)
0.20627920610480943
>>>
>>> def cross_entropy_error(y, t):
... if y.ndim == 1:
... t = t.reshape(1, t.size)
... y = y.reshape(1, y.size)
... batch_size = y.shape[0]
... return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size
...
>>>
... return -np.sum(t * np.log(y + 1e-7)) / batch_size
... return -np.sum(np.log(y[np.arange(batch_size), t] + 1e-7)) / batch_size