Home>

Currently, we are creating a model for classifying stock price declines using CNN.
It worked properly until now, but suddenly
StopIteration error occurred and the second and subsequent learnings are no longer repeated.
I have no idea what the cause is even if I look into it.

The following is the full error
epoch main/loss main/accuracy validation/main/loss validation/main/accuracy elapsed_time
1 20.1328 0.509596 0.692708 0.526667 605.443
Exception in main training loop:
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 180, in update_core
batch = iterator.next ()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/iterators/serial_iterator.py", line 75, innext
raise StopIteration
Will finalize trainer extensions and updater before reraising the exception.
Traceback (most recent call last):
File "cnn.py", line 67, in
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 180, in update_core
batch = iterator.next ()
File "/Users/kojimakazuya/anaconda3/lib/python3.7/site-packages/chainer/iterators/serial_iterator.py", line 75, innext
raise StopIteration
StopIteration

import matplotlib.pyplot as plt
import load_data
import stock_data
import img_to_vector
import load_vector
import numpy as np
from sklearn.preprocessing import MinMaxScaler
import chainer
import chainer.links as L
import chainer.functions as F
from chainer import Chain, Variable, datasets, optimizers
from chainer import report, training
from chainer.training import extensions
from chainer import iterators
from chainer import serializers
import chainer.cuda
#Data preparation
sp500, topix500, usd_jpy = load_vector.load_vector ()
x_train, x_test = topix500 [: 2000],

 topix500 [2000:]
t_train, t_test = load_data.label (stock_data.teacher_dataset () [: 2000]), load_data.label (stock_data.teacher_dataset () [2000:])
train = list (zip (x_train, t_train))
test = list (zip (x_test, t_test))
class CNN (Chain):
    def __init __ (self):
        super (CNN, self) .__ init __ ()
        with self.init_scope ():
            self.cn1 = L.Convolution2D (None, 32, ksize = 3, pad = 1)
            self.cn2 = L.Convolution2D (None, 32, ksize = 3, pad = 1)self.fc1 = L.Linear (None, 100)
            self.fc2 = L.Linear (100, 2)
    def __call __ (self, x, t = None):
        h1 = F.max_pooling_2d (F.relu (self.cn1 (x)), ksize = 2, stride = 2)
        h2 = F.max_pooling_2d (F.relu (self.cn2 (h1)), ksize = 2, stride = 2)
        h3 = F.dropout (F.relu (self.fc1 (h2)))
        return self.fc2 (h3)

model = CNN ()
net = L.Classifier (model)
optimizer = optimizers.Adam (). setup (net)
batchsize = 20
train_iter = chainer.iterators.SerialIterator (train, batchsize, repeat = False, shuffle = True)
test_iter = chainer.iterators.SerialIterator (test, batchsize, repeat = False, shuffle = True)
updater = training.StandardUpdater (train_iter, optimizer, device = -1) # Specify calculation execution on CPU with device = -1
epoch = 100
trainer = training.Trainer (updater, (epoch, 'epoch'), out = 'result')
# Evaluate with test data
trainer.extend (extensions.Evaluator (test_iter, net, device = -1))
# Record learning
trainer.extend (extensions.LogReport (trigger = (1, 'epoch')))
# Draw and save on graph
trainer.extend (extensions.PlotReport (['main/loss', 'validation/main/loss'],

 x_key = 'epoch', file_name = 'cnn2_loss.png'))
trainer.extend (extensions.PlotReport (['main/accuracy', 'validation/main/accuracy'],

 x_key = 'epoch', file_name = 'cnn2_accuracy.png'))
trainer.extend (extensions.PrintReport (['epoch', 'main/loss', 'main/accuracy', 'validation/main/loss', 'validation/main/accuracy', 'elapsed_time']), trigger = ( 1, 'epoch'))
trainer.run ()
  • Answer # 1

    It was cured by changing the repeat argument in the place of iter to True.
    Speaking of which, when shuffle was changed to true, it did not understand the meaning of repeat and it was set to false like test.
    Repeat is whether to repeat.