現在chainerを活用して簡単なニューラルネットワークを実装しており、以下のエラー文が出て詰まっております。
IndexError: index 1 is out of bounds for axis 0 with size 1
どうやらリストの範囲外を指定しまっているらしいのですが・・・
やろうとしていること:
一定期間の株価の終値を入力値としており(100次元のベクトル、時系列データであるがとり合えず無視)、出力値には次の日の株価が上がるか下がるかの二値分類。(1または0)
おそらく出力値に対してインデックス1の箇所を指定しまっているのだと思いますがどこでそのような処理が行われいるのかがイマイチわかりません。
python
1import matplotlib.pyplot as plt 2import learning 3import numpy as np 4from sklearn.preprocessing import MinMaxScaler 5import chainer 6import chainer.links as L 7import chainer.functions as F 8from chainer import Chain, Variable, datasets, optimizers 9from chainer import report, training 10from chainer.training import extensions 11from chainer import iterators 12import chainer.cuda 13 14#x_train,x_test,y_train,y_test = learning.fluctuations_test() 15x_train,x_test,y_train,y_test = learning.stock_test() 16 17scaler = MinMaxScaler(feature_range=(0, 1)) 18x_train, x_test = scaler.fit_transform(x_train), scaler.fit_transform(x_test) 19 20train = list(zip(x_train, y_train)) 21test = list(zip(x_test, y_test)) 22 23class MLP(chainer.Chain): 24 25 def __init__(self, n_mid_units=10, n_out=1): 26 super().__init__() 27 28 with self.init_scope(): 29 self.fc1 = L.Linear(None, n_mid_units) 30 self.fc2 = L.Linear(n_mid_units, n_mid_units) 31 self.fc3 = L.Linear(n_mid_units, n_out) 32 33 def __call__(self, x, t=None): 34 h = F.relu(self.fc1(x)) 35 h = F.relu(self.fc2(h)) 36 h = self.fc3(h) 37 return h 38 39 40batchsize = 32 41 42train_iter = iterators.SerialIterator(train, batchsize) 43test_iter = iterators.SerialIterator(test, batchsize, shuffle=False, repeat=False) 44 45# ネットワークを作成 46model = MLP() 47 48# L.Classifier でラップし、損失の計算などをモデルに含める 49net = L.Classifier(model) 50 51# 最適化手法を選択してオプティマイザを作成し、最適化対象のネットワークを持たせる 52optimizer = optimizers.MomentumSGD(lr=0.1).setup(net) 53 54# アップデータにイテレータとオプティマイザを渡す 55updater = training.StandardUpdater(train_iter, optimizer, device=-1) # device=-1でCPUでの計算実行を指定 56 57# Trainerとそのextensions 58epoch = 3000 59trainer = training.Trainer(updater, (epoch, 'epoch'), out='result') 60 61# 評価データで評価 62trainer.extend(extensions.Evaluator(test_iter, model, device = -1)) 63 64# 学習結果の途中を表示する 65trainer.extend(extensions.LogReport(trigger=(1, 'epoch'))) 66 67# 1エポックごとに、trainデータに対するlossと、testデータに対するlossを出力させる 68trainer.extend(extensions.PrintReport(['epoch', 'main/loss', 'main/accuracy','validation/main/loss', 'val/main/accuracy', 'elapsed_time']), trigger=(1, 'epoch')) 69 70trainer.run()
Exception in main training loop: index 1 is out of bounds for axis 0 with size 1
Traceback (most recent call last):
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 316, in run
update()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 175, in update
self.update_core()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 187, in update_core
optimizer.update(loss_func, *in_arrays)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/optimizer.py", line 864, in update
loss = lossfun(*args, **kwds)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/link.py", line 294, in call
out = forward(*args, **kwargs)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/links/model/classifier.py", line 144, in forward
self.loss = self.lossfun(self.y, t)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/functions/loss/softmax_cross_entropy.py", line 500, in softmax_cross_entropy
loss, = func.apply((x, t))
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/function_node.py", line 321, in apply
outputs = self.forward(in_data)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/function_node.py", line 513, in forward
return self.forward_cpu(inputs)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/functions/loss/softmax_cross_entropy.py", line 143, in forward_cpu
log_p = log_yd[t.ravel(), numpy.arange(t.size)]
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "test2.py", line 70, in <module>
trainer.run()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 349, in run
six.reraise(*exc_info)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/six.py", line 693, in reraise
raise value
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/trainer.py", line 316, in run
update()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 175, in update
self.update_core()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/training/updaters/standard_updater.py", line 187, in update_core
optimizer.update(loss_func, *in_arrays)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/optimizer.py", line 864, in update
loss = lossfun(*args, **kwds)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/link.py", line 294, in call
out = forward(*args, **kwargs)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/links/model/classifier.py", line 144, in forward
self.loss = self.lossfun(self.y, t)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/functions/loss/softmax_cross_entropy.py", line 500, in softmax_cross_entropy
loss, = func.apply((x, t))
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/function_node.py", line 321, in apply
outputs = self.forward(in_data)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/function_node.py", line 513, in forward
return self.forward_cpu(inputs)
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/functions/loss/softmax_cross_entropy.py", line 143, in forward_cpu
log_p = log_yd[t.ravel(), numpy.arange(t.size)]
IndexError: index 1 is out of bounds for axis 0 with size 1
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。