【问题标题】:KeyError: Failed to format this callback filepathKeyError:无法格式化此回调文件路径
【发布时间】:2020-04-05 21:34:30
【问题描述】:

我正在尝试获得验证准确度并选择保存具有最佳准确度的模型。但是在第一个 epoch 之后,它显示检查点错误。

filepath="tumor_detection-{epoch:02d}-{val_acc:.2f}.hdf5"

# save the model with the best validation (development) accuracy till now
checkpoint = ModelCheckpoint(filepath, monitor='val_acc', verbose=1, save_best_only=True, mode='max')

%%time
model.fit(xtrain,ytrain,batch_size=32,epochs=30,validation_data=(xval,yval),callbacks=[checkpoint])```

It is then showing an error as follows:
Epoch 1/30
51/51 [==============================] - ETA: 0s - loss: 0.4651 - accuracy: 0.7725
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _get_file_path(self, epoch, logs)
   1243         # placeholders can cause formatting to fail.
-> 1244         return self.filepath.format(epoch=epoch + 1, **logs)
   1245       except KeyError as e:

KeyError: 'val_acc'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
9 frames
<decorator-gen-60> in time(self, line, cell, local_ns)

<timed eval> in <module>()

/usr/local/lib/python3.6/dist-packages/tensorflow/python/keras/callbacks.py in _get_file_path(self, epoch, logs)
   1245       except KeyError as e:
   1246         raise KeyError('Failed to format this callback filepath: "{}". '
-> 1247                        'Reason: {}'.format(self.filepath, e))
   1248     else:
   1249       # If this is multi-worker training, and this worker should not

KeyError: 'Failed to format this callback filepath: "tumor_detection-{epoch:02d}-{val_acc:.2f}.hdf5". Reason: \'val_acc\'

【问题讨论】:

  • 我认为这是 tensorflow 的一个错误,我假设您的 tf 版本是 2.2.0 或更高版本?
  • 我有 tensorflow 2.3.0 版。我该如何解决这个问题?

标签: python validation google-colaboratory checkpoint


【解决方案1】:

两个都替换

'val_acc'

在文件路径和检查点中

'val_accuracy'

它会起作用的。

【讨论】:

    【解决方案2】:

    我添加了validation_split=0.1 并为我工作:

    ...
    my_callbacks = [
        tf.keras.callbacks.EarlyStopping(patience=2),
        tf.keras.callbacks.ModelCheckpoint(filepath='model.{epoch:02d}-{val_loss:.2f}.h5'),
        tf.keras.callbacks.TensorBoard(log_dir='./logs'),
    ]
    
    history = model.fit(X_train, y_train, validation_split=0.1, epochs=200, callbacks=my_callbacks)
    

    【讨论】:

      【解决方案3】:

      这可能与this 问题重复。

      我在使用较小的数据集进行训练时遇到了同样的问题,其中训练数据的验证拆分为 0。

      【讨论】:

        【解决方案4】:

        替换这个

        val_accuracy
        

        有了这个

        val_acc
        

        我试试这个,效果很好

        【讨论】:

          猜你喜欢
          • 2021-10-09
          • 1970-01-01
          • 1970-01-01
          • 2017-06-04
          • 1970-01-01
          • 2020-01-28
          • 2016-11-10
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多