首先辨析一下概念:
1. loss是整体网络进行优化的目标, 是需要参与到优化运算,更新权值W的过程的
2. metric只是作为评价网络表现的一种“指标”, 比如accuracy,是为了直观地了解算法的效果,充当view的作用,并不参与到优化过程
在keras中实现自定义loss, 可以有两种方式,一种自定义 loss function, 例如:
# 方式一
def vae_loss(x, x_decoded_mean):
xent_loss = objectives.binary_crossentropy(x, x_decoded_mean)
kl_loss = - 0.5 * K.mean(1 + z_log_sigma - K.square(z_mean) - K.exp(z_log_sigma), axis=-1)
return xent_loss + kl_loss
pile(optimizer='rmsprop', loss=vae_loss)
或者通过自定义一个keras的层(layer)来达到目的, 作为model的最后一层,最后令pile中的loss=None:
# 方式二
# Custom loss layer
class CustomVariationalLayer(Layer):
def __init__(self, **kwargs):
self.is_placeholder = True
super(CustomVariationalLayer, self).__init__(**kwargs)
def vae_loss(self, x, x_decoded_mean_squash):
x = K.flatten(x)
x_decoded_mean_squash = K.flatten(x_decoded_mean_squash)
xent_loss = img_rows * img_cols * metrics.binary_crossentropy(x, x_decoded_mean_squash)
kl_loss = - 0.5 * K.mean(1 + z_log_var - K.square(z_mean) - K.exp(z_log_var), axis=-1)
return K.mean(xent_loss + kl_loss)
def call(self, inputs):
x = inputs[0]
x_decoded_mean_squash = inputs[1]
loss = self.vae_loss(x, x_decoded_mean_squash)
self.add_loss(loss, inputs=inputs)
# We don't use this output.
return x
y = CustomVariationalLayer()([x, x_decoded_mean_squash])
vae = Model(x, y)
pile(optimizer='rmsprop', loss=None)
在keras中自定义metric非常简单,需要用y_pred和y_true作为自定义metric函数的输入参数 点击查看metric的设置
注意事项:
1. keras中定义loss,返回的是batch_size长度的tensor, 而不是像tensorflow中那样是一个scalar
2. 为了能够将自定义的loss保存到model, 以及可以之后能够顺利load model, 需要把自定义的loss拷贝到keras.losses.py 源代码文件下,否则运行时找不到相关信息,keras会报错
有时需要不同的sample的loss施加不同的权重,这时需要用到sample_weight,例如
# Class weights:
# To balance the difference in occurences of digit class labels.
# 50% of labels that the discriminator trains on are 'fake'.
# Weight = 1 / frequency
cw1 = {0: 1, 1: 1}
cw2 = {i: self.num_classes / half_batch for i in range(self.num_classes)}
cw2[self.num_classes] = 1 / half_batch
class_weights = [cw1, cw2] # 使得两种loss能够一样重要
discriminator.train_on_batch(imgs, [valid, labels], class_weight=class_weights)
---------------------
作者:ustc_lijia
来源:CSDN
原文:/xiaojiajia007/article/details/73274669
版权声明:本文为博主原创文章,转载请附上博文链接!