Keras ValueError:关闭文件的I / O操作

时间:2016-04-01 23:59:52

标签: python neural-network theano keras

我正在尝试编写单层网络。当它开始训练model.fit时,在某个随机时期它会抛出以下错误:

ValueError: I/O operation on closed file

以下是我使用model.fit

的方法

my_model = model.fit(train_x, train_y, batch_size=100, nb_epoch=20, show_accuracy=True, verbose=1)

如果您有任何想法或遇到同样的问题,请告诉我。

由于

以下是错误的完整输出:

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

2 个答案:

答案 0 :(得分:7)

正如问题评论中提到的那样(直到现在才看到),这实际上是由于IPython / Jupyter IO中的一个错误以及它如何处理来自Keras的详细输出。您可以通过在模型上调用的verbose=Falsetrainpredict方法上设置predict_proba作为同时解决方法来停用报告,或者只是在笔记本。

issue on the Keras Github总结了问题。

答案 1 :(得分:7)

我遇到了同样的问题并立即修复了。通过在model.fit中设置verbose = 2可以帮助解决这个问题。在Model.fit的Keras文档中,您可以找到:&#34;详细:0表示没有记录到stdout,1表示进度条记录,2表示每个纪元的一个日志行。&#34;。设置verbose = 2将更新每个纪元后的进度,并有助于减少输出到屏幕的日志信息量,我猜这会导致IO关闭问题。有些人建议设置verbose = 0来禁用输出,但在这种情况下我们无法跟踪进度。我希望这也有助于解决您的问题。