我是Keras的新手,他检查了许多与负载模型有关的问题,但没有一个问题(例如eg1 eg2}使我解决问题。
很抱歉,很长的帖子,但我想提供尽可能多的数据来帮助您重现错误
我在google colab中运行代码
我有一个具有以下自定义损失函数的模型:
def wasserstein_loss(y_true, y_pred):
return K.mean(y_true * y_pred)
def gradient_penalty_loss(y_true, y_pred, averaged_samples, gradient_penalty_weight):
gradients = K.gradients(y_pred, averaged_samples)[0]
gradients_sqr = K.square(gradients)
gradients_sqr_sum = K.sum(gradients_sqr,
axis=np.arange(1, len(gradients_sqr.shape)))
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
gradient_penalty = gradient_penalty_weight * K.square(1 -
gradient_l2_norm)
return K.mean(gradient_penalty)
partial_gp_loss = partial(gradient_penalty_loss,
averaged_samples=averaged_samples,
gradient_penalty_weight=GRADIENT_PENALTY_WEIGHT)
partial_gp_loss.__name__ = 'gradient_penalty' # Functions need names or Keras will throw an error
使用损失函数:
discriminator_model = Model(inputs=[real_samples, generator_input_for_discriminator],
outputs=[discriminator_output_from_real_samples,discriminator_output_from_generator,averaged_samples_out])
discriminator_model.compile(optimizer=Adam(0.0001, beta_1=0.5, beta_2=0.9),
loss=[wasserstein_loss,
wasserstein_loss,
partial_gp_loss])
我保存到模型的方式:
discriminator_model.save('D_' + str(epoch) + '.h5')
generator_model.save('G_' + str(epoch) + '.h5')
我加载模型的方式:
generator_model = models.load_model(Gh5files[-1],custom_objects={'wasserstein_loss': wasserstein_loss})
discriminator_model = models.load_model(Dh5files[-1],custom_objects={'wasserstein_loss': wasserstein_loss ,
'RandomWeightedAverage': RandomWeightedAverage ,
'gradient_penalty':partial_gp_loss(gradient_penalty_loss,
averaged_samples=averaged_samples,
gradient_penalty_weight=GRADIENT_PENALTY_WEIGHT)
})
否,当我尝试上传保存的模型时,出现以下错误
Loading pretrained models
about to load follwoing files: ./G_31.h5 ./D_31.h5
/usr/local/lib/python3.6/dist-packages/keras/engine/saving.py:327: UserWarning: Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.
warnings.warn('Error in loading the saved optimizer '
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-30-5ed3e08a8fce> in <module>()
12 'gradient_penalty':partial_gp_loss(gradient_penalty_loss,
13 averaged_samples=averaged_samples,
---> 14 gradient_penalty_weight=GRADIENT_PENALTY_WEIGHT)
15 })
16
TypeError: gradient_penalty_loss() missing 1 required positional argument: 'y_pred'
我缺少什么,如何介绍y_pred?
答案 0 :(得分:1)
Keras自定义损失函数的格式必须为my_loss_function(y_true, y_pred)
。您的gradient_penalty_loss
函数无效,因为它具有其他参数。
正确的方法如下:
def get_gradient_penalty_loss(averaged_samples, gradient_penalty_weight):
def gradient_penalty_loss(y_true, y_pred):
gradients = K.gradients(y_pred, averaged_samples)[0]
gradients_sqr = K.square(gradients)
gradients_sqr_sum = K.sum(gradients_sqr,
axis=np.arange(1, len(gradients_sqr.shape)))
gradient_l2_norm = K.sqrt(gradients_sqr_sum)
gradient_penalty = gradient_penalty_weight * K.square(1 -
gradient_l2_norm)
return K.mean(gradient_penalty)
return gradient_penalty_loss
gradient_penalty_loss= get_gradient_penalty_loss(
gradient_penalty_loss,
averaged_samples=averaged_samples,
gradient_penalty_weight=GRADIENT_PENALTY_WEIGHT)
然后通过models.load_model(..., custom_objects={'gradient_penalty_loss':gradient_penalty_loss})
几乎看起来您可能正在尝试使用partial
函数来执行类似的操作,但是由于您尚未定义它,因此我不知道是否是这种情况。
无论哪种方式,都存在另一个问题,因为您正在调用partial_gp_loss = partial(...)
并返回gradient_penalty_loss
。然后,在加载模型时,调用partial_gp_loss(...)
,但是此时您应该调用任何东西,而应该只是传递函数!
您收到错误TypeError: gradient_penalty_loss() missing 1 required positional argument: 'y_pred'
,因为此时您正在尝试执行gradient_penalty_loss
,并且向其传递了两个命名参数(averaged_samples
和gradient_penalty_weight
) ,除了传递一个位置参数(转到y_true
)并寻找缺少的第二个位置参数y_pred
。