我正在尝试使用Keras训练模型,我得到一个“ValueError:关闭文件的I / O操作”。困扰我的部分是我每次都在训练过程的不同点上得到它,所以我不知道造成它的原因。
这是我的代码:
from keras.models import Sequential
from keras.layers.core import Dense
from keras.optimizers import SGD
np.random.seed(1)
model = Sequential()
model.add(Dense(input_dim=X_train.shape[1], output_dim=50, init='uniform', activation='tanh'))
model.add(Dense(input_dim=50, output_dim=50, init='uniform', activation='tanh'))
model.add(Dense(input_dim=50, output_dim=y_train_ohe.shape[1], init='uniform', activation='softmax'))
sgd = SGD(lr=0.001, decay=1e-7, momentum=.9)
model.compile(loss='categorical_crossentropy', optimizer=sgd)
model.fit(X_train, y_train_ohe, nb_epoch=50, batch_size=300, verbose=1, validation_split=0.1)
每次都不一样,但我得到这样的输出:
Train on 54000 samples, validate on 6000 samples
Epoch 1/50
54000/54000 [==============================] - 1s - loss: 0.2964 - val_loss: 0.2558
Epoch 2/50
54000/54000 [==============================] - 0s - loss: 0.2927 - val_loss: 0.2546
Epoch 3/50
12000/54000 [=====>........................] - ETA: 0s - loss: 0.2829
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-93-cdea496300a5> in <module>()
----> 1 model.fit(X_train, y_train_ohe, nb_epoch=50, batch_size=300, verbose=1, validation_split=0.1)
/home/julius/anaconda2/lib/python2.7/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)
400 shuffle=shuffle,
401 class_weight=class_weight,
--> 402 sample_weight=sample_weight)
403
404 def evaluate(self, x, y, batch_size=32, verbose=1,
正如你所看到的,模型训练了几个时代,然后才给我这个错误。 X_train和y_train来自MNIST数据集。此外,我发现我关闭了详细信息,问题就消失了。这基本上解决了它,但不让我使用详细。有没有办法让它显示更新而不会出现此错误?
答案 0 :(得分:1)
我通过添加%%capture
作为包含model.fit()