netloader 3.11.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,298 @@
1
+ """
2
+ Pooling network layers
3
+ """
4
+ from typing import Any, Type, Literal, cast
5
+
6
+ from torch import nn, Tensor
7
+
8
+ from netloader.layers.misc import Pad
9
+ from netloader.layers.base import BaseSingleLayer
10
+ from netloader.utils import Shapes, compare_versions
11
+ from netloader.layers.utils import _kernel_shape, _padding
12
+
13
+
14
+ class AdaptivePool(BaseSingleLayer):
15
+ """
16
+ Uses pooling to downscale the input to the desired shape.
17
+
18
+ Attributes
19
+ ----------
20
+ group : int
21
+ Layer group, if 0 it will always be used, else it will only be used if its group matches the
22
+ Networks
23
+ description : str
24
+ Description of the layer
25
+ layers : Sequential
26
+ Layers to loop through in the forward pass
27
+ """
28
+ def __init__(
29
+ self,
30
+ shape: int | list[int],
31
+ shapes: Shapes,
32
+ *,
33
+ channels: bool = True,
34
+ mode: Literal['average', 'max'] = 'average',
35
+ **kwargs: Any) -> None:
36
+ """
37
+ Parameters
38
+ ----------
39
+ shape : int | list[int]
40
+ Output shape of the layer
41
+ shapes : list[list[int]]
42
+ Shape of the outputs from each layer
43
+ channels : bool, Optional
44
+ If the input includes a channels dimension, default = True
45
+ mode : {'average', 'max'}
46
+ Whether to use 'max' or 'average' pooling
47
+
48
+ **kwargs
49
+ Leftover parameters to pass to base layer for checking
50
+ """
51
+ super().__init__(**kwargs)
52
+ self._channels: bool = channels
53
+ self._mode: str = mode
54
+ adapt_pool: Type[nn.Module]
55
+
56
+ self._check_pool_shape(shapes[-1])
57
+ shape = self._check_adapt_pool(shapes[-1], shape)
58
+ self._check_options('mode', mode, {None, 'average', 'max'})
59
+
60
+ adapt_pool = [
61
+ [nn.AdaptiveMaxPool1d, nn.AdaptiveAvgPool1d],
62
+ [nn.AdaptiveMaxPool2d, nn.AdaptiveAvgPool2d],
63
+ [nn.AdaptiveMaxPool3d, nn.AdaptiveAvgPool3d],
64
+ ][len(shapes[-1]) - self._channels - 1][self._mode == 'average']
65
+ self.layers.add_module('AdaptivePool', adapt_pool(shape))
66
+
67
+ assert isinstance(shape, list)
68
+ shapes.append(shapes[-1].copy())
69
+ shapes[-1][self._channels:] = shape
70
+
71
+ def __getstate__(self) -> dict[str, Any]:
72
+ return super().__getstate__() | {
73
+ 'channels': self._channels,
74
+ 'mode': self._mode,
75
+ 'shape': self.layers.AdaptivePool.output_size, # type: ignore[union-attr]
76
+ }
77
+
78
+ def _check_pool_shape(self, shape: list[int]) -> None:
79
+ """
80
+ Checks if the input shape has more than 3 dimensions + channels or fewer than 1 + channels.
81
+
82
+ Parameters
83
+ ----------
84
+ shape : list[int]
85
+ Input shape
86
+ """
87
+ if len(shape) - self._channels > 3 or len(shape) - self._channels < 1:
88
+ raise ValueError(f'Tensors with more than 3 dimensions + channels or less than 1 + '
89
+ f'channels is not supported, input shape is {shape} and channels is '
90
+ f'{bool(self._channels)}.')
91
+
92
+ def _check_adapt_pool(self, in_shape: list[int], shape: int | list[int]) -> list[int]:
93
+ """
94
+ Checks if the target shape is compatible with the input shape and calculates the output
95
+ shape.
96
+
97
+ Parameters
98
+ ----------
99
+ in_shape : list[int]
100
+ Input shape
101
+ shape : int | list[int]
102
+ Target shape
103
+
104
+ Returns
105
+ -------
106
+ list[int]
107
+ Output shape
108
+ """
109
+ if isinstance(shape, int):
110
+ shape = [shape] * (len(in_shape) - self._channels)
111
+ elif len(shape) == 1:
112
+ shape = shape * (len(in_shape) - self._channels)
113
+ elif len(shape) != len(in_shape) - self._channels:
114
+ raise ValueError(f'Target shape {shape} does not match the input shape {in_shape} if '
115
+ f'channels is {bool(self._channels)}, output dimensions must be '
116
+ f'either 1, or {len(in_shape) - self._channels}')
117
+ return shape
118
+
119
+ def extra_repr(self) -> str:
120
+ """
121
+ Displays layer parameters when printing the network
122
+
123
+ Returns
124
+ -------
125
+ str
126
+ Layer parameters
127
+ """
128
+ return f'channels={bool(self._channels)}, mode={self._mode}'
129
+
130
+
131
+ class Pool(BaseSingleLayer):
132
+ """
133
+ Constructs a max or average pooling layer.
134
+
135
+ Supports 1D, 2D, and 3D pooling.
136
+
137
+ Attributes
138
+ ----------
139
+ group : int
140
+ Layer group, if 0 it will always be used, else it will only be used if its group matches the
141
+ Networks
142
+ description : str
143
+ Description of the layer
144
+ layers : Sequential
145
+ Layers to loop through in the forward pass
146
+ """
147
+ def __init__(
148
+ self,
149
+ shapes: Shapes,
150
+ *,
151
+ kernel: int | list[int] = 2,
152
+ stride: int | list[int] = 2,
153
+ padding: int | Literal['same'] | list[int] = 0,
154
+ mode: Literal['max', 'average'] = 'max',
155
+ pool_kwargs: dict[str, Any] | None = None,
156
+ **kwargs: Any) -> None:
157
+ """
158
+ Parameters
159
+ ----------
160
+ shapes : list[list[int]]
161
+ Shape of the outputs from each layer
162
+ kernel : int | list[int], Optional
163
+ Size of the kernel, default = 2
164
+ stride : int | list[int], Optional
165
+ Stride of the kernel, default = 2
166
+ padding : int | {'same'} | list[int], Optional
167
+ Input padding, can an int or 'same' where 'same' preserves the input shape, default = 0
168
+ mode : {'max', 'average'}
169
+ Whether to use 'max' or 'average' pooling
170
+ pool_kwargs : dict[str, Any] | None, Optional
171
+ Additional keyword arguments to pass to the pooling layer
172
+
173
+ **kwargs
174
+ Leftover parameters to pass to base layer for checking
175
+ """
176
+ super().__init__(**kwargs)
177
+ self._mode: Literal['max', 'average'] = mode
178
+ self._pad: int | Literal['same'] | list[int] = padding
179
+ self._kwargs: dict[str, Any] = pool_kwargs or {}
180
+ asymmetric: bool = False
181
+ padding_: int | list[int]
182
+ shape: list[int] = shapes[-1].copy()
183
+ modes: tuple[str, str] = ('max', 'average')
184
+ pool: Type[nn.Module]
185
+
186
+ # Check for errors and calculate same padding
187
+ if isinstance(self._pad, str):
188
+ self._check_options('padding', self._pad, {'same'})
189
+ self._check_stride(stride)
190
+ asymmetric, padding_ = _padding(kernel, stride, shape, shape)
191
+ else:
192
+ padding_ = self._pad
193
+
194
+ if asymmetric and compare_versions(self._ver, '3.10.1'):
195
+ assert isinstance(padding_, list)
196
+ self.layers.add_module('Pad', Pad(tuple(padding_)))
197
+ padding_ = 0
198
+
199
+ # Check for errors
200
+ self._check_shape((1, 4), shape)
201
+ self._check_options('mode', self._mode, set(modes))
202
+
203
+ pool = cast(list[list[Type[nn.Module]]], [
204
+ [nn.MaxPool1d, nn.AvgPool1d],
205
+ [nn.MaxPool2d, nn.AvgPool2d],
206
+ [nn.MaxPool3d, nn.AvgPool3d],
207
+ ])[len(shape) - 2][self._mode == modes[1]]
208
+
209
+ if self._mode == modes[1]:
210
+ self._kwargs = {'count_include_pad': False} | self._kwargs
211
+
212
+ self.layers.add_module('Pool', pool(
213
+ kernel_size=kernel,
214
+ stride=stride,
215
+ padding=padding_,
216
+ **self._kwargs,
217
+ ))
218
+
219
+ if self._pad == 'same' or asymmetric:
220
+ shapes.append(shape)
221
+ else:
222
+ shapes.append(_kernel_shape(kernel, stride, padding_, shape))
223
+
224
+ def __getstate__(self) -> dict[str, Any]:
225
+ return super().__getstate__() | {
226
+ 'kernel': self.layers.Pool.kernel_size, # type: ignore[union-attr]
227
+ 'stride': self.layers.Pool.stride,
228
+ 'padding': self._pad,
229
+ 'mode': self._mode,
230
+ 'pool_kwargs': self._kwargs,
231
+ }
232
+
233
+ def forward(self, x: Tensor, *args: Any, **kwargs: Any) -> Tensor:
234
+ """
235
+ Forward pass of the pool layer.
236
+
237
+ Parameters
238
+ ----------
239
+ x : Tensor
240
+ Input tensor of shape (N,...) and type float, where N is the batch size
241
+ *args
242
+ Optional arguments to pass to the parent forward method
243
+ **kwargs
244
+ Optional keyword arguments to pass to the parent forward method
245
+
246
+ Returns
247
+ -------
248
+ Tensor
249
+ Output tensor of shape (N,...) and type float
250
+ """
251
+ return super().forward(x, *args, **kwargs)
252
+
253
+
254
+ class PoolDownscale(Pool):
255
+ """
256
+ Downscales the input using pooling.
257
+
258
+ Supports 1D, 2D, and 3D pooling.
259
+
260
+ Attributes
261
+ ----------
262
+ group : int
263
+ Layer group, if 0 it will always be used, else it will only be used if its group matches the
264
+ Networks
265
+ description : str
266
+ Description of the layer
267
+ layers : Sequential
268
+ Layers to loop through in the forward pass
269
+ """
270
+ def __init__(
271
+ self,
272
+ scale: int,
273
+ shapes: Shapes,
274
+ *,
275
+ mode: Literal['max', 'average'] = 'max',
276
+ **kwargs: Any) -> None:
277
+ """
278
+ Parameters
279
+ ----------
280
+ scale : int
281
+ Stride and size of the kernel, which acts as the downscaling factor
282
+ shapes : list[list[int]]
283
+ Shape of the outputs from each layer
284
+ mode : {'max', 'average'}
285
+ Whether to use 'max' or 'average' pooling
286
+
287
+ **kwargs
288
+ Leftover parameters to pass to base layer for checking
289
+ """
290
+ super().__init__(shapes=shapes, kernel=scale, stride=scale, padding=0, mode=mode, **kwargs)
291
+
292
+ def __getstate__(self) -> dict[str, Any]:
293
+ state: dict[str, Any] = super().__getstate__()
294
+ state.pop('padding', None)
295
+ return state
296
+
297
+
298
+ __all__ = ['AdaptivePool', 'Pool', 'PoolDownscale']
@@ -0,0 +1,163 @@
1
+ """
2
+ Recurrent network layers
3
+ """
4
+ from typing import Any, Type, Literal
5
+
6
+ import torch
7
+ import numpy as np
8
+ from torch import nn, Tensor
9
+
10
+ from netloader.utils import Shapes
11
+ from netloader.layers.misc import Reshape
12
+ from netloader.layers.base import BaseSingleLayer
13
+
14
+
15
+ class Recurrent(BaseSingleLayer):
16
+ """
17
+ Recurrent layer constructor for either RNN, GRU or LSTM.
18
+
19
+ Attributes
20
+ ----------
21
+ group : int
22
+ Layer group, if 0 it will always be used, else it will only be used if its group matches the
23
+ Networks
24
+ description : str
25
+ Description of the layer
26
+ layers : Sequential
27
+ Layers to loop through in the forward pass
28
+ """
29
+ def __init__(
30
+ self,
31
+ idx: int,
32
+ shapes: Shapes,
33
+ *,
34
+ batch_norm: bool = False,
35
+ layers: int = 2,
36
+ filters: int = 1,
37
+ dropout: float = 0,
38
+ mode: Literal['gru', 'lstm', 'rnn'] = 'gru',
39
+ activation: str | None = 'ELU',
40
+ bidirectional: Literal[None, 'sum', 'mean', 'concatenate'] = None,
41
+ **kwargs: Any) -> None:
42
+ """
43
+ Parameters
44
+ ----------
45
+ idx : int
46
+ Layer number
47
+ shapes : list[list[int]]
48
+ Shape of the outputs from each layer
49
+ batch_norm : bool, Optional
50
+ If batch normalisation should be used, default = False
51
+ layers : int, Optional
52
+ Number of stacked recurrent layers, default = 2
53
+ filters : int, Optional
54
+ Number of output filters, default = 1
55
+ dropout : float, Optional
56
+ Probability of dropout, requires layers > 1, default = 0
57
+ mode : {'gru', 'rnn', 'lstm'}
58
+ Type of recurrent layer
59
+ activation : str | None, Optional
60
+ Which activation function to use, if mode is 'rnn', ReLU activation will always be used,
61
+ default = 'ELU'
62
+ bidirectional : {None, 'sum', 'mean', 'concatenate'}
63
+ If a bidirectional recurrence should be used and method for combining the two
64
+ directions
65
+ **kwargs
66
+ Leftover parameters to pass to base layer for checking
67
+ """
68
+ super().__init__(idx=idx, **kwargs)
69
+ self._bidirectional: str | None = bidirectional
70
+ self._options: list[str | None] = [None, 'sum', 'mean', 'concatenate']
71
+ shape: list[int] = [filters, int(np.prod(shapes[-1][1:]))]
72
+ modes: dict[str, Type[nn.RNN] | Type[nn.LSTM] | Type[nn.GRU]] = {
73
+ 'rnn': nn.RNN,
74
+ 'lstm': nn.LSTM,
75
+ 'gru': nn.GRU,
76
+ }
77
+ recurrent: nn.RNNBase
78
+
79
+ self._check_options('bidirectional', self._bidirectional, set(self._options))
80
+ self._check_options('mode', mode, set(modes))
81
+
82
+ if layers == 1:
83
+ dropout = 0
84
+
85
+ recurrent = modes[mode.lower()](
86
+ shapes[-1][0],
87
+ shape[0],
88
+ num_layers=layers,
89
+ batch_first=True,
90
+ dropout=dropout,
91
+ bidirectional=self._bidirectional is not None,
92
+ **({'nonlinearity': 'relu'} if mode.lower() == 'rnn' else {}),
93
+ )
94
+
95
+ if bidirectional == 'concatenate':
96
+ shape[0] *= 2
97
+
98
+ # Convert 2D data to 1D and add recurrent layer
99
+ self.layers.add_module('Reshape', Reshape([shapes[-1][0], -1]))
100
+ self.layers.add_module('Recurrent', recurrent)
101
+
102
+ # Optional layers
103
+ if activation and mode.lower() != 'rnn':
104
+ self.layers.add_module('Activation', getattr(nn, activation)())
105
+
106
+ if batch_norm:
107
+ self.layers.add_module('BatchNorm', nn.BatchNorm1d(shape[0]))
108
+
109
+ shapes.append(shape)
110
+
111
+ def __getstate__(self) -> dict[str, Any]:
112
+ return super().__getstate__() | {
113
+ 'batch_norm': hasattr(self.layers, 'BatchNorm'),
114
+ 'layers': self.layers.Recurrent.num_layers, # type: ignore[union-attr]
115
+ 'filters': self.layers.Recurrent.hidden_size, # type: ignore[union-attr]
116
+ 'dropout': self.layers.Recurrent.dropout, # type: ignore[union-attr]
117
+ 'mode': self.layers.Recurrent.__class__.__name__.lower(),
118
+ 'activation': self.layers.Activation.__class__.__name__
119
+ if hasattr(self.layers, 'Activation') else None,
120
+ 'bidirectional': self._bidirectional,
121
+ }
122
+
123
+ def forward(self, x: Tensor, *_: Any, **__: Any) -> Tensor:
124
+ r"""
125
+ Forward pass of the recurrent layer
126
+
127
+ Parameters
128
+ ----------
129
+ x : Tensor
130
+ Input tensor with shape `(N,C_{in},L)` and type float, where N is the batch size,
131
+ `C_{in}` is the number of input channels and L is the length of the sequence
132
+
133
+ Returns
134
+ -------
135
+ Tensor
136
+ Output tensor with shape `(N,C_{out},L)` and type float, where `C_{out}` is the number
137
+ of output channels
138
+ """
139
+ x = self.layers[0](x)
140
+ x = self.layers[1](torch.transpose(x, 1, 2))[0]
141
+
142
+ if self._bidirectional in ['sum', 'mean']:
143
+ x = x.view(*x.size()[:2], 2, -1)
144
+
145
+ if self._bidirectional == self._options[1]:
146
+ x = torch.sum(x, dim=-2)
147
+ elif self._bidirectional == self._options[2]:
148
+ x = torch.mean(x, dim=-2)
149
+ return self.layers[2:](torch.transpose(x, 1, 2))
150
+
151
+ def extra_repr(self) -> str:
152
+ """
153
+ Displays layer parameters when printing the network
154
+
155
+ Returns
156
+ -------
157
+ string
158
+ Layer parameters
159
+ """
160
+ return f'bidirectional_method={self._bidirectional}'
161
+
162
+
163
+ __all__ = ['Recurrent']
@@ -0,0 +1,168 @@
1
+ """
2
+ Layer utility functions
3
+ """
4
+ from typing import Any
5
+ from warnings import warn
6
+
7
+ # For compatibility
8
+ from netloader.layers import base
9
+
10
+
11
+ class BaseLayer(base.BaseLayer):
12
+ """
13
+ Deprecated BaseLayer class as BaseLayer was moved to netloader.layers.base
14
+ """
15
+ def __init_subclass__(cls, **kwargs: Any) -> None:
16
+ warn(
17
+ 'BaseLayer is deprecated, use BaseLayer from netloader.layers.base instead',
18
+ DeprecationWarning,
19
+ stacklevel=2,
20
+ )
21
+ super().__init_subclass__(**kwargs)
22
+
23
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
24
+ warn(
25
+ 'BaseLayer is deprecated, use BaseLayer from netloader.layers.base instead',
26
+ DeprecationWarning,
27
+ stacklevel=2,
28
+ )
29
+ super().__init__(*args, **kwargs)
30
+
31
+
32
+ class BaseMultiLayer(base.BaseMultiLayer):
33
+ """
34
+ Deprecated MultiBaseLayer class as MultiBaseLayer was moved to netloader.layers.base
35
+ """
36
+ def __init_subclass__(cls, **kwargs: Any) -> None:
37
+ warn(
38
+ 'BaseMultiLayer is deprecated, use BaseLayer from netloader.layers.base',
39
+ DeprecationWarning,
40
+ stacklevel=2,
41
+ )
42
+ super().__init_subclass__(**kwargs)
43
+
44
+ def __init__(self, *args: Any, **kwargs: Any) -> None:
45
+ warn(
46
+ 'BaseMultiLayer is deprecated, use BaseLayer from netloader.layers.base '
47
+ 'instead',
48
+ DeprecationWarning,
49
+ stacklevel=2,
50
+ )
51
+ super().__init__(*args, **kwargs)
52
+
53
+
54
+ def _int_list_conversion(length: int, elements: list[int | list[int]]) -> list[list[int]]:
55
+ """
56
+ Converts integers to a list of integers, if integer is already a list of integers, then list
57
+ will be preserved.
58
+
59
+ Parameters
60
+ ----------
61
+ length : int
62
+ Number of elements in the converted list of integers
63
+ elements : list[int | list[int]]
64
+ Integers to convert to list of integers, or list of integers to remain unchanged
65
+
66
+ Returns
67
+ -------
68
+ list[list[int]]
69
+ List of integers for each inputted integer or list of integers
70
+ """
71
+ lists: list[list[int]] = []
72
+
73
+ for element in elements:
74
+ if isinstance(element, int):
75
+ lists.append([element] * length)
76
+ else:
77
+ lists.append(element)
78
+
79
+ return lists
80
+
81
+
82
+ def _kernel_shape(
83
+ kernel: int | list[int],
84
+ strides: int | list[int],
85
+ padding: int | list[int],
86
+ shape: list[int]) -> list[int]:
87
+ """
88
+ Calculates the output shape after a kernel operation.
89
+
90
+ Parameters
91
+ ----------
92
+ kernel : int | list[int]
93
+ Size of the kernel
94
+ strides : int | list[int]
95
+ Stride of the kernel
96
+ padding : int | list[int]
97
+ Input padding
98
+ shape : list[int]
99
+ Input shape of the layer
100
+
101
+ Returns
102
+ -------
103
+ list[int]
104
+ Output shape of the layer
105
+ """
106
+ shape = shape.copy()
107
+ strides, kernel, padding = _int_list_conversion(
108
+ len(shape[1:]),
109
+ [strides, kernel, padding],
110
+ )
111
+
112
+ for i, (stride, kernel_length, pad, length) in enumerate(zip(
113
+ strides,
114
+ kernel,
115
+ padding,
116
+ shape[1:]
117
+ )):
118
+ shape[i + 1] = max(1, (length + 2 * pad - kernel_length) // stride + 1)
119
+
120
+ return shape
121
+
122
+
123
+ def _padding(
124
+ kernel: int | list[int],
125
+ strides: int | list[int],
126
+ in_shape: list[int],
127
+ out_shape: list[int]) -> tuple[bool, list[int]]:
128
+ """
129
+ Calculates the padding required for specific output shape.
130
+
131
+ Parameters
132
+ ----------
133
+ kernel : int | list[int]
134
+ Size of the kernel
135
+ strides : int | list[int]
136
+ Stride of the kernel
137
+ in_shape : list[int]
138
+ Input shape of the layer
139
+ out_shape : list[int]
140
+ Output shape from the layer
141
+
142
+ Returns
143
+ -------
144
+ bool
145
+ Whether the padding is asymmetric
146
+ list[int]
147
+ Required padding for specific output shape, if padding is asymmetric, then list will contain
148
+ padding for both sides of the input, otherwise list will contain padding for one side of the
149
+ input
150
+ """
151
+ asymmetric: bool = False
152
+ pad: int
153
+ padding: list[int] = []
154
+ strides, kernel = _int_list_conversion(len(in_shape[1:]), [strides, kernel])
155
+
156
+ for stride, kernel_length, in_length, out_length in zip(
157
+ strides,
158
+ kernel,
159
+ in_shape[1:],
160
+ out_shape[1:],
161
+ ):
162
+ pad = stride * (out_length - 1) + kernel_length - in_length
163
+ asymmetric = asymmetric or bool(pad % 2)
164
+ padding.extend([pad // 2, pad // 2 + int(asymmetric)])
165
+ return asymmetric, padding if asymmetric else padding[::2]
166
+
167
+
168
+ __all__ = ['_int_list_conversion', '_kernel_shape', '_padding']