|
41 | 41 |
|
42 | 42 |
|
43 | 43 | class MLP(nn.Module): |
44 | | - """Multi-Layer Perceptron for time series forecasting. |
| 44 | + """Multi-Layer Perceptron for time series forecasting. |
45 | 45 |
|
46 | | - A feedforward neural network with configurable depth and width. The network |
47 | | - consists of an input layer, multiple hidden layers with activation functions |
48 | | - and dropout, and an output layer. All hidden layers have the same dimensionality. |
| 46 | + A feedforward neural network with configurable depth and width. The network |
| 47 | + consists of an input layer, multiple hidden layers with activation functions |
| 48 | + and dropout, and an output layer. All hidden layers have the same dimensionality. |
49 | 49 |
|
50 | | - Args: |
51 | | - in_features (int): Dimension of input features. |
52 | | - out_features (int): Dimension of output features. |
53 | | - activation (str): Activation function name. Must be one of the supported |
54 | | - activations in ACTIVATIONS list (e.g., 'ReLU', 'Tanh', 'GELU', 'ELU'). |
55 | | - hidden_size (int): Number of units in each hidden layer. All hidden layers |
56 | | - share the same dimensionality. |
57 | | - num_layers (int): Total number of layers including input and output layers. |
58 | | - Must be at least 2. For example, num_layers=3 creates: input layer, |
59 | | - one hidden layer, and output layer. |
60 | | - dropout (float): Dropout probability applied after each hidden layer's |
| 50 | + Args: |
| 51 | + in_features (int): Dimension of input features. |
| 52 | + out_features (int): Dimension of output features. |
| 53 | + activation (str): Activation function name. Must be one of the supported |
| 54 | + activations in ACTIVATIONS list (e.g., 'ReLU', 'Tanh', 'GELU', 'ELU'). |
| 55 | + Ignored when num_layers=1. |
| 56 | + hidden_size (int): Number of units in each hidden layer. All hidden layers |
| 57 | + share the same dimensionality. Ignored when num_layers=1. |
| 58 | + num_layers (int): Total number of layers including input and output layers. |
| 59 | + Use num_layers=1 for a direct linear projection with no hidden layers or |
| 60 | + activation. For num_layers>=2, creates: input layer, (num_layers-2) hidden |
| 61 | + layers, and output layer. |
| 62 | + dropout (float): Dropout probability applied after each hidden layer's |
61 | 63 | activation. Should be in range [0.0, 1.0]. Not applied to output layer. |
| 64 | + Ignored when num_layers=1. |
62 | 65 |
|
63 | 66 | Returns: |
64 | 67 | (torch.Tensor): Transformed output tensor of shape [..., out_features]. |
65 | 68 |
|
66 | | - Notes: |
67 | | - - The activation function is applied after each hidden layer's linear |
68 | | - transformation, but not after the final output layer. |
69 | | - - Dropout is applied after activation in hidden layers for regularization. |
70 | | - - This MLP is used as a decoder component in various forecasting models |
71 | | - including RNN, LSTM, GRU, DilatedRNN, TCN, and xLSTM. |
| 69 | + Notes: |
| 70 | + - The activation function is applied after each hidden layer's linear |
| 71 | + transformation, but not after the final output layer. |
| 72 | + - Dropout is applied after activation in hidden layers for regularization. |
| 73 | + - This MLP is used as a decoder component in various forecasting models |
| 74 | + including RNN, LSTM, GRU, DilatedRNN, TCN, xLSTM, and DeepAR. |
72 | 75 | """ |
73 | 76 |
|
74 | 77 | def __init__( |
75 | 78 | self, in_features, out_features, activation, hidden_size, num_layers, dropout |
76 | 79 | ): |
77 | 80 | super().__init__() |
78 | | - assert activation in ACTIVATIONS, f"{activation} is not in {ACTIVATIONS}" |
79 | 81 |
|
80 | | - self.activation = getattr(nn, activation)() |
| 82 | + if num_layers == 1: |
| 83 | + # Direct linear projection with no hidden layers or activation |
| 84 | + self.layers = nn.Sequential( |
| 85 | + nn.Linear(in_features=in_features, out_features=out_features) |
| 86 | + ) |
| 87 | + else: |
| 88 | + assert activation in ACTIVATIONS, f"{activation} is not in {ACTIVATIONS}" |
| 89 | + self.activation = getattr(nn, activation)() |
81 | 90 |
|
82 | | - # MultiLayer Perceptron |
83 | | - # Input layer |
84 | | - layers = [ |
85 | | - nn.Linear(in_features=in_features, out_features=hidden_size), |
86 | | - self.activation, |
87 | | - nn.Dropout(dropout), |
88 | | - ] |
89 | | - # Hidden layers |
90 | | - for i in range(num_layers - 2): |
91 | | - layers += [ |
92 | | - nn.Linear(in_features=hidden_size, out_features=hidden_size), |
| 91 | + # MultiLayer Perceptron |
| 92 | + # Input layer |
| 93 | + layers = [ |
| 94 | + nn.Linear(in_features=in_features, out_features=hidden_size), |
93 | 95 | self.activation, |
94 | 96 | nn.Dropout(dropout), |
95 | 97 | ] |
96 | | - # Output layer |
97 | | - layers += [nn.Linear(in_features=hidden_size, out_features=out_features)] |
98 | | - |
99 | | - # Store in layers as ModuleList |
100 | | - self.layers = nn.Sequential(*layers) |
| 98 | + # Hidden layers |
| 99 | + for i in range(num_layers - 2): |
| 100 | + layers += [ |
| 101 | + nn.Linear(in_features=hidden_size, out_features=hidden_size), |
| 102 | + self.activation, |
| 103 | + nn.Dropout(dropout), |
| 104 | + ] |
| 105 | + # Output layer |
| 106 | + layers += [nn.Linear(in_features=hidden_size, out_features=out_features)] |
| 107 | + |
| 108 | + # Store in layers as ModuleList |
| 109 | + self.layers = nn.Sequential(*layers) |
101 | 110 |
|
102 | 111 | def forward(self, x): |
103 | 112 | return self.layers(x) |
|
0 commit comments