Keras ValueError:对已关闭文件的I/O操作 [英] Keras ValueError: I/O operation on closed file

查看:115
本文介绍了Keras ValueError:对已关闭文件的I/O操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个单层网络.当它开始通过model.fit进行训练时,在某个随机的时期,它将引发以下错误:

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

这是我使用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.

谢谢

以下是错误的完整输出:

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

推荐答案

我遇到了同样的问题,并已将其修复.通过在model.fit中设置verbose = 2可以帮助解决此问题.在model.fit的Keras文档中,您可以找到:详细:0表示不记录到stdout,1表示进度条记录,2表示每个纪元一个日志行.".设置verbose = 2将在每个时期后更新进度,并有助于减少输出到屏幕的日志信息的数量,我猜这是造成IO关闭问题的原因.有人建议将verbose设置为0以禁用输出,但是在那种情况下,我们无法跟踪进度.我希望这也可以解决您的问题.

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.

这篇关于Keras ValueError:对已关闭文件的I/O操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