Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pooling #55

Open
ngragaei opened this issue Dec 9, 2019 · 0 comments
Open

pooling #55

ngragaei opened this issue Dec 9, 2019 · 0 comments

Comments

@ngragaei
Copy link

ngragaei commented Dec 9, 2019

I want to use gated pooling instead of max pooling, but i do not know how to tune the pooling gate function with the code. any help, please

`def gated_pooling(inputs, filter, size=2, learn_option='l/c'):
"""Gated pooling operation, responsive
Combine max pooling and average pooling in a mixing proportion,
which is obtained from the inner product between the gating mask and the region being
pooled and then fed through a sigmoid:
fgate(x) = sigmoid(wx) fmax(x) + (1-sigmoid(wx)) favg(x)

   arguments:
     inputs: input of shape [batch size, height, width, channels]
     filter: filter size of the input layer, used to initialize gating mask
     size: an integer, width and height of the pooling filter
     learn_option: learning options of gated pooling, include:
                    'l/c': learn a mask per layer/channel
                    'l/r/c': learn a mask per layer/pooling region/channel combined
   return:
     outputs: tensor with the shape of [batch_size, height//size, width//size, channels]

"""
if learn_option == 'l':
    gating_mask = all_channel_connected2d(inputs)
if learn_option == 'l/c':
    w_gated = tf.Variable(tf.truncated_normal([size,size,filter,filter], stddev=2/(size*size*filter*2)**0.5))
    gating_mask = tf.nn.conv2d(inputs, w_gated, strides=[1,size,size,1], padding='VALID')
if learn_option == 'l/r/c':
    gating_mask = locally_connected2d(inputs)

alpha = tf.sigmoid(gating_mask)

x1 = tf.contrib.layers.max_pool2d(inputs=inputs, kernel_size=[size, size], stride=2, padding='VALID')
x2 = tf.contrib.layers.avg_pool2d(inputs=inputs, kernel_size=[size, size],stride=2, padding='VALID')
outputs = tf.add(tf.multiply(x1, alpha), tf.multiply(x2, (1-alpha)))
return outputs`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant