cnn
A module of feature extractors based on convolutional neural networks.
CnnExtractor
Bases: Module
A Convolutional Neural Network (CNN) based network that extracts a feature vector from same-length time windows.
This feature extractor consists of multiple CNN layers and an optional fully connected (FC) layer. Each CNN layer can be configured with a number of filters and a kernel size. Additionally, batch normalization, same-padding and dropout can be applied. The fully connected layer can have a separate dropout probability.
Both CNN and FC layers use ReLU activation functions by default. Custom activation functions can be set for each layer type.
The data flow is as follows: Input --> CNN x n --> [FC] --> Output
The expected input shape is [batch_size, num_features, window_size]
. The output
of this network is always flattened to [batch_size, num_extracted_features]
.
Examples:
Without FC
>>> import torch
>>> from rul_adapt.model import CnnExtractor
>>> cnn = CnnExtractor(14,units=[16, 1],seq_len=30)
>>> cnn(torch.randn(10, 14, 30)).shape
torch.Size([10, 26])
With FC
>>> import torch
>>> from rul_adapt.model import CnnExtractor
>>> cnn = CnnExtractor(14,units=[16, 1],seq_len=30,fc_units=16)
>>> cnn(torch.randn(10, 14, 30)).shape
torch.Size([10, 16])
__init__(input_channels, units, seq_len, kernel_size=3, dilation=1, stride=1, padding=False, fc_units=None, dropout=0.0, fc_dropout=0.0, batch_norm=False, act_func=nn.ReLU, fc_act_func=nn.ReLU)
Create a new CNN-based feature extractor.
The units
are the number of output filters for each CNN layer. The
seq_len
is needed to calculate the input units for the FC layer. The kernel
size of each CNN layer can be set by passing a list to kernel_size
. If an
integer is passed, each layer has the same kernel size. If padding
is true,
same-padding is applied before each CNN layer, which keeps the window_size
the same. If batch_norm
is set, batch normalization is applied for each CNN
layer. If fc_units
is set, a fully connected layer is appended.
Dropout can be applied to each CNN layer by setting conv_dropout
to a
number greater than zero. The same is valid for the fully connected layer and
fc_dropout
. Dropout will never be applied to the input layer.
The whole network uses ReLU activation functions. This can be customized by
setting either conv_act_func
or fc_act_func
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_channels |
int
|
The number of input channels. |
required |
units |
List[int]
|
The list of output filters for the CNN layers. |
required |
seq_len |
int
|
The window_size of the input data. |
required |
kernel_size |
Union[int, List[int]]
|
The kernel size for the CNN layers. Passing an integer uses the same kernel size for each layer. |
3
|
dilation |
int
|
The dilation for the CNN layers. |
1
|
stride |
int
|
The stride for the CNN layers. |
1
|
padding |
bool
|
Whether to apply same-padding before each CNN layer. |
False
|
fc_units |
Optional[int]
|
Number of output units for the fully connected layer. |
None
|
dropout |
float
|
The dropout probability for the CNN layers. |
0.0
|
fc_dropout |
float
|
The dropout probability for the fully connected layer. |
0.0
|
batch_norm |
bool
|
Whether to use batch normalization on the CNN layers. |
False
|
act_func |
Type[Module]
|
The activation function for the CNN layers. |
ReLU
|
fc_act_func |
Type[Module]
|
The activation function for the fully connected layer. |
ReLU
|