Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么我们需要在Keras load_模型函数中提到损失函数?_Python_Machine Learning_Keras_Loss Function - Fatal编程技术网

Python 为什么我们需要在Keras load_模型函数中提到损失函数?

Python 为什么我们需要在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

我使用自定义损失函数创建了一个Keras模型并保存了它。当我使用Keras
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)
这样,优化器和使用的损失函数将不会被保存,因此您不需要指定加载模型时使用的自定义损失函数