enchanter.tasks¶
Classification Task¶
ClassificationRunner¶
- class enchanter.tasks.ClassificationRunner(model: torch.nn.modules.module.Module, optimizer: torch.optim.optimizer.Optimizer, criterion: torch.nn.modules.loss._Loss, experiment, scheduler: Optional[List] = None, callbacks: Optional[List[enchanter.callbacks.base.Callback]] = None)[source]¶
Bases:
enchanter.engine.runner.BaseRunnerRunner for classification tasks.
Examples
>>> from comet_ml import Experiment >>> import torch >>> model = torch.nn.Sequential(...) >>> optimizer = torch.optim.Adam(model.parameters()) >>> criterion = torch.nn.CrossEntropyLoss() >>> runner = ClassificationRunner( >>> model, >>> optimizer, >>> criterion, >>> Experiment() >>> ) >>> runner.fit(...) >>> # or >>> runner.add_loader(...) >>> runner.train_config(epochs=10) >>> runner.run()
Methods
general_step(batch)This method is executed by train_step, val_step, test_step.
general_end(outputs)This method is executed by train_end, val_end, test_end.
- static general_end(outputs: List) Dict[source]¶
This method is executed by train_end, val_end, test_end.
- Parameters
outputs –
Returns:
- general_step(batch: Tuple) Dict[source]¶
This method is executed by train_step, val_step, test_step.
- Parameters
batch –
Returns:
- predict(x: Union[torch.Tensor, numpy.ndarray]) numpy.ndarray[source]¶
A method that makes predictions based on the given input.
- Parameters
x (Union[torch.Tensor, np.ndarray]) –
- Returns
predict
- test_end(outputs: List) Dict[source]¶
This method is executed at the end of each step of neural network testing.
- Parameters
outputs –
- Returns
You need to return a dictionary.
- test_step(batch: Tuple) Dict[source]¶
This method is executed at every 1 step when testing the neural net. See train_step() for help.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary.
- train_end(outputs: List) Dict[source]¶
This method is executed at the end of each step of neural network training.
- Parameters
outputs –
Returns:
- train_step(batch: Tuple) Dict[source]¶
- When training the neural network,
>>> import torch.nn as nn >>> train_loader: DataLoader = ... >>> model: nn.Module = ... >>> criterion = ... >>> for x, y in train_loader: >>> out = model(x) >>> loss = criterion(out, y)
this method is responsible for the above areas.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary with the key ‘loss’.
Examples
>>> def train_step(self, batch): >>> x, y = batch >>> out = self.model(x) >>> loss = nn.functional.cross_entropy(out, y) >>> return {"loss": loss}
Regression Task¶
RegressionnRunner¶
- class enchanter.tasks.RegressionRunner(model: torch.nn.modules.module.Module, optimizer: torch.optim.optimizer.Optimizer, criterion: torch.nn.modules.loss._Loss, experiment, scheduler: Optional[List] = None, callbacks: Optional[List[enchanter.callbacks.base.Callback]] = None)[source]¶
Bases:
enchanter.engine.runner.BaseRunnerRunner for regression problems.
Examples
>>> runner = RegressionRunner(...) >>> runner.add_loader("train", ...) >>> runner.train_config(epochs=1) >>> runner.run() >>> # OR >>> runner = RegressionRunner(...) >>> runner.fit(x, y, epochs=1, batch_size=32)
Methods
general_step(batch)This method is executed by train_step, val_step, test_step.
general_end(outputs)This method is executed by train_end, val_end, test_end.
- static general_end(outputs: List) Dict[source]¶
This method is executed by train_end, val_end, test_end.
- Parameters
outputs –
Returns:
- general_step(batch: Tuple) Dict[source]¶
This method is executed by train_step, val_step, test_step.
- Parameters
batch –
Returns:
- predict(x: Union[torch.Tensor, numpy.ndarray]) numpy.ndarray[source]¶
A method that makes predictions based on the given input.
- Parameters
x (Union[torch.Tensor, np.ndarray]) –
- Returns
predict
- test_end(outputs: List) Dict[source]¶
This method is executed at the end of each step of neural network testing.
- Parameters
outputs –
- Returns
You need to return a dictionary.
- test_step(batch: Tuple) Dict[source]¶
This method is executed at every 1 step when testing the neural net. See train_step() for help.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary.
- train_end(outputs: List) Dict[source]¶
This method is executed at the end of each step of neural network training.
- Parameters
outputs –
Returns:
- train_step(batch: Tuple) Dict[source]¶
- When training the neural network,
>>> import torch.nn as nn >>> train_loader: DataLoader = ... >>> model: nn.Module = ... >>> criterion = ... >>> for x, y in train_loader: >>> out = model(x) >>> loss = criterion(out, y)
this method is responsible for the above areas.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary with the key ‘loss’.
Examples
>>> def train_step(self, batch): >>> x, y = batch >>> out = self.model(x) >>> loss = nn.functional.cross_entropy(out, y) >>> return {"loss": loss}
AutoEncoderRunner¶
- class enchanter.tasks.AutoEncoderRunner(model: torch.nn.modules.module.Module, optimizer: torch.optim.optimizer.Optimizer, criterion: torch.nn.modules.loss._Loss, experiment, scheduler: Optional[List] = None, callbacks: Optional[List[enchanter.callbacks.base.Callback]] = None)[source]¶
Bases:
enchanter.tasks.regression.RegressionRunnerRunner for training AutoEncoder.
Methods
general_step(batch)This method is executed by train_step, val_step, test_step.
general_end(outputs)This method is executed by train_end, val_end, test_end.
Time Series Representation Learning Task¶
TimeSeriesUnsupervisedRunner¶
- class enchanter.tasks.TimeSeriesUnsupervisedRunner(model: torch.nn.modules.module.Module, optimizer: torch.optim.optimizer.Optimizer, experiment, n_negative_samples: int = 1, negative_penalty: int = 1, compared_len: Optional[int] = None, evaluator: Union[sklearn.linear_model._base.LinearRegression, sklearn.tree._classes.BaseDecisionTree, sklearn.svm._base.BaseLibSVM, sklearn.neighbors._base.NeighborsBase] = SVC(), save_memory: bool = False, scheduler: Optional[List] = None, callbacks: Optional[List[enchanter.callbacks.base.Callback]] = None)[source]¶
Bases:
enchanter.engine.runner.BaseRunnerRunner for unsupervised time series representation learning.
Unsupervised representation learning for time series uses the the Unsupervised Triplet Loss proposed in NeurIPS 2019.
Paper: Unsupervised Scalable Representation Learning for Multivariate Time Series
Examples
>>> experiment = ... >>> model: torch.nn.Module = ... >>> optimizer: torch.optim.Optimizer = ... >>> runner = TimeSeriesUnsupervisedRunner(model, optimizer, experiment) >>> runner.add_loader("train", ...) >>> runner.train_config(...) >>> runner.run()
Initializer
- Parameters
model – PyTorch model which outputting a fixed-length vector regardless of the length of the input series.
optimizer – PyTorch Optimizer
experiment –
comet_ml.BaseExperimentorenchanter.callbacks.BaseLoggern_negative_samples – Parameter K in the paper. The number of negative samples to be sampled during training.
negative_penalty – Coefficients that control how much negative values are valued.
compared_len – Maximum length of randomly chosen time series. (default None).
save_memory – If True, enables to save GPU memory.
scheduler – lr scheduler. use
torch.optim.lr_schedulercallbacks – List of callback.
Methods
calculate negative loss per negative sample
calculate_negative_loss(positive_loss, …)calculate negative loss using all negative samples.
encode(data)Output encoded data.
train_step(batch)When training the neural network,
train_end(outputs)This method is executed at the end of each step of neural network training.
- calculate_negative_loss(positive_loss, anchor_representation)[source]¶
calculate negative loss using all negative samples.
- Parameters
positive_loss –
anchor_representation –
- Returns
loss (torch.Tensor)
- calculate_negative_loss_per_negative_sample(begin_neg: torch.Tensor, len_pos_neg: int, batch_size: int, negative_sample_step: int, anchor_representation: torch.Tensor, data: torch.Tensor) torch.Tensor[source]¶
calculate negative loss per negative sample
- Parameters
begin_neg –
len_pos_neg –
batch_size –
negative_sample_step –
anchor_representation –
data –
- Returns
negative loss (torch.Tensor)
- encode(data: Union[numpy.ndarray, torch.Tensor]) Union[numpy.ndarray, torch.Tensor][source]¶
Output encoded data. The output data has the same data type as the input.
- Parameters
data – data
- Returns
encoded data
- initialize() None[source]¶
The method that prepares the Runner. If the variables required for execution, such as self.model, self.optimizer, self.experiment, etc., are not defined, the program will exit with an error message. If there are no problems, pass the model to the CPU or GPU.
- Returns
None
- test_end(outputs: List) Dict[str, torch.Tensor][source]¶
This method is executed at the end of each step of neural network testing.
- Parameters
outputs –
- Returns
You need to return a dictionary.
- test_step(batch: Tuple) Dict[str, torch.Tensor][source]¶
This method is executed at every 1 step when testing the neural net. See train_step() for help.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary.
- train_end(outputs: List) Dict[str, torch.Tensor][source]¶
This method is executed at the end of each step of neural network training.
- Parameters
outputs –
Returns:
- train_step(batch) Dict[str, torch.Tensor][source]¶
- When training the neural network,
>>> import torch.nn as nn >>> train_loader: DataLoader = ... >>> model: nn.Module = ... >>> criterion = ... >>> for x, y in train_loader: >>> out = model(x) >>> loss = criterion(out, y)
this method is responsible for the above areas.
- Parameters
batch – A tuple containing data & labels get from the PyTorch DataLoader.
- Returns
You need to return a dictionary with the key ‘loss’.
Examples
>>> def train_step(self, batch): >>> x, y = batch >>> out = self.model(x) >>> loss = nn.functional.cross_entropy(out, y) >>> return {"loss": loss}
Ensemble¶
BaseEnsembleEstimator¶
- class enchanter.tasks.BaseEnsembleEstimator(runners: List[enchanter.engine.runner.BaseRunner], mode: Optional[str] = None)[source]¶
Bases:
sklearn.base.BaseEstimator
SoftEnsemble¶
- class enchanter.tasks.SoftEnsemble(runners: List[enchanter.engine.runner.BaseRunner], mode: Optional[str] = None)[source]¶
Bases:
enchanter.tasks.ensemble.BaseEnsembleEstimatorEnsemble that averages probabilities
HardEnsemble¶
- class enchanter.tasks.HardEnsemble(runners: List[enchanter.engine.runner.BaseRunner])[source]¶
Bases:
enchanter.tasks.ensemble.BaseEnsembleEstimatorEnsemble that takes a majority vote