前提・実現したいこと
MNISTで60000個の訓練データと10000個のテストデータを用いて、
ソフトマックス回帰を実装したいです。
ただしkerasはデータの読み込み以外に使わず、
ネットワーク自体は書きたいです(大学の課題なので)
以下のコードを実行しても、エラーは出ませんが計算が2時間以上終わりません。
以下のコードにまずい点はあるでしょうか?
該当のソースコード
python3
1import numpy as np 2from keras.datasets import mnist # データの読み込み 3from keras.utils import np_utils # 1-of-K符号化法 4from scipy.optimize import minimize # 検算用 5 6(x_train, y_train), (x_test, y_test) = mnist.load_data() 7 8x_train = x_train.reshape(60000, 784) 9x_train = x_train.astype('float32') 10x_train = x_train / 255 11num_classes = 10 12y_train = np_utils.to_categorical(y_train, num_classes) 13 14x_test = x_test.reshape(10000, 784) 15x_test = x_test.astype('float32') 16x_test = x_test / 255 17y_test = np_utils.to_categorical(y_test, num_classes) 18 19 20# softmax関数 21def logistic(x, w): 22 w = w.reshape((10, 785)) # (クラス数 = 10, 次元数 + 1 = 785) 23 x = np.c_[x, np.ones((60000, 1))] # x_trainの最後列に1を追加する 24 y = np.exp(np.dot(x, w.T)) # y = exp(線形和) 25 26 wk = np.sum(y, axis = 1) 27 wk = y.T / wk 28 y = wk.T 29 return y 30 31# 交差エントロピー誤差 32def cee_logistic(w, x, t): 33 y = logistic(x, w) 34 y = np.log(y) # 先にlogを取っておく 35 N = y.shape[0] 36 37 cee = 0 38 for n in range(N): 39 cee = cee - np.dot(t[n, :], y[n, :]) 40 41 cee = cee / N 42 return cee 43 44# 交差エントロピー誤差の微分 45def dcee_logistic(w, x, t): 46 y = logistic(x, w) 47 N, K = y.shape 48 x = np.c_[x, np.ones((60000, 1))] # x_trainの最後列に1を追加する 49 50 dcee = np.zeros((10, 785)) #交差エントロピー誤差を重みで微分した行列 51 for k in range(K): 52 wk = y[:, k] - t[:, k] 53 wk = np.dot(x.T ,wk) 54 dcee[k, :] = wk.T 55 56 dcee = dcee / N 57 return dcee.reshape(-1) 58 59# 最適化 60def fit_logistic(w_init, x, t): 61 res = minimize(fun = cee_logistic, x0 = w_init, args = (x, t), jac = dcee_logistic, method = "CG") 62 return res.x 63 64W_init = np.zeros((10, 785)) 65W = fit_logistic(W_init, x_train, y_train) 66print(np.round(W.reshape(10, 785), 2))
あなたの回答
tips
プレビュー