- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
745 :デフォルトの名無しさん[]:2017/06/12(月) 10:46:14.31 ID:yuw+moiO - 斎藤の本
p.94 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])) / batch_size これ間違っていますね。 y.ndim == 1 のときに、 cross_entropy_error(np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], 3) みたいな使い方ができないはずです。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
746 :デフォルトの名無しさん[]:2017/06/12(月) 10:50:06.62 ID:yuw+moiO - cross_entropy_error(np.array([0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1], np.array([3]))
とすればOKですが、不自然です。 (x_train, t_train), (x_test, t_test) = load_mnist(normalize = True, one_hot_label = False) とすると、 t_train は、 [[3], [1], [4], [1], [5], …] ではなく、 [3, 1, 4, 1, 5, …] みたいなデータになるからです。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
747 :デフォルトの名無しさん[]:2017/06/12(月) 10:52:56.23 ID:yuw+moiO - それと、p.91では、使っていた delta をp.94では忘れていますね。
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
345 :デフォルトの名無しさん[]:2017/06/12(月) 13:19:36.59 ID:yuw+moiO - 斎藤康毅のディープラーニングの本に意味が分からないコードがありました。
p.111 def f(W): ■■■■return net.loss(x, t) net は simpleNet というクラスのインスタンスです。 このコードがさっぱり分かりません。 W がなぜ引数にあるのでしょうか?
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
750 :デフォルトの名無しさん[]:2017/06/12(月) 14:21:36.69 ID:yuw+moiO - 斎藤康毅のディープラーニングの本に意味が分からないコードがありました。
p.111 def f(W): ■■■■return net.loss(x, t) net は simpleNet というクラスのインスタンスです。 このコードがさっぱり分かりません。 W がなぜ引数にあるのでしょうか?
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
752 :デフォルトの名無しさん[]:2017/06/12(月) 14:51:29.93 ID:yuw+moiO - def softmax(x):
■■■■if x.ndim == 2: ■■■■■■■■x = x.T ■■■■■■■■x = x - np.max(x, axis=0) ■■■■■■■■y = np.exp(x) / np.sum(np.exp(x), axis=0) ■■■■■■■■return y.T ■■■■x = x - np.max(x) # オーバーフロー対策 ■■■■return np.exp(x) / np.sum(np.exp(x)) def cross_entropy_error(y, t): ■■■■if y.ndim == 1: ■■■■■■■■t = t.reshape(1, t.size) ■■■■■■■■y = y.reshape(1, y.size) ■■■■■■■■ ■■■■# 教師データがone-hot-vectorの場合、正解ラベルのインデックスに変換 ■■■■if t.size == y.size: ■■■■■■■■t = t.argmax(axis=1) ■■■■■■■■■■■■ ■■■■batch_size = y.shape[0] ■■■■return -np.sum(np.log(y[np.arange(batch_size), t])) / batch_size
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
753 :デフォルトの名無しさん[]:2017/06/12(月) 14:52:31.32 ID:yuw+moiO - def numerical_gradient(f, x):
■■■■h = 1e-4 # 0.0001 ■■■■grad = np.zeros_like(x) ■■■■ ■■■■it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) ■■■■while not it.finished: ■■■■■■■■idx = it.multi_index ■■■■■■■■tmp_val = x[idx] ■■■■■■■■x[idx] = float(tmp_val) + h ■■■■■■■■fxh1 = f(x) # f(x+h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val - h ■■■■■■■■fxh2 = f(x) # f(x-h) ■■■■■■■■grad[idx] = (fxh1 - fxh2) / (2*h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val # 値を元に戻す ■■■■■■■■it.iternext() ■■■■■■■■ ■■■■return grad
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
754 :デフォルトの名無しさん[]:2017/06/12(月) 14:52:48.36 ID:yuw+moiO - class simpleNet:
■■■■def __init__(self): ■■■■■■■■self.W = np.random.randn(2,3) #標準正規分布による 2x3 の行列 ■■■■def predict(self, x): ■■■■■■■■return np.dot(x, self.W) ■■■■def loss(self, x, t): ■■■■■■■■z = self.predict(x) ■■■■■■■■y = softmax(z) ■■■■■■■■loss = cross_entropy_error(y, t) ■■■■■■■■return loss
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
755 :デフォルトの名無しさん[]:2017/06/12(月) 14:54:09.40 ID:yuw+moiO - x = np.array([0.6, 0.9])
t = np.array([0, 0, 1]) net = simpleNet() f = lambda w: net.loss(x, t) dW = numerical_gradient(f, net.W) print(dW) ★★★★★★★★★★★★★ ★↑の f が理解できません。★ ★★★★★★★★★★★★★
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
756 :デフォルトの名無しさん[]:2017/06/12(月) 14:57:27.95 ID:yuw+moiO - まず、
仮引数の w が使われていません。 仮引数に渡していない、 net, x, t を使っているところもよくわかりません。 グローバル変数ということでしょうか? その関数 f を numerical_gradient に渡しています。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
757 :デフォルトの名無しさん[]:2017/06/12(月) 15:03:35.02 ID:yuw+moiO - スコープについて正確に理解していないからよく理解できないのかもしれません。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
758 :デフォルトの名無しさん[]:2017/06/12(月) 15:05:03.31 ID:yuw+moiO - numerical_gradient 内のこれ↓ですが、 x はダミーの引数ですね。
fxh1 = f(x) # f(x+h)
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
759 :デフォルトの名無しさん[]:2017/06/12(月) 15:07:56.32 ID:yuw+moiO - グローバル変数の net というのは、関数の中で呼び出されている関数の中でも
使えるんですか?
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
760 :デフォルトの名無しさん[]:2017/06/12(月) 15:09:49.85 ID:yuw+moiO - あ、できますね↓
glvar = "abc" def myfunc1(): ■■■■myfunc2() def myfunc2(): ■■■■print(glvar) myfunc1()
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
761 :デフォルトの名無しさん[]:2017/06/12(月) 15:11:05.87 ID:yuw+moiO - しかし、この斎藤っていう人、こんあひどいコードをよく恥ずかしげもなく公開できますね。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
762 :デフォルトの名無しさん[]:2017/06/12(月) 15:11:58.06 ID:yuw+moiO - なんでこの斎藤っていう人の本は高評価なんですか?
こんなひどいコード見たことがないです。正直言って。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
763 :デフォルトの名無しさん[]:2017/06/12(月) 15:12:31.37 ID:yuw+moiO - 意図的に人を混乱に陥れようとしているかのようです。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
764 :デフォルトの名無しさん[]:2017/06/12(月) 15:22:22.17 ID:yuw+moiO - def f(W):
■■■■return cross_entropy_error(softmax(np.dot(x, W)), t) ↑こう書けば、x, t がグローバル変数ですが、理解可能だったと思います。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
765 :デフォルトの名無しさん[]:2017/06/12(月) 15:37:37.83 ID:yuw+moiO - もっと先でどうなっているのか知りませんけど、
コードの再利用を絶対しなければならないとかいう強迫観念があるかのようですね。
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
346 :デフォルトの名無しさん[]:2017/06/12(月) 18:38:21.92 ID:yuw+moiO - ↓このプログラムですが、ひどすぎないですか?
斎藤康毅のディープラーニングの本のコードです。 def softmax(x): ■■■■if x.ndim == 2: ■■■■■■■■x = x.T ■■■■■■■■x = x - np.max(x, axis=0) ■■■■■■■■y = np.exp(x) / np.sum(np.exp(x), axis=0) ■■■■■■■■return y.T ■■■■x = x - np.max(x) # オーバーフロー対策 ■■■■return np.exp(x) / np.sum(np.exp(x)) def cross_entropy_error(y, t): ■■■■if y.ndim == 1: ■■■■■■■■t = t.reshape(1, t.size) ■■■■■■■■y = y.reshape(1, y.size) ■■■■■■■■ ■■■■# 教師データがone-hot-vectorの場合、正解ラベルのインデックスに変換 ■■■■if t.size == y.size: ■■■■■■■■t = t.argmax(axis=1) ■■■■■■■■■■■■ ■■■■batch_size = y.shape[0] ■■■■return -np.sum(np.log(y[np.arange(batch_size), t])) / batch_size
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
768 :デフォルトの名無しさん[]:2017/06/12(月) 18:38:50.85 ID:yuw+moiO - def numerical_gradient(f, x):
■■■■h = 1e-4 # 0.0001 ■■■■grad = np.zeros_like(x) ■■■■ ■■■■it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) ■■■■while not it.finished: ■■■■■■■■idx = it.multi_index ■■■■■■■■tmp_val = x[idx] ■■■■■■■■x[idx] = float(tmp_val) + h ■■■■■■■■fxh1 = f(x) # f(x+h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val - h ■■■■■■■■fxh2 = f(x) # f(x-h) ■■■■■■■■grad[idx] = (fxh1 - fxh2) / (2*h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val # 値を元に戻す ■■■■■■■■it.iternext() ■■■■■■■■ ■■■■return grad
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
347 :デフォルトの名無しさん[]:2017/06/12(月) 18:39:18.66 ID:yuw+moiO - def numerical_gradient(f, x):
■■■■h = 1e-4 # 0.0001 ■■■■grad = np.zeros_like(x) ■■■■ ■■■■it = np.nditer(x, flags=['multi_index'], op_flags=['readwrite']) ■■■■while not it.finished: ■■■■■■■■idx = it.multi_index ■■■■■■■■tmp_val = x[idx] ■■■■■■■■x[idx] = float(tmp_val) + h ■■■■■■■■fxh1 = f(x) # f(x+h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val - h ■■■■■■■■fxh2 = f(x) # f(x-h) ■■■■■■■■grad[idx] = (fxh1 - fxh2) / (2*h) ■■■■■■■■ ■■■■■■■■x[idx] = tmp_val # 値を元に戻す ■■■■■■■■it.iternext() ■■■■■■■■ ■■■■return grad
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
348 :デフォルトの名無しさん[]:2017/06/12(月) 18:39:42.89 ID:yuw+moiO - class simpleNet:
■■■■def __init__(self): ■■■■■■■■self.W = np.random.randn(2,3) #標準正規分布による 2x3 の行列 ■■■■def predict(self, x): ■■■■■■■■return np.dot(x, self.W) ■■■■def loss(self, x, t): ■■■■■■■■z = self.predict(x) ■■■■■■■■y = softmax(z) ■■■■■■■■loss = cross_entropy_error(y, t) ■■■■■■■■return loss
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
349 :デフォルトの名無しさん[]:2017/06/12(月) 18:40:13.16 ID:yuw+moiO - x = np.array([0.6, 0.9])
t = np.array([0, 0, 1]) net = simpleNet() f = lambda w: net.loss(x, t) dW = numerical_gradient(f, net.W) print(dW) ★★★★★★★★★★★★★ ★↑の f がひどすぎる ★ ★★★★★★★★★★★★★
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
351 :デフォルトの名無しさん[]:2017/06/12(月) 19:14:55.53 ID:yuw+moiO - >>350
間違っていないというのは分かりますが、あまりにもひどすぎます。 こんなひどいコードは見たことがありません。
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
352 :デフォルトの名無しさん[]:2017/06/12(月) 19:16:21.04 ID:yuw+moiO - fxh1 = f(x) # f(x+h)
↑ここですが、 fxh1 = f(a) とかでもいいわけです。
|
- くだすれPython(超初心者用) その34 [無断転載禁止]©2ch.net
353 :デフォルトの名無しさん[]:2017/06/12(月) 19:16:47.93 ID:yuw+moiO - こんなひどいコードを公にするという神経が分かりません。
害悪以外の何物でもありません。
|
- 【統計分析】機械学習・データマイニング15 [無断転載禁止]©2ch.net
772 :デフォルトの名無しさん[]:2017/06/12(月) 20:20:13.35 ID:yuw+moiO - 錯視が見えない人のほうがやばいのではないでしょうか?
↓新井仁之さん作の錯視です。 夏ワナー夏ワナー夏ワナー夏ワナー夏ワナー 夏ワナー夏ワナー夏ワナー夏ワナー夏ワナー ーナワ夏ーナワ夏ーナワ夏ーナワ夏ーナワ夏 ーナワ夏ーナワ夏ーナワ夏ーナワ夏ーナワ夏 夏ワナー夏ワナー夏ワナー夏ワナー夏ワナー 夏ワナー夏ワナー夏ワナー夏ワナー夏ワナー
|