问题
I am trying to write a single layer network. When it starts to train through model.fit
, at some random epoch it will throw the following error:
ValueError: I/O operation on closed file
Here is how I am using model.fit
my_model = model.fit(train_x, train_y, batch_size=100, nb_epoch=20, show_accuracy=True, verbose=1)
Please let me know if you have any thoughts or is encountering the same problem.
Thanks
Here is the full output of the error:
Epoch 1/20
47900/60816 [======================>.......] - ETA: 3s - loss: 0.1688 - acc: 0.9594
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-4-273f2082a322> in <module>()
14 model.compile(loss='binary_crossentropy', optimizer='adadelta')
15
---> 16 model.fit(train_x, train_y, batch_size=100, nb_epoch=20, show_accuracy=True, verbose=1)
17 score = model.evaluate(test_x, test_y, show_accuracy=True, verbose=0)
18 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)
699 verbose=verbose, callbacks=callbacks,
700 val_f=val_f, val_ins=val_ins,
--> 701 shuffle=shuffle, metrics=metrics)
702
703 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)
321 batch_logs[l] = o
322
--> 323 callbacks.on_batch_end(batch_index, batch_logs)
324
325 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:
---> 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)
187 # will be handled by on_epoch_end
188 if self.verbose and self.seen < self.params['nb_sample']:
--> 189 self.progbar.update(self.seen, self.log_values)
190
191 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)
---> 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())
--> 317 self._buffer.write(string)
318 if is_child:
319 # newlines imply flush in subprocesses
ValueError: I/O operation on closed file
回答1:
As mentioned in the question comments (didn't see until just now), this is actually due to a bug in IPython/Jupyter IO and how it handles the verbose output from Keras. You can disable reporting by setting verbose=False
on the train
and predict
or predict_proba
methods invoked on the model as a workaround in the mean time, or just run the model outside of the notebook.
There's an issue on the Keras Github that summarizes the problem.
回答2:
I got the same problem and had it fixed now. By setting verbose = 2 in model.fit can help to solve this problem. In Keras document for model.fit, you can find this: "verbose: 0 for no logging to stdout, 1 for progress bar logging, 2 for one log line per epoch.". Setting verbose = 2 will update the progress after each epoch, and help to reduce the amount of logging information output to the screen, which i guess caused the IO closed problem. Some people suggest to set verbose = 0 to disable the output but in that case we can't track the progress. I hope this can help to solve your problem as well.
来源:https://stackoverflow.com/questions/36367641/keras-valueerror-i-o-operation-on-closed-file