summaryrefslogtreecommitdiff
path: root/src/text_recognizer/networks/mlp.py
blob: d704d99e3f19feada9eda07a77c69361f42375b8 (plain)
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
"""Defines the MLP network."""
from typing import Callable, Dict, List, Optional, Union

import torch
from torch import nn


class MLP(nn.Module):
    """Multi layered perceptron network."""

    def __init__(
        self,
        input_size: int = 784,
        output_size: int = 10,
        hidden_size: Union[int, List] = 128,
        num_layers: int = 3,
        dropout_rate: float = 0.2,
        activation_fn: Optional[Callable] = None,
        activation_fn_args: Optional[Dict] = None,
    ) -> None:
        """Initialization of the MLP network.

        Args:
            input_size (int): The input shape of the network. Defaults to 784.
            output_size (int): Number of classes in the dataset. Defaults to 10.
            hidden_size (Union[int, List]): The number of `neurons` in each hidden layer. Defaults to 128.
            num_layers (int): The number of hidden layers. Defaults to 3.
            dropout_rate (float): The dropout rate at each layer. Defaults to 0.2.
            activation_fn (Optional[Callable]): The activation function in the hidden layers. Defaults to
                None.
            activation_fn_args (Optional[Dict]): The arguments for the activation function. Defaults to None.

        """
        super().__init__()

        if activation_fn is not None:
            activation_fn = getattr(nn, activation_fn)(activation_fn_args)
        else:
            activation_fn = nn.ReLU(inplace=True)

        if isinstance(hidden_size, int):
            hidden_size = [hidden_size] * num_layers

        self.layers = [
            nn.Linear(in_features=input_size, out_features=hidden_size[0]),
            activation_fn,
        ]

        for i in range(num_layers - 1):
            self.layers += [
                nn.Linear(in_features=hidden_size[i], out_features=hidden_size[i + 1]),
                activation_fn,
            ]

            if dropout_rate:
                self.layers.append(nn.Dropout(p=dropout_rate))

        self.layers.append(
            nn.Linear(in_features=hidden_size[-1], out_features=output_size)
        )

        self.layers = nn.Sequential(*self.layers)

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """The feedforward."""
        x = torch.flatten(x, start_dim=1)
        return self.layers(x)

    @property
    def __name__(self) -> str:
        """Returns the name of the network."""
        return "mlp"