2024-10-04 04:50:31 +00:00
|
|
|
from typing import Any
|
2023-03-20 15:06:33 +00:00
|
|
|
|
|
|
|
import torch
|
|
|
|
|
2023-03-22 15:50:00 +00:00
|
|
|
from freqtrade.freqai.base_models.BasePyTorchRegressor import BasePyTorchRegressor
|
2023-03-20 15:06:33 +00:00
|
|
|
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
|
2024-05-12 13:18:32 +00:00
|
|
|
from freqtrade.freqai.torch.PyTorchDataConvertor import (
|
|
|
|
DefaultPyTorchDataConvertor,
|
|
|
|
PyTorchDataConvertor,
|
|
|
|
)
|
2023-03-21 14:09:54 +00:00
|
|
|
from freqtrade.freqai.torch.PyTorchMLPModel import PyTorchMLPModel
|
|
|
|
from freqtrade.freqai.torch.PyTorchModelTrainer import PyTorchModelTrainer
|
2023-03-20 15:06:33 +00:00
|
|
|
|
|
|
|
|
2023-03-22 15:50:00 +00:00
|
|
|
class PyTorchMLPRegressor(BasePyTorchRegressor):
|
2023-03-20 15:06:33 +00:00
|
|
|
"""
|
|
|
|
This class implements the fit method of IFreqaiModel.
|
|
|
|
in the fit method we initialize the model and trainer objects.
|
|
|
|
the only requirement from the model is to be aligned to PyTorchRegressor
|
|
|
|
predict method that expects the model to predict tensor of type float.
|
|
|
|
the trainer defines the training loop.
|
|
|
|
|
|
|
|
parameters are passed via `model_training_parameters` under the freqai
|
|
|
|
section in the config file. e.g:
|
|
|
|
{
|
|
|
|
...
|
|
|
|
"freqai": {
|
|
|
|
...
|
|
|
|
"model_training_parameters" : {
|
|
|
|
"learning_rate": 3e-4,
|
|
|
|
"trainer_kwargs": {
|
2023-08-04 13:45:21 +00:00
|
|
|
"n_steps": 5000,
|
2023-03-20 15:06:33 +00:00
|
|
|
"batch_size": 64,
|
2023-07-13 16:41:39 +00:00
|
|
|
"n_epochs": null,
|
2023-03-20 15:06:33 +00:00
|
|
|
},
|
|
|
|
"model_kwargs": {
|
|
|
|
"hidden_dim": 512,
|
|
|
|
"dropout_percent": 0.2,
|
|
|
|
"n_layer": 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
|
2023-04-03 12:19:10 +00:00
|
|
|
@property
|
|
|
|
def data_convertor(self) -> PyTorchDataConvertor:
|
|
|
|
return DefaultPyTorchDataConvertor(target_tensor_type=torch.float)
|
|
|
|
|
2023-03-21 13:19:34 +00:00
|
|
|
def __init__(self, **kwargs) -> None:
|
2023-03-20 15:06:33 +00:00
|
|
|
super().__init__(**kwargs)
|
2023-03-20 17:40:36 +00:00
|
|
|
config = self.freqai_info.get("model_training_parameters", {})
|
2024-05-12 15:12:20 +00:00
|
|
|
self.learning_rate: float = config.get("learning_rate", 3e-4)
|
2024-10-04 04:50:31 +00:00
|
|
|
self.model_kwargs: dict[str, Any] = config.get("model_kwargs", {})
|
|
|
|
self.trainer_kwargs: dict[str, Any] = config.get("trainer_kwargs", {})
|
2023-03-20 15:06:33 +00:00
|
|
|
|
2024-10-04 04:50:31 +00:00
|
|
|
def fit(self, data_dictionary: dict, dk: FreqaiDataKitchen, **kwargs) -> Any:
|
2023-03-20 15:06:33 +00:00
|
|
|
"""
|
|
|
|
User sets up the training and test data to fit their desired model here
|
2023-04-08 10:09:53 +00:00
|
|
|
:param data_dictionary: the dictionary holding all data for train, test,
|
|
|
|
labels, weights
|
|
|
|
:param dk: The datakitchen object for the current coin/model
|
2023-03-20 15:06:33 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
n_features = data_dictionary["train_features"].shape[-1]
|
2024-05-12 15:12:20 +00:00
|
|
|
model = PyTorchMLPModel(input_dim=n_features, output_dim=1, **self.model_kwargs)
|
2023-03-20 15:06:33 +00:00
|
|
|
model.to(self.device)
|
|
|
|
optimizer = torch.optim.AdamW(model.parameters(), lr=self.learning_rate)
|
|
|
|
criterion = torch.nn.MSELoss()
|
2024-04-18 20:51:25 +00:00
|
|
|
# check if continual_learning is activated, and retrieve the model to continue training
|
2023-05-13 11:14:16 +00:00
|
|
|
trainer = self.get_init_model(dk.pair)
|
|
|
|
if trainer is None:
|
|
|
|
trainer = PyTorchModelTrainer(
|
|
|
|
model=model,
|
|
|
|
optimizer=optimizer,
|
|
|
|
criterion=criterion,
|
|
|
|
device=self.device,
|
|
|
|
data_convertor=self.data_convertor,
|
2023-05-14 12:00:03 +00:00
|
|
|
tb_logger=self.tb_logger,
|
2023-05-13 11:14:16 +00:00
|
|
|
**self.trainer_kwargs,
|
|
|
|
)
|
2023-03-28 11:40:23 +00:00
|
|
|
trainer.fit(data_dictionary, self.splits)
|
2023-03-20 15:06:33 +00:00
|
|
|
return trainer
|