Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Keras. ValueError: I/O operation on closed file

I use jupyter notebook with anaconda. I use kerast firstly, and i can't do tutorial. About this issues are two themes in stackoverflow, but solve not found.

My code:

model = Sequential()
model.add(Dense(1, input_dim=1, activation='softmax'))

model.compile(optimizer='rmsprop',
              loss='binary_crossentropy',
              metrics=['accuracy'])

X_train_shape = X_train.reshape(len(X_train), 1)
Y_train_shape = Y_train.reshape(len(Y_train), 1)
model.fit(X_train, Y_train, nb_epoch=5, batch_size=32)

And I have error, it's some random and sometimes one or two epoch competed:

Epoch 1/5 4352/17500 [======>.......................]

--------------------------------------------------------------------------- ValueError Traceback (most recent call last) in () 2 # of 32 samples 3 #sleep(0.1) ----> 4 model.fit(X_train, Y_train, nb_epoch=5, batch_size=32) 5 #sleep(0.1)

C:\Anaconda3\envs\py27\lib\site-packages\keras\models.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, **kwargs) 395 shuffle=shuffle, 396 class_weight=class_weight, --> 397 sample_weight=sample_weight) 398 399 def evaluate(self, x, y, batch_size=32, verbose=1,

C:\Anaconda3\envs\py27\lib\site-packages\keras\engine\training.pyc in fit(self, x, y, batch_size, nb_epoch, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight) 1009 verbose=verbose, callbacks=callbacks, 1010
val_f=val_f, val_ins=val_ins, shuffle=shuffle, -> 1011 callback_metrics=callback_metrics) 1012 1013 def evaluate(self, x, y, batch_size=32, verbose=1, sample_weight=None):

C:\Anaconda3\envs\py27\lib\site-packages\keras\engine\training.pyc in _fit_loop(self, f, ins, out_labels, batch_size, nb_epoch, verbose, callbacks, val_f, val_ins, shuffle, callback_metrics) 753 batch_logs[l] = o 754 --> 755 callbacks.on_batch_end(batch_index, batch_logs) 756 757 epoch_logs = {}

C:\Anaconda3\envs\py27\lib\site-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)

C:\Anaconda3\envs\py27\lib\site-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={}):

C:\Anaconda3\envs\py27\lib\site-packages\keras\utils\generic_utils.pyc in update(self, current, values) 110 info += ((prev_total_width - self.total_width) * " ") 111 --> 112 sys.stdout.write(info) 113 sys.stdout.flush() 114

C:\Anaconda3\envs\py27\lib\site-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

like image 879
attashe Avatar asked Apr 13 '16 13:04

attashe


People also ask

Why does keras-team-2110 keep raising'ValueError-I/O operation on closed file'?

This is a workaround for keras-team#2110 where calling `model.fit` with verbose=1 using IPython can intermittently raise "ValueError: I/O operation on closed file". This exception appears to be caused by an unknown IO bug with IPython that is raised when updating the ProgbarLogger progress bar .

Why do I get ValueError when a file is closed?

Once the file is closed, you can no longer access the file and perform read/write operations on that file. There are three main common reasons why we face the ValueError: I/O operation on closed file. First, when you try, forget to indent the code in the with statement. Second, when you try to read/write to a file in a closed state.

What is I/O operation on closed file in Python?

The i/o operations in Python are performed when the file is in an open state. So if we are trying to read or write into a file that is already closed state Python interpreter will raise the ValueError: I/O operation on closed file.

What went wrong with Keras-team?

Sorry, something went wrong. This is a workaround for keras-team#2110 where calling `model.fit` with `verbose=1` using IPython can intermittently raise "ValueError: I/O operation on closed file". This exception appears to be caused by an unknown IO bug with IPython that is raised when updating the `ProgbarLogger` progress bar .


1 Answers

Change your verbose level in model.fit() to verbose=0.
See github.com/fchollet/keras/issues/2110

It's not a straight-on "fix", but it should help alleviate a race condition associated with updating of the iPython console.

like image 175
Amw 5G Avatar answered Oct 23 '22 18:10

Amw 5G