<p dir="auto">I am trying to run the following code in anaconda on 64 bit windows 8.1. But it is generating error randomly after some epochs. Please help to get out of this.</p>
<p dir="auto">Code:</p>
<p dir="auto">from <strong>future</strong> import print_function<br>
import os<br>
import datetime<br>
import time<br>
import numpy as np<br>
from keras.datasets import cifar10<br>
from keras.preprocessing.image import ImageDataGenerator<br>
from keras.models import Sequential<br>
from keras.layers.core import Dense, Dropout, Activation, Flatten<br>
from keras.layers.convolutional import Convolution2D, MaxPooling2D<br>
from keras.optimizers import SGD,Adam, RMSprop<br>
from keras.utils import np_utils<br>
import matplotlib.pyplot as plt<br>
from keras.callbacks import Callback, ModelCheckpoint, History</p>
<p dir="auto">batch_size = 32<br>
nb_classes = 10<br>
nb_epoch = 50</p>
<p dir="auto">nwt = datetime.datetime.now</p>
<h1 dir="auto">input image dimensions</h1>
<p dir="auto">img_rows, img_cols = 32, 32</p>
<h1 dir="auto">the CIFAR10 images are RGB</h1>
<p dir="auto">img_channels = 3</p>
<h1 dir="auto">the data, shuffled and split between train and test sets</h1>
<p dir="auto">(X_train, y_train), (X_test, y_test) = cifar10.load_data()</p>
<h1 dir="auto">X_train = X_train[0:1000,:]</h1>
<h1 dir="auto">y_train = y_train[0:1000]</h1>
<h1 dir="auto">X_test = X_test[0:1000,:]</h1>
<h1 dir="auto">y_test = y_test[0:1000,]</h1>
<p dir="auto">print('X_train shape:', X_train.shape)<br>
print(X_train.shape[0], 'train samples')<br>
print(X_test.shape[0], 'test samples')</p>
<h1 dir="auto">convert class vectors to binary class matrices</h1>
<p dir="auto">Y_train = np_utils.to_categorical(y_train, nb_classes)<br>
Y_test = np_utils.to_categorical(y_test, nb_classes)</p>
<p dir="auto">model = Sequential()</p>
<p dir="auto">model.add(Convolution2D(32, 3, 3, border_mode='same',<br>
input_shape=(img_channels, img_rows, img_cols)))<br>
model.add(Activation('relu'))<br>
model.add(Convolution2D(32, 3, 3))<br>
model.add(Activation('relu'))<br>
model.add(MaxPooling2D(pool_size=(2, 2)))<br>
model.add(Dropout(0.25))</p>
<p dir="auto">model.add(Convolution2D(64, 3, 3, border_mode='same'))<br>
model.add(Activation('relu'))<br>
model.add(Convolution2D(64, 3, 3))<br>
model.add(Activation('relu'))<br>
model.add(MaxPooling2D(pool_size=(2, 2)))<br>
model.add(Dropout(0.25))</p>
<p dir="auto">model.add(Flatten())<br>
model.add(Dense(512))<br>
model.add(Activation('relu'))<br>
model.add(Dropout(0.5))<br>
model.add(Dense(nb_classes))<br>
model.add(Activation('softmax'))</p>
<h1 dir="auto">let's train the model using SGD + momentum (how original).</h1>
<p dir="auto">sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)<br>
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics = ['accuracy'])</p>
<p dir="auto">X_train = X_train.astype('float32')<br>
X_test = X_test.astype('float32')<br>
X_train /= 255<br>
X_test /= 255</p>
<p dir="auto">start_time = nwt()</p>
<p dir="auto">print('Using real-time data augmentation.')<br>
WEIGHTS_FNAME = 'cifar_aug_cnn_weights.hdf'</p>
<p dir="auto">if False and os.path.exists(WEIGHTS_FNAME):<br>
# Just change the True to false to force re-training<br>
print('Loading existing weights')<br>
model.load_weights(WEIGHTS_FNAME)<br>
else:<br>
# this will do preprocessing and realtime data augmentation<br>
datagen = ImageDataGenerator(<br>
featurewise_center=False,  # set input mean to 0 over the dataset<br>
samplewise_center=False,  # set each sample mean to 0<br>
featurewise_std_normalization=False,  # divide inputs by std of the dataset<br>
samplewise_std_normalization=False,  # divide each input by its std<br>
zca_whitening=False,  # apply ZCA whitening<br>
rotation_range=0,  # randomly rotate images in the range (degrees, 0 to 180)<br>
width_shift_range=0.1,  # randomly shift images horizontally (fraction of total width)<br>
height_shift_range=0.1,  # randomly shift images vertically (fraction of total height)<br>
horizontal_flip=True,  # randomly flip images<br>
vertical_flip=False)  # randomly flip images</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="# compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)

