-
Notifications
You must be signed in to change notification settings - Fork 17
/
maxout.py
49 lines (40 loc) · 1.64 KB
/
maxout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import numpy as np
import tensorflow as tf
"""
Maxout OP from https://arxiv.org/abs/1302.4389
Max pooling is performed in given filter/channel dimension. This can also be
used after fully-connected layers to reduce number of features.
Args:
inputs: A Tensor on which maxout will be performed
num_units: Specifies how many features will remain after max pooling at the
channel dimension. This must be multiple of number of channels.
axis: The dimension where max pooling will be performed. Default is the
last dimension.
outputs_collections: The collections to which the outputs are added.
scope: Optional scope for name_scope.
Returns:
A `Tensor` representing the results of the pooling operation.
Raises:
ValueError: if num_units is not multiple of number of features.
"""
def max_out(inputs, num_units, axis=None):
shape = inputs.get_shape().as_list()
if shape[0] is None:
shape[0] = -1
if axis is None: # Assume that channel is the last dimension
axis = -1
num_channels = shape[axis]
if num_channels % num_units:
raise ValueError('number of features({}) is not '
'a multiple of num_units({})'.format(num_channels, num_units))
shape[axis] = num_units
shape += [num_channels // num_units]
outputs = tf.reduce_max(tf.reshape(inputs, shape), -1, keep_dims=False)
return outputs
if __name__ == '__main__':
with tf.Session() as sess:
x = tf.Variable(np.random.uniform(size=(25, 10, 500)))
y = tf.square(x)
mo = max_out(x, 5, axis=2)
sess.run(tf.global_variables_initializer())
print(mo.eval().shape)