如何裁剪Keras中图层返回的值? [英] How can I clip the values returned by a layer in Keras?

查看:251
本文介绍了如何裁剪Keras中图层返回的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何裁剪Lambda层返回的值?

我尝试使用这个:

from keras.backend.tensorflow_backend import clip
from keras.layers.core import Lambda

...
model.add(Dense(1))
model.add(Activation('linear'))
model.add(Lambda(lambda x: clip(x, min_value=200, max_value=1000)))

但是我放在Lambda +剪辑上的位置并不重要,它不会影响任何东西吗?

But it does not matter where I put my Lambda+clip, it does not affect anything?

推荐答案

实际上,它必须在model.compile步骤中实现为损失.

It actually has to be implemented as loss, at the model.compile step.

from keras import backend as K

def clipped_mse(y_true, y_pred):
    return K.mean(K.square(K.clip(y_pred, 0., 1900.) - K.clip(y_true, 0., 1900.)), axis=-1)

model.compile(loss=clipped_mse)

实际上,事后看来,我认为这可能不是正确的方法.实际上,这意味着我们不会因过高的值而增加惩罚,这与我们想要的相反.

Actually, now in hindsight I think that this might not be the right approach. This actually means we do not add penalty for going over too high of a values - it's in a way the opposite of what we want.

这篇关于如何裁剪Keras中图层返回的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