Skip to content

Commit

Permalink
add transfomers for composing
Browse files Browse the repository at this point in the history
  • Loading branch information
v1docq committed Dec 1, 2023
1 parent 4126c93 commit fede2b8
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 161 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import matplotlib.pyplot as plt
import numpy as np
from fedot import Fedot
from fedot.core.pipelines.pipeline_builder import PipelineBuilder
from fedot.core.repository.tasks import TsForecastingParams, Task, TaskTypesEnum

from examples.example_utils import get_ts_data
from examples.pipeline_example.time_series.ts_forecasting.ssa_forecasting import plot_metrics_and_prediction
Expand All @@ -21,36 +24,50 @@
window_length = max(window_length_hac, window_length_heuristic)

model_dict = {
# 'tst_model':
# PipelineBuilder().add_node('patch_tst_model', params={'patch_len': None,
# 'forecast_length': forecast_length,
# 'epochs': 200}),
'tst_model':
PipelineBuilder().add_node('patch_tst_model', params={'patch_len': None,
'forecast_length': forecast_length,
'epochs': 10}),
'spectral_tst_model':
PipelineBuilder().add_node('ar').add_node('eigen_basis',
params={'window_size': window_length}, branch_idx=1).
add_node('feature_filter_model', params={'grouping_level': 0.5}, branch_idx=1).
add_node('patch_tst_model', params={'patch_len': None,
'forecast_length': forecast_length,
'epochs': 100}, branch_idx=1).join_branches('cat_features', params={
'average_type': 'average',
'prediction_length': forecast_length}),
'epochs': 1}, branch_idx=1),

'baseline': PipelineBuilder().add_node('ar')
}
baseline = model_dict['baseline'].build()
del model_dict['baseline']
baseline.fit(train_data)
baseline_prediction = np.ravel(baseline.predict(test_data).predict)

with IndustrialModels():
for model in model_dict.keys():
pipeline = model_dict[model].build()
pipeline.fit(train_data)
model_prediction = pipeline.predict(test_data).predict
plot_metrics_and_prediction(test_data,
train_data,
model_prediction,
baseline_prediction,
model,
dataset_name)
_ = 1
IndustrialModels().setup_repository()
for model in model_dict.keys():
# pipeline.fit(train_data)
model = Fedot(problem='ts_forecasting',
logging_level=10,
preset='ts',
available_operations=[
'ssa_forecaster',
'patch_tst_model',
'ar',
'ridge',
'ts_naive_average',
'stl_arima',
'lagged', 'arima', 'lasso'
],
task_params=TsForecastingParams(forecast_length=forecast_length),
timeout=30
)
model.fit(train_data)
model.current_pipeline.show()
model_prediction = model.predict(test_data)
#
# model_prediction = pipeline.predict(test_data).predict
plot_metrics_and_prediction(test_data,
train_data,
model_prediction,
baseline_prediction,
model,
dataset_name)
2 changes: 2 additions & 0 deletions fedot_ind/core/architecture/settings/constanst_repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ class TorchLossesConstant(Enum):
CROSS_ENTROPY = nn.CrossEntropyLoss
MULTI_CLASS_CROSS_ENTROPY = nn.BCEWithLogitsLoss
MSE = nn.MSELoss
RMSE = RMSE
SMAPE = SMAPELoss
TWEEDIE_LOSS = TweedieLoss
FOCAL_LOSS = FocalLoss
Expand Down Expand Up @@ -177,6 +178,7 @@ class TorchLossesConstant(Enum):
CROSS_ENTROPY = TorchLossesConstant.CROSS_ENTROPY.value
MULTI_CLASS_CROSS_ENTROPY = TorchLossesConstant.MULTI_CLASS_CROSS_ENTROPY.value
MSE = TorchLossesConstant.MSE.value
RMSE = TorchLossesConstant.RMSE.value
SMAPE = TorchLossesConstant.SMAPE.value
TWEEDIE_LOSS = TorchLossesConstant.TWEEDIE_LOSS.value
FOCAL_LOSS = TorchLossesConstant.FOCAL_LOSS.value
Expand Down
20 changes: 18 additions & 2 deletions fedot_ind/core/models/nn/fedot_evaluation_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from fedot.core.data.data import InputData, OutputData
from fedot.core.operations.evaluation.evaluation_interfaces import EvaluationStrategy
from fedot.core.operations.evaluation.time_series import FedotTsForecastingStrategy
from fedot.core.operations.operation_parameters import OperationParameters

from fedot_ind.core.models.nn.network_impl.explainable_convolution_model import XCModel
Expand All @@ -18,8 +19,7 @@ class FedotNNClassificationStrategy(EvaluationStrategy):
'omniscale_model': OmniScaleModel,
'tst_model': TSTModel,
'resnet_model': ResNetModel,
'xcm_model': XCModel,
'patch_tst_model': PatchTSTModel
'xcm_model': XCModel
}

def _convert_to_operation(self, operation_type: str):
Expand All @@ -45,3 +45,19 @@ def predict_for_fit(self, trained_operation, predict_data: InputData) -> OutputD
return trained_operation.predict_for_fit(predict_data, self.output_mode)
else:
return trained_operation.predict(predict_data, self.output_mode)


class FedotNNTimeSeriesStrategy(FedotTsForecastingStrategy):
__operations_by_types = {
'patch_tst_model': PatchTSTModel
}

def _convert_to_operation(self, operation_type: str):
if operation_type in self.__operations_by_types.keys():
return self.__operations_by_types[operation_type]
else:
raise ValueError(f'Impossible to obtain custom preprocessing strategy for {operation_type}')

def __init__(self, operation_type: str, params: Optional[OperationParameters] = None):
self.operation_impl = self._convert_to_operation(operation_type)
super().__init__(operation_type, params)
Loading

0 comments on commit fede2b8

Please sign in to comment.