简体   繁体   English

Python 中卷积神经网络的检查点 function 出错

[英]Error in Checkpoint function of Convolutional neural networks in Python

Currently, this is the code I have.目前,这是我拥有的代码。 I know the problem is in the checkpoint callback because I've tested running my program without it and it works fine.我知道问题出在检查点回调中,因为我已经测试过在没有它的情况下运行我的程序并且它工作正常。

model.compile(optimizer = 'adam', loss = 'categorical_crossentropy', metrics = ['accuracy'])

class_weight = {0: 70.,
                1: 110.,
                2: 82.,
                3: 17.,
                4: 33.,
                5: 1.,
                6: 1.,
                7: 9.}

lr_schedule = tf.keras.callbacks.LearningRateScheduler(lr_schedule)

checkpoint = tf.keras.callbacks.ModelCheckpoint(filepath='Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5', 
                                               save_weights_only=True,
                                                save_best_only=True,monitor='loss',
                                                mode='min',verbose=1) 

model.fit(train_generator, epochs = 5, steps_per_epoch=10, callbacks=[lr_schedule, checkpoint], class_weight=(class_weight))  

In a folder named tests I have the pyton file and another folder called Checkpoint.在名为 tests 的文件夹中,我有 pyton 文件和另一个名为 Checkpoint 的文件夹。 I want to save my model inside the folder named Checkpoint but I've tried different name and all give me the error below我想将我的 model 保存在名为 Checkpoint 的文件夹中,但我尝试了不同的名称,但都给了我下面的错误

Error: 'Failed to format this callback filepath: "Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5". Reason: \'val_loss\'错误: 'Failed to format this callback filepath: "Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5". Reason: \'val_loss\' 'Failed to format this callback filepath: "Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5". Reason: \'val_loss\'

How can I make the ModelCheckpoint work correctly?如何使 ModelCheckpoint 正常工作?

EDIT: I am not using validation data because my monitor is based on the loss function.编辑:我没有使用验证数据,因为我的监视器基于损失 function。 I have updated my version of tensorflow by doing activate <name_of_env> followed by pip install tensorflow --upgrade .我已经更新了我的 tensorflow 版本,方法是activate <name_of_env>然后pip install tensorflow --upgrade After this I ran it again and I still get the same error.在此之后我再次运行它,我仍然得到同样的错误。

I was wondering, do I have to have something inside the Checkpoint folder for it to work?我想知道,我必须在 Checkpoint 文件夹中有一些东西才能工作吗?

I solved it.我解决了。 I just changed the filepath from 'Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5' to 'Checkpoint/random.h5' and it worked.我刚刚将文件路径从'Checkpoint/model.{epoch:02d}-{val_loss:.4f}.h5'更改为'Checkpoint/random.h5'并且它起作用了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM