stable/freqtrade/freqai/base_models/BasePyTorchModel.py

84 lines
2.9 KiB
Python
Raw Normal View History

2023-03-05 14:59:24 +00:00
import logging
2023-04-03 12:19:10 +00:00
from abc import ABC, abstractmethod
2023-03-05 14:59:24 +00:00
from time import time
from typing import Any
2023-03-05 14:59:24 +00:00
import torch
from pandas import DataFrame
from freqtrade.freqai.data_kitchen import FreqaiDataKitchen
from freqtrade.freqai.freqai_interface import IFreqaiModel
2023-04-03 13:03:15 +00:00
from freqtrade.freqai.torch.PyTorchDataConvertor import PyTorchDataConvertor
2023-03-05 14:59:24 +00:00
2023-03-08 14:03:36 +00:00
2023-03-05 14:59:24 +00:00
logger = logging.getLogger(__name__)
2023-04-03 12:19:10 +00:00
class BasePyTorchModel(IFreqaiModel, ABC):
2023-03-05 14:59:24 +00:00
"""
2023-03-09 09:14:54 +00:00
Base class for PyTorch type models.
2023-04-03 12:19:10 +00:00
User *must* inherit from this class and set fit() and predict() and
data_convertor property.
2023-03-05 14:59:24 +00:00
"""
def __init__(self, **kwargs):
2023-03-09 11:29:11 +00:00
super().__init__(config=kwargs["config"])
self.dd.model_type = "pytorch"
self.device = "cuda" if torch.cuda.is_available() else "cpu"
2023-03-28 11:40:23 +00:00
test_size = self.freqai_info.get('data_split_parameters', {}).get('test_size')
self.splits = ["train", "test"] if test_size != 0 else ["train"]
2023-03-05 14:59:24 +00:00
def train(
self, unfiltered_df: DataFrame, pair: str, dk: FreqaiDataKitchen, **kwargs
) -> Any:
"""
Filter the training data and train a model to it. Train makes heavy use of the datakitchen
for storing, saving, loading, and analyzing the data.
:param unfiltered_df: Full dataframe for the current training period
:return:
:model: Trained model which can be used to inference (self.predict)
"""
logger.info(f"-------------------- Starting training {pair} --------------------")
start_time = time()
features_filtered, labels_filtered = dk.filter_features(
unfiltered_df,
dk.training_features_list,
dk.label_list,
training_filter=True,
)
# split data into train/test data.
data_dictionary = dk.make_train_test_datasets(features_filtered, labels_filtered)
if not self.freqai_info.get("fit_live_predictions", 0) or not self.live:
dk.fit_labels()
# normalize all data based on train_dataset only
data_dictionary = dk.normalize_data(data_dictionary)
# optional additional data cleaning/analysis
self.data_cleaning_train(dk)
logger.info(
f"Training model on {len(dk.data_dictionary['train_features'].columns)} features"
)
logger.info(f"Training model on {len(data_dictionary['train_features'])} data points")
model = self.fit(data_dictionary, dk)
end_time = time()
logger.info(f"-------------------- Done training {pair} "
f"({end_time - start_time:.2f} secs) --------------------")
return model
2023-04-03 12:19:10 +00:00
@property
@abstractmethod
def data_convertor(self) -> PyTorchDataConvertor:
2023-04-03 14:06:39 +00:00
"""
a class responsible for converting `*_features` & `*_labels` pandas dataframes
to pytorch tensors.
"""
2023-04-03 12:19:10 +00:00
raise NotImplementedError("Abstract property")