AI Logic

Activation Functions

Maxout

Updated:

Graph of Maxout for a one-dimensional input. With k = 2, the maximum of the lines 0 and x gives ReLU; with k = 4, the maximum of four lines approximates the dashed curve xΒ²/4 with a piecewise-linear function. The dots are the kinks where the maximal line switches

What is Maxout?

Maxout is an activation function in which each unit computes kk linear outputs (weighted sums plus biases) and outputs their maximum. It was designed to be used together with Dropout; the name reflects both that it outputs the maximum (max) of a set of inputs and that it is a natural companion to dropout (Goodfellow et al., 2013).

For an input x∈Rdx \in \mathbb{R}^d, the ii-th unit of a Maxout hidden layer outputs:

hi(x)=max⁑j∈[1,k]zij,zij=x⊀Wβ‹…β‹…ij+bijh_i(x) = \max_{j \in [1, k]} z_{ij}, \qquad z_{ij} = x^\top W_{\cdot\cdot ij} + b_{ij}
  • kk: the number of linear outputs pooled by one unit, set by hand before training.
  • W∈RdΓ—mΓ—kW \in \mathbb{R}^{d \times m \times k}, b∈RmΓ—kb \in \mathbb{R}^{m \times k}: the learnable parameters, where mm is the number of units.

Whereas ReLU and similar functions apply a fixed-shape function to a linear output, Maxout has no fixed-shape function. The weights decide which line is the largest, so the network learns the shape of the activation function itself.

For a one-dimensional input, Maxout is the maximum of kk lines, that is, a convex piecewise-linear function. The examples in the graph at the top take these values:

Input xxβˆ’3βˆ’2023
k=2k = 2: max⁑(0, x)\max(0,\, x)00023
k=4k = 4: maximum of 4 tangent lines2.250.75βˆ’0.250.752.25
Target curve x2/4x^2/42.251012.25

The first row uses the two lines 00 (weight 0, bias 0) and xx, which gives the same shape as ReLU. Using the lines xx and βˆ’x-x gives the absolute value ∣x∣|x|. The second row is the maximum of the four lines tangent to x2/4x^2/4 at x=βˆ’3,βˆ’1,1,3x = -3, -1, 1, 3; adding more kinks brings it closer to the curve.

In a convolutional network, Maxout takes the maximum over the values at the same position in kk feature maps to produce one Maxout feature map. This is the same computation as max pooling applied along the channel dimension.

Maxout outputs are not sparse (they do not contain many zeros). ReLU sets negative inputs to 0, but Maxout has no fixed part that outputs 0, and the output distribution in the paper (Figure 2) is not concentrated at 0.

What shapes can it represent?

A single Maxout unit can approximate any convex function with a piecewise-linear function. Increasing kk provides more lines and a finer approximation. However, the maximum of lines is always convex, so a single unit cannot produce non-convex shapes (e.g., a hump or an S-curve).

The network as a whole can represent non-convex functions as well. The paper combines the following two facts to show that two Maxout units suffice to approximate any continuous function arbitrarily well on a bounded closed region (a compact set) (Theorem 4.3).

  1. A continuous function can be approximated arbitrarily well by a piecewise-linear function (Stone–Weierstrass approximation theorem).
  2. A continuous piecewise-linear function can be written as the difference of two convex piecewise-linear functions, g(v)=h1(v)βˆ’h2(v)g(v) = h_1(v) - h_2(v) (Wang, 2004).

Making h1h_1 and h2h_2 Maxout units and setting the output-layer weights to 11 and βˆ’1-1 produces their difference directly. The finer the approximation, the larger the required kk.

This result shows only that such functions can be represented; it does not guarantee that training with few units will succeed. The actual experiments use networks with many units.

Why does it work well with dropout?

The paper gives two reasons why Maxout is strong when combined with Dropout.

1. The test-time approximation of the average becomes accurate

Dropout trains a "submodel" with some units removed at each step, and at test time approximates the average of many submodels with a single model whose weights are halved. This approximation is exact for single-layer softmax regression and remains exact for deeper stacks if every layer is linear.

