Search code examples
pythontensorflowkerasconv-neural-networkmax-pooling

I want to add some error percentage to the output of max-pooling layer?


I want to add some error percentage (relative error) to the output of max-pooling layer in CNN. I am using max pooling layer from keras. Below is the code

i = Input(shape=x_train[0].shape)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(i)
x = BatchNormalization()(x)
x = Conv2D(32, (3, 3), activation='relu', padding='same')(x)
x = BatchNormalization()(x)
x = MaxPooling2D((2, 2))(x)

How can I add some errors to the output of this layer? I want to add some fraction of the original output. e.g. if x is my original output, I want my output to be x+ some fraction of (x).

Thanks in advance.


Solution

  • If you just want to add a fraction of the input, you can just use Add:

    x = K.layers.Add()([x, 1/4 * x])
    

    for example:

    input = K.layers.Input(shape=(5,))
    x = K.layers.Add()([input, 1/4 * input])
    model = K.Model(inputs=[input], outputs=[x])
    model(np.ones((1,5)))
    #<tf.Tensor: shape=(1, 5), dtype=float32, numpy=array([[1.25, 1.25, 1.25, 1.25, 1.25]], dtype=float32)>
    

    However this is not noise, and the affine transformation after this layer will vanish what you have done, infact:

    A(x+ 1/4x)+b
    = A(5/4x)+b
    = 5/4 * A(x)+b
    

    so you are not adding any "additional expressivity" to your network

    if you clarify what noise (wrt to a a fraction of the input) you want, I'll fix my answer