# fit the model on the batches generated by datagen.flow()"><pre class="notranslate"><code class="notranslate"># compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied)
datagen.fit(X_train)

# fit the model on the batches generated by datagen.flow()
</code></pre></div>
<h1 dir="auto">checkpt = ModelCheckpoint(filepath="F:\own_trial\p1_da\data\weights_{epoch:}.hdf", verbose=0, save_best_only=False)</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
                        samples_per_epoch=X_train.shape[0],
                        nb_epoch=nb_epoch, verbose = 1,
                        validation_data=(X_test, Y_test),
                        nb_worker=1)"><pre class="notranslate"><code class="notranslate">model.fit_generator(datagen.flow(X_train, Y_train, batch_size=batch_size),
                        samples_per_epoch=X_train.shape[0],
                        nb_epoch=nb_epoch, verbose = 1,
                        validation_data=(X_test, Y_test),
                        nb_worker=1)
</code></pre></div>
<h1 dir="auto">time.sleep(3)</h1>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="model.save_weights(WEIGHTS_FNAME)"><pre class="notranslate"><code class="notranslate">model.save_weights(WEIGHTS_FNAME)
</code></pre></div>
<hr>
<p dir="auto">Error occured at 43th epoch after processing 3424 files is:<br>
Epoch 43/50<br>
3424/50000 [=&gt;............................] - ETA: 91s - loss: 0.7680 - acc: 0.7314  Traceback (most recent call last):</p>
<p dir="auto">File "", line 1, in <br>
runfile('F:/da_trail/p1_da_50/demo_da.py', wdir='F:/da_trail/p1_da_50')</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile<br>
execfile(filename, namespace)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile<br>
exec(compile(scripttext, filename, 'exec'), glob, loc)</p>
<p dir="auto">File "F:/da_trail/p1_da_50/demo_da.py", line 116, in <br>
nb_worker=1)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\keras\models.py", line 851, in fit_generator<br>
pickle_safe=pickle_safe)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\keras\engine\training.py", line 1454, in fit_generator<br>
callbacks.on_batch_end(batch_index, batch_logs)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\keras\callbacks.py", line 61, in on_batch_end<br>
callback.on_batch_end(batch, logs)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\keras\callbacks.py", line 189, in on_batch_end<br>
self.progbar.update(self.seen, self.log_values)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\keras\utils\generic_utils.py", line 119, in update<br>
sys.stdout.write(info)</p>
<p dir="auto">File "C:\Users\Y50-70\Anaconda2\lib\site-packages\ipykernel\iostream.py", line 317, in write<br>
self._buffer.write(string)</p>
<p dir="auto">ValueError: I/O operation on closed file</p>
<p dir="auto">Any idea what could be causing this error? I've been trying to solve this for a week. Thanks in advance.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Train on 60816 samples, validate on 15204 samples
Epoch 1/20
60816/60816 [==============================] - 19s - loss: 0.1665 - acc: 0.9597 - val_loss: 0.1509 - val_acc: 0.9605
Epoch 2/20
31200/60816 [==============&gt;...............] - ETA: 8s - loss: 0.1583 - acc: 0.9600�����������������������������������������������������������������������������������
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-4-e3590f9fe5e6&gt; in &lt;module&gt;()
     16 
     17 my_model = model.fit(train_x, train_y, batch_size=100, nb_epoch=20,                       
---&gt; 18                      show_accuracy=True, verbose=1, validation_data=(test_x, test_y))
     19 score = model.evaluate(test_x, test_y, show_accuracy=True, verbose=0)
     20 print('Test loss:', score[0])

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in fit(self, X, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, show_accuracy, class_weight, sample_weight)
    644                          verbose=verbose, callbacks=callbacks,
    645                          val_f=val_f, val_ins=val_ins,