Maxout is locally linear almost everywhere. According to the paper, training with Dropout makes it unlikely that the maximal line changes when the mask changes; in other words, the network learns to have wide linear regions around the training data. On MNIST, the difference (KL divergence) between the geometric mean of sampled submodel predictions and the prediction with halved weights was smaller for Maxout than for Tanh (Figure 8).

2. Gradients always flow, so training rarely stalls

The 0 in ReLU's max⁑(0,z)\max(0, z) is a constant, so no gradient flows while the unit outputs 0, and the unit struggles to recover (the dying ReLU problem). The paper observed that under ordinary stochastic gradient descent fewer than 5% of ReLU units were stuck at 0, but with Dropout this rose to 60%. The maximum in Maxout is always the value of one of the lines, which is a function of the parameters, so the gradient always flows into one of the lines.

The only difference between Maxout and "channel-wise max pooling applied to ReLU outputs" is whether 0 is included among the candidates for the maximum. In the paper's comparisons, this 0 substantially hindered training.

  • MLP on MNIST: The validation error was 1.04% with Maxout and above 1.2% when 0 was added to the candidates.
  • Unused filters: In a 2-layer MLP with 1200 filters per layer (pooled in groups of 5), adding 0 left 17.6% of the filters in layer 1 and 39.2% in layer 2 unused, while Maxout used all but 2 of the 2400 filters.
  • Training error of small models: Training a 2-layer convolutional network (k=2k = 2, 16 kernels) on SVHN, ReLU stalled at a training error of 7.3%, while Maxout reached 5.1%.
  • Depth: When a narrow network with 80 units per layer (k=5k = 5) was made deeper on MNIST, ReLU degraded noticeably at 6 layers and sharply at 7, while Maxout degraded only gradually (Figure 9).

Experimental results

Combining Maxout with Dropout, the paper achieved the best error rates at the time on four image classification datasets. All values are test error rates (lower is better).

DatasetSettingBest compared methodMaxout + Dropout
MNIST (permutation invariant)2 fully connected Maxout layersReLU MLP + Dropout 1.05%0.94%
MNIST3 convolutional Maxout layers, no data augmentationStochastic pooling 0.47%0.45%
CIFAR-10No data augmentationCNN + Spearmint 14.98%11.68%
CIFAR-10Translation and horizontal-flip augmentationCNN + Spearmint + augmentation 9.50%9.38%
CIFAR-100CIFAR-10 hyperparameters reused as isStochastic pooling 42.51%38.57%
SVHNβ€”ReLU + Dropout + synthetic translations 2.68%2.47%
  • Permutation-invariant MNIST: This setting does not use the 2D structure of the images. Maxout was the best among methods without unsupervised pretraining, but did not reach methods that regularize by modeling the input distribution (such as DBM + Dropout at 0.79%).
  • CIFAR-10: The model was retrained using the validation data as well. Without using the validation data for training, the error was 13.2%. Dropout lowered Maxout's validation error by more than 25% (Figure 5).
  • Fair comparison with ReLU: On CIFAR-10 with the same preprocessing and cross-validated learning rate and momentum, Maxout outperformed ReLU with channel-wise pooling at the same number of parameters; for ReLU to approach the same performance, the size of the state and the number of parameters had to grow by about kk times (Figure 6).

Practical considerations

Parameters and computation grow by about kk times: Because kk linear outputs are computed for each output, the number of weights and the computation grow about kk-fold compared with a ReLU layer with the same number of outputs. The paper's comparisons show that Maxout is advantageous even after accounting for this increase.

  • Apply the Dropout mask right before the weights: In the paper, the mask is applied to the input of the Maxout layer (before multiplying by the weights); the kk candidates for the maximum themselves are not dropped.
  • A single unit can only form convex shapes: If non-convex shapes are needed, multiple units must be combined. APL is an activation function that can form non-convex shapes with a single unit.
  • Combined with a weight-norm constraint: In the paper's MNIST experiments, in addition to Dropout, an upper bound on the norm of each weight vector was used.
  • No dedicated layer in PyTorch: Implement it by producing kk times as many outputs with a linear (or convolutional) layer and taking the maximum over each group of kk (see the example below).
  • No weight-initialization formula: Schemes such as He initialization are derived assuming a fixed shape such as ReLU. For Maxout, there are examples using LSUV, which passes data through the network and normalizes the output scale.

