Search code examples
pythonkerasclipping

How to clip a tensor in Keras, with a different clipping value for each subtensor?


I have a 4d tensor W (the kernel of a 2d convolution) and a 1d array/tensor H specifying the clipping values for each filter. This means that I have

len(H) == W.shape[-1]

and I want all elements of W[:,:,:,i] to be clipped between -H[i] and H[i].

I have looked at the keras.backend.clip, which says you can specify the min and max values as tensors, but you cannot specify the axis. I would appreciate if anyone can show me how to achieve this. Thanks in advance.


Solution

  • Put this inside some layer:

    H = K.reshape(H, (1, 1, 1, -1))
    topClip = K.minimum(H, W)
    clipped = K.maximum(-H, topClip)
    

    As commented by Diego Palacios, although clip accepts tensors, it accepts only tensors with 1 element.