--&gt; 646                          shuffle=shuffle, metrics=metrics)
    647 
    648     def predict(self, X, batch_size=128, verbose=0):

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in _fit(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, metrics)
    284                     batch_logs[l] = o
    285 
--&gt; 286                 callbacks.on_batch_end(batch_index, batch_logs)
    287 
    288                 epoch_logs = {}

/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc in on_batch_end(self, batch, logs)
     58         t_before_callbacks = time.time()
     59         for callback in self.callbacks:
---&gt; 60             callback.on_batch_end(batch, logs)
     61         self._delta_ts_batch_end.append(time.time() - t_before_callbacks)
     62         delta_t_median = np.median(self._delta_ts_batch_end)

/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc in on_batch_end(self, batch, logs)
    168         # will be handled by on_epoch_end
    169         if self.verbose and self.seen &lt; self.params['nb_sample']:
--&gt; 170             self.progbar.update(self.seen, self.log_values)
    171 
    172     def on_epoch_end(self, epoch, logs={}):

/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.pyc in update(self, current, values)
     59             prev_total_width = self.total_width
     60             sys.stdout.write(&quot;\b&quot; * prev_total_width)
---&gt; 61             sys.stdout.write(&quot;\r&quot;)
     62 
     63             numdigits = int(np.floor(np.log10(self.target))) + 1

/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in write(self, string)
    315 
    316             is_child = (not self._is_master_process())
--&gt; 317             self._buffer.write(string)
    318             if is_child:
    319                 # newlines imply flush in subprocesses

ValueError: I/O operation on closed file"><pre class="notranslate"><code class="notranslate">Train on 60816 samples, validate on 15204 samples
Epoch 1/20
60816/60816 [==============================] - 19s - loss: 0.1665 - acc: 0.9597 - val_loss: 0.1509 - val_acc: 0.9605
Epoch 2/20
31200/60816 [==============&gt;...............] - ETA: 8s - loss: 0.1583 - acc: 0.9600�����������������������������������������������������������������������������������
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
&lt;ipython-input-4-e3590f9fe5e6&gt; in &lt;module&gt;()
     16 
     17 my_model = model.fit(train_x, train_y, batch_size=100, nb_epoch=20,                       
---&gt; 18                      show_accuracy=True, verbose=1, validation_data=(test_x, test_y))
     19 score = model.evaluate(test_x, test_y, show_accuracy=True, verbose=0)
     20 print('Test loss:', score[0])

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in fit(self, X, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, show_accuracy, class_weight, sample_weight)
    644                          verbose=verbose, callbacks=callbacks,
    645                          val_f=val_f, val_ins=val_ins,
--&gt; 646                          shuffle=shuffle, metrics=metrics)
    647 
    648     def predict(self, X, batch_size=128, verbose=0):

/usr/local/lib/python2.7/dist-packages/keras/models.pyc in _fit(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, metrics)
    284                     batch_logs[l] = o
    285 
--&gt; 286                 callbacks.on_batch_end(batch_index, batch_logs)
    287 
    288                 epoch_logs = {}

/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc in on_batch_end(self, batch, logs)
     58         t_before_callbacks = time.time()
     59         for callback in self.callbacks:
---&gt; 60             callback.on_batch_end(batch, logs)
     61         self._delta_ts_batch_end.append(time.time() - t_before_callbacks)
     62         delta_t_median = np.median(self._delta_ts_batch_end)

/usr/local/lib/python2.7/dist-packages/keras/callbacks.pyc in on_batch_end(self, batch, logs)
    168         # will be handled by on_epoch_end
    169         if self.verbose and self.seen &lt; self.params['nb_sample']:
--&gt; 170             self.progbar.update(self.seen, self.log_values)
    171 
    172     def on_epoch_end(self, epoch, logs={}):

/usr/local/lib/python2.7/dist-packages/keras/utils/generic_utils.pyc in update(self, current, values)
     59             prev_total_width = self.total_width
     60             sys.stdout.write("\b" * prev_total_width)
---&gt; 61             sys.stdout.write("\r")
     62 
     63             numdigits = int(np.floor(np.log10(self.target))) + 1

/usr/local/lib/python2.7/dist-packages/ipykernel/iostream.pyc in write(self, string)
    315 
    316             is_child = (not self._is_master_process())
--&gt; 317             self._buffer.write(string)
    318             if is_child:
    319                 # newlines imply flush in subprocesses

ValueError: I/O operation on closed file
</code></pre></div>