Comparison with other activation functions and how to choose

The table compares Maxout with activation functions whose shape is or is not learned, from the same viewpoints.

FunctionLearned parametersShapes one unit can representAdded parameters
ReLUNoneFixed shape with a kink at 0None
PReLUNegative-side slopePiecewise-linear with a slope on the negative sideOne per channel, etc.
APLHinge positions and slopesPiecewise-linear, including non-convex shapes under some conditions2S2S per unit
MaxoutWeights and biases of kk linear outputsAny convex piecewise-linear function (multidimensional input allowed)About kk times the layer's weights

Rough guidelines for choosing:

  • Start with ReLU: It is cheap and gives sufficient accuracy on many tasks.
  • Try Maxout when you want higher accuracy from a shallow network with strong Dropout: The paper's results were obtained in combination with Dropout. Maxout with Dropout was also used in the discriminator of the original GAN.
  • Try PReLU or APL when you do not want more parameters or computation: Maxout multiplies the weights of the whole layer by about kk, while PReLU and APL add only a small number of parameters for the activation function.

PyTorch example

Show code
python
import torch
import torch.nn as nn


class Maxout(nn.Module):
    """Fully connected Maxout layer (Goodfellow et al., 2013).

    h_i(x) = max_j (x^T W_ij + b_ij), j = 1..k
    A linear layer produces out_features Γ— k values, and the maximum is taken over each group of k.
    """

    def __init__(self, in_features: int, out_features: int, k: int = 2):
        super().__init__()
        self.out_features = out_features
        self.k = k
        self.linear = nn.Linear(in_features, out_features * k)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        z = self.linear(x)                                  # (..., out_features * k)
        z = z.view(*x.shape[:-1], self.out_features, self.k)
        return z.max(dim=-1).values                         # (..., out_features)


class Maxout2d(nn.Module):
    """Convolutional Maxout layer. Takes the maximum at each position over k feature maps (channel-wise max pooling)."""

    def __init__(self, in_channels: int, out_channels: int, k: int = 2, **conv_kwargs):
        super().__init__()
        self.out_channels = out_channels
        self.k = k
        self.conv = nn.Conv2d(in_channels, out_channels * k, **conv_kwargs)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        z = self.conv(x)                                    # (N, out_channels * k, H, W)
        n, _, h, w = z.shape
        return z.view(n, self.out_channels, self.k, h, w).max(dim=2).values


# As in the paper, Dropout is applied to the input of each Maxout layer (before the weights)
model = nn.Sequential(
    nn.Dropout(0.2),
    Maxout(784, 240, k=5),
    nn.Dropout(0.5),
    Maxout(240, 240, k=5),
    nn.Dropout(0.5),
    nn.Linear(240, 10),
)

x = torch.randn(32, 784)
model.train()   # Dropout is active
y_train = model(x)
model.eval()    # Dropout is off; this is the test-time computation (Maxout itself is unchanged)
y_test = model(x)
print(y_test.shape)  # torch.Size([32, 10])

FAQ

Q. Can Maxout be used as a drop-in replacement for ReLU?
A.

Yes, but with the same number of outputs, the layer's weights and computation grow by about kk times. With k=2k = 2 and one line fixed to 00, it has the same shape as ReLU, so Maxout can be seen as a broader family of functions that includes ReLU.

Q. What value of k should I use?
A.

The paper uses, for example, k=5k = 5 in the MNIST depth experiment and k=2k = 2 for the small SVHN model. A larger kk allows finer shapes but increases weights and computation accordingly. The standard approach is to choose it on validation data.

Q. Does Maxout work without Dropout?
A.

It can be used, but the paper's claims and results assume the combination with Dropout. On CIFAR-10, Dropout lowered Maxout's validation error by more than 25%. The paper's comparisons with ReLU were also run with Dropout.

References

  1. Goodfellow, I. J., Warde-Farley, D., Mirza, M., Courville, A., & Bengio, Y. (2013). Maxout Networks. ICML 2013.