Skip to content

Maxout Layer #805

Closed
Closed
@vzhong

Description

@vzhong

Are there plans for a maxout layer? For example:

class Maxout(nn.Module):

    def __init__(self, d_in, d_out, pool_size):
        super().__init__()
        self.d_in, self.d_out, self.pool_size = d_in, d_out, pool_size
        self.lin = Linear(d_in, d_out * pool_size)

    def forward(self, inputs):
        shape = list(inputs.size())
        shape[-1] = self.d_out
        shape.append(self.pool_size)
        last_dim = len(shape) - 1
        out = self.lin(inputs)
        m, i = out.view(*shape).max(last_dim)
        return m.squeeze(last_dim)

EDIT: it seems like people are still requesting Maxout in 2021. Please do not use this implementation as it is for a very very old version of PyTorch and (probably) no longer works.

Activity

soumith

soumith commented on Feb 21, 2017

@soumith
Member

as your example itself shows, it's simple to write by the user itself.
We're not sure it's common enough to write a reference layer in the pytorch core.
For now, we're probably not going to add it, unless a lot of folks think otherwise.

erogol

erogol commented on Jun 28, 2017

@erogol

For ones who need Maxout, I changed the above code to make it work.

class Maxout(nn.Module):

    def __init__(self, d_in, d_out, pool_size):
        super().__init__()
        self.d_in, self.d_out, self.pool_size = d_in, d_out, pool_size
        self.lin = nn.Linear(d_in, d_out * pool_size)


    def forward(self, inputs):
        shape = list(inputs.size())
        shape[-1] = self.d_out
        shape.append(self.pool_size)
        max_dim = len(shape) - 1
        out = self.lin(inputs)
        m, i = out.view(*shape).max(max_dim)
        return m
kkorovesis

kkorovesis commented on Dec 17, 2017

@kkorovesis

Can you please explain how this works please ? I have a 2D tensor off size [128,600] and I want to get it through a Maxout Layer.

added a commit that references this issue on Mar 30, 2018
lucasb-eyer

lucasb-eyer commented on May 16, 2018

@lucasb-eyer
Contributor

And here's a version that is a pure non-linearity applied on the last dimension, this way it can be used with more than just Linears: convs of any dimension, time-series, etc. But it's almost trivial, honestly, so I agree with not including it in the lib to avoid bloat.

class Maxout(nn.Module):
    def __init__(self, pool_size):
        super().__init__()
        self._pool_size = pool_size

    def forward(self, x):
        assert x.shape[-1] % self._pool_size == 0, \
            'Wrong input last dim size ({}) for Maxout({})'.format(x.shape[-1], self._pool_size)
        m, i = x.view(*x.shape[:-1], x.shape[-1] // self._pool_size, self._pool_size).max(-1)
        return m

Example use:

torch.arange(3*6).view(3,6)
#tensor([[  0,   1,   2,   3,   4,   5],
#        [  6,   7,   8,   9,  10,  11],
#        [ 12,  13,  14,  15,  16,  17]])

Maxout(3)(torch.arange(3*6).view(3,6))
#tensor([[  2,   5],
#        [  8,  11],
#        [ 14,  17]])
davidtvs

davidtvs commented on Feb 4, 2019

@davidtvs

Actually, Maxout is applied to the channel dimension which in PyTorch is dimension 1 and not the last:

class Maxout(nn.Module):
    def __init__(self, pool_size):
        super().__init__()
        self._pool_size = pool_size

    def forward(self, x):
        assert x.shape[1] % self._pool_size == 0, \
            'Wrong input last dim size ({}) for Maxout({})'.format(x.shape[1], self._pool_size)
        m, i = x.view(*x.shape[:1], x.shape[1] // self._pool_size, self._pool_size, *x.shape[2:]).max(2)
        return m
shamoons

shamoons commented on Mar 24, 2020

@shamoons

If ReLU6 made it into the core, I should think that max out would as well.

paniabhisek

paniabhisek commented on May 29, 2020

@paniabhisek

Here, you can find both variant(Linear and Conv). The Linear version is more efficient than presented above.

added a commit that references this issue on Sep 20, 2021
cowwoc

cowwoc commented on Oct 12, 2021

@cowwoc

Honestly, this is a mess. TensorFlow provides a very simple API: https://www.tensorflow.org/addons/api_docs/python/tfa/layers/Maxout

Some of the implementations I see for PyTorch insert Linear layers under the hood, others apply an activation against preexisting layers. Some of them invoke Tensor.view() with 3 components, others with 4. It's hard to make heads and tails of which is correct, and why.

I am looking for a drop-in replacement for torch.nn.ReLU and torch.nn.functional.relu(). Can anyone provide that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @cowwoc@shamoons@soumith@erogol@lucasb-eyer

        Issue actions

          Maxout Layer · Issue #805 · pytorch/pytorch