Python 为什么我们需要在Keras load_模型函数中提到损失函数?
我使用自定义损失函数创建了一个Keras模型并保存了它。当我使用KerasPython 为什么我们需要在Keras load_模型函数中提到损失函数?,python,machine-learning,keras,loss-function,Python,Machine Learning,Keras,Loss Function,我使用自定义损失函数创建了一个Keras模型并保存了它。当我使用Kerasload_model()函数重新加载经过训练的模型进行预测时,我必须将自定义损失函数传递给custom_objects参数: new_model=load_model('yolo_1.model',custom_objects={'custom_loss':custom_loss}) 我的问题是,为什么我们必须在load\u model()中提到自定义损耗函数?我认为在预测过程中不需要损失函数。这是因为save方法的i
load_model()
函数重新加载经过训练的模型进行预测时,我必须将自定义损失函数传递给custom_objects
参数:
new_model=load_model('yolo_1.model',custom_objects={'custom_loss':custom_loss})
我的问题是,为什么我们必须在
load\u model()
中提到自定义损耗函数?我认为在预测过程中不需要损失函数。这是因为save
方法的include\u optimizer
参数默认设置为True
。因此,优化器和损失函数将被保存,因此,当您使用load\u model
函数时,它们将被加载
但是,如果您只想使用模型进行预测,则无需保存优化器,因此在保存模型时将include\u优化器
设置为False
:
model.save('my_model.h5', include_optimizer=False)
这样,优化器和使用的损失函数将不会被保存,因此您不需要指定加载模型时使用的自定义损失函数