consistency
The Consistency DANN approach uses a consistency loss in tandem with a DANN loss. First, the network is pre-trained in a supervised fashion on the source domain. The pre-trained weights are then used to initialize the main training stage. The consistency loss penalizes the weights of the feature extractor moving away from the pre-trained version. This way the feature extractor weights stay close to the pre-trained weights.
# pre-training stage
Source --> PreFeatEx --> Source Feats --> Regressor --> RUL Prediction
# main training stage
------- PreTrainFeatEx --> PreTrain Source Feats --> Consistency Loss
|
|
Source --> FeatEx --> Source Feats -----------> Regressor --> RUL Prediction
^ | |
| | v
Target -- --> Target Feats --> GRL --> DomainDisc --> Domain Prediction
This version of DANN was introduced by Siahpour et al..
ConsistencyApproach
Bases: AdaptionApproach
The Consistency DANN approach introduces a consistency loss that keeps the weights of the feature extractor close to the ones of a pre-trained version. This approach should only be used with a pre-trained feature extractor. Otherwise, the consistency loss would serve no purpose.
The regressor and domain discriminator need the same number of input units as the feature extractor has output units. The discriminator is not allowed to have an activation function on its last layer for it to work with the DANN loss.
Examples:
>>> from rul_adapt import model
>>> from rul_adapt import approach
>>> feat_ex = model.CnnExtractor(1, [16, 16, 1], 10, fc_units=16)
>>> reg = model.FullyConnectedHead(16, [1])
>>> disc = model.FullyConnectedHead(16, [8, 1], act_func_on_last_layer=False)
>>> pre = approach.SupervisedApproach("rmse")
>>> pre.set_model(feat_ex, reg, disc)
>>> main = approach.ConsistencyApproach(1.0, 100)
>>> main.set_model(pre.feature_extractor, pre.regressor, disc)
dann_factor
property
Return the influency of the DANN loss based on the current epoch.
It is calculated as: 2 / (1 + math.exp(-10 * current_epoch / max_epochs)) - 1
domain_disc
property
The domain discriminator network.
__init__(consistency_factor, max_epochs, loss_type='rmse', rul_score_mode='phm08', evaluate_degraded_only=False, **optim_kwargs)
Create a new consistency DANN approach.
The consistency factor is the strength of the consistency loss' influence.
The influence of the DANN loss is increased during the training process. It
starts at zero and reaches one at max_epochs
.
The domain discriminator is set by the set_model
function together with the
feature extractor and regressor. For more information, see the approach module page.
For more information about the possible optimizer keyword arguments, see here.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
consistency_factor |
float
|
The strength of the consistency loss' influence. |
required |
max_epochs |
int
|
The number of epochs after which the DANN loss' influence is maximal. |
required |
loss_type |
Literal['mse', 'mae', 'rmse']
|
The type of regression loss, either 'mse', 'rmse' or 'mae'. |
'rmse'
|
rul_score_mode |
Literal['phm08', 'phm12']
|
The mode for the val and test RUL score, either 'phm08' or 'phm12'. |
'phm08'
|
evaluate_degraded_only |
bool
|
Whether to only evaluate the RUL score on degraded samples. |
False
|
**optim_kwargs |
Any
|
Keyword arguments for the optimizer, e.g. learning rate. |
{}
|
configure_optimizers()
Configure an optimizer to train the feature extractor, regressor and domain discriminator.
forward(inputs)
Predict the RUL values for a batch of input features.
set_model(feature_extractor, regressor, domain_disc=None, *args, **kwargs)
Set the feature extractor, regressor and domain discriminator for this approach. The discriminator is not allowed to have an activation function on its last layer and needs to use only a single output neuron. It is wrapped by a DomainAdversarialLoss.
A frozen copy of the feature extractor is produced to be used for the consistency loss. The feature extractor should, therefore, be pre-trained.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_extractor |
Module
|
The pre-trained feature extraction network. |
required |
regressor |
Module
|
The optionally pre-trained RUL regression network. |
required |
domain_disc |
Optional[Module]
|
The domain discriminator network. |
None
|
test_step(batch, batch_idx, dataloader_idx)
Execute one test step.
The batch
argument is a list of two tensors representing features and
labels. A RUL prediction is made from the features and the validation RMSE
and RUL score are calculated. The metrics recorded for dataloader idx zero
are assumed to be from the source domain and for dataloader idx one from the
target domain. The metrics are written to the configured logger under the
prefix test
.
Args:
batch: A list containing a feature and a label tensor.
batch_idx: The index of the current batch.
dataloader_idx: The index of the current dataloader (0: source, 1: target).
training_step(batch, batch_idx)
Execute one training step.
The batch
argument is a list of three tensors representing the source
features, source labels and target features. Both types of features are fed
to the feature extractor. Then the regression loss for the source domain and
the DANN loss between domains is computed. Afterwards the consistency loss is
calculated. The regression, DANN, consistency and combined loss are logged.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
batch |
List[Tensor]
|
A list of a source feature, source label and target feature tensors. |
required |
batch_idx |
int
|
The index of the current batch. |
required |
Returns: The combined loss.
validation_step(batch, batch_idx, dataloader_idx)
Execute one validation step.
The batch
argument is a list of two tensors representing features and
labels. A RUL prediction is made from the features and the validation RMSE
and RUL score are calculated. The metrics recorded for dataloader idx zero
are assumed to be from the source domain and for dataloader idx one from the
target domain. The metrics are written to the configured logger under the
prefix val
.
Args:
batch: A list containing a feature and a label tensor.
batch_idx: The index of the current batch.
dataloader_idx: The index of the current dataloader (0: source, 1: target).
StdExtractor
This extractor can be used to extract the per-feature standard deviation from windows of data. It can be used to pre-process datasets like FEMTO and XJTU-SY with the help of the RulDataModule.
Examples:
Extract the std of the horizontal acceleration and produce windows of size 30.
>>> import rul_datasets
>>> import rul_adapt
>>> fd1 = rul_datasets.XjtuSyReader(fd=1)
>>> extractor = rul_adapt.approach.consistency.StdExtractor([0])
>>> dm = rul_datasets.RulDataModule(fd1, 32, extractor, window_size=30)
__call__(inputs, targets)
Extract features from the input data.
The input is expected to have a shape of [num_windows, window_size,
num_features]
. The output will have a shape of [num_windows,
len(self.channels)]
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
inputs |
ndarray
|
The input data. |
required |
Returns: The features extracted from the input data.
__init__(channels)
Create a new feature extractor for standard deviations.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
channels |
List[int]
|
The list of channel indices to extract features from. |
required |