model module
Model module.
This module is intended to provide a set of Networks for features extraction and predictions leveraging domain adaptation.
The code for the Transfer Network is taken and readjusted from the DeepDA project.
The code can be found here: https://github.com/jindongwang/transferlearning/tree/master/code/DeepDA
- class model.FeaturesExtractor(input_dim: int, hidden_dim: int, output_dim: int, num_layers: int = 1, device: str = 'cpu')
Bases:
Module
Features extraction Recurrent network based on Bi-LSTM. It requires a dataloader to be organized in sequences.
The structure is the following: 1. Bi-LSTM 2. LSTM 3. Linear 4. LayerNorm 5. PReLU 6. Dropout 7. Linear
__init__.
- Parameters:
input_dim (int) – input data dimension
hidden_dim (int) – layers hidden dimensions
output_dim (int) – number of features to extract (output)
num_layers (int) – number of layers of the BiLSTM
device (str) – device to use, can be either ‘cpu’ or ‘cuda’
- forward(x: Tensor) Tensor
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class model.Regressor(input_dim: int, hidden_dim: int = 32)
Bases:
Module
Predictive linear network performing a regression task.
__init__.
- Parameters:
input_dim (int) – input data dimension
hidden_dim (int) – layers hidden dimensions
- forward(x: Tensor) Tensor
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class model.TransferNet(input_dim: int, fe_hidden_dim: int = 24, fe_output_dim: int = 8, fe_num_layers: int = 1, hidden_dim: int = 32, transfer: bool = True, transfer_loss: str = 'adv', max_iter: int = 200, learning_rate: float = 0.001, weight_decay: float = 0, scheduler_step: int = 100, scheduler_gamma: float = 0.1, device: str = 'cpu', **kwargs)
Bases:
Module
Main network that gathers features extracted from the base network, the predictive network and the transfer loss.
__init__.
- Parameters:
input_dim (int) – input data dimension
fe_hidden_dim (int) – hidden dimension of layers of feature extraction network
fe_output_dim (int) – output dimension of feature extraction network
fe_num_layers (int) – number of layers of feature extraction Bi-LSTM network
hidden_dim (int) – hidden dimension of layers of predictive network
transfer (bool) – true to enable transfer
transfer_loss (str) – transfer loss to use. Available values are: * ‘adv’: adversarial * ‘daan’: DAAN * ‘mmd’: MMD * ‘coral’: CORAL * ‘bnm’: BNM
max_iter (int) – maximum number of iterations of the Lambda Scheduler used in DAAN and Adversarial losses
learning_rate (float) – learning rate
weight_decay (float) – optimizer weight decay
scheduler_step (int) – step size for the step scheduler
scheduler_gamma (float) – gamma for the step scheduler
device (str) – ‘cpu’ or ‘cuda’
kwargs –
- epoch_based_processing(*args, **kwargs)
Handle DAAN loss in a distinct way. Update the DAAN dynamic factor during training time.
- Parameters:
args –
kwargs –
- forward(source: Tensor, target: Tensor, source_label: float64)
Defines the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Module
instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- get_parameters(initial_lr: float = 1.0)
Define specific parameters and adjust the learning rate for transfer losses. Typically, the learning rate for the base network is slowed.
- Parameters:
initial_lr (float) – initial learning rate
- predict(x: Tensor) Tensor
Output a prediction
- Parameters:
x (torch.Tensor) – input tensor