Skip to content

Commit

Permalink
fix: add params passing and steps fixation in every nn model
Browse files Browse the repository at this point in the history
  • Loading branch information
Lopa10ko committed Jul 11, 2024
1 parent 0b38f6d commit 1521fde
Show file tree
Hide file tree
Showing 9 changed files with 86 additions and 111 deletions.
42 changes: 18 additions & 24 deletions fedot_ind/core/models/nn/network_impl/deepar.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,40 +213,34 @@ class DeepAR(BaseNeuralModel):
Variational Inference + Probable Anomaly detection"""

def __init__(self, params: Optional[OperationParameters] = None):
super().__init__()
if not params:
params = {}
super().__init__(params)
# training settings
self.epochs = params.get('epochs', 50)
self.learning_rate = params.get('learning_rate', 0.1)
self.batch_size = params.get('batch_size', 16)
self.epochs = self.params.get('epochs', 50)
self.learning_rate = self.params.get('learning_rate', 0.1)
self.batch_size = self.params.get('batch_size', 16)

# architecture settings
self.activation = params.get('activation', 'tanh')
self.cell_type = params.get('cell_type', 'LSTM')
self.hidden_size = params.get('hidden_size', 10)
self.rnn_layers = params.get('rnn_layers', 2)
self.dropout = params.get('dropout', 0.1)
self.expected_distribution = params.get(
'expected_distribution', 'normal')
self.patch_len = params.get('patch_len', None)
self.activation = self.params.get('activation', 'tanh')
self.cell_type = self.params.get('cell_type', 'LSTM')
self.hidden_size = self.params.get('hidden_size', 10)
self.rnn_layers = self.params.get('rnn_layers', 2)
self.dropout = self.params.get('dropout', 0.1)
self.expected_distribution = self.params.get('expected_distribution', 'normal')
self.patch_len = self.params.get('patch_len', None)
self.preprocess_to_lagged = False
self.horizon = 1 # params.get('horizon', 1) for future extension
self.task_type = 'ts_forecsting'

# forecasting settings
self.forecast_mode = params.get('forecast_mode', 'predictions')
self.quantiles = torch.tensor(
params.get('quantiles', [0.25, 0.5, 0.75]))
self.n_samples = params.get('n_samples', 10)
self.forecast_mode = self.params.get('forecast_mode', 'predictions')
self.quantiles = torch.tensor(self.params.get('quantiles', [0.25, 0.5, 0.75]))
self.n_samples = self.params.get('n_samples', 10)
self.test_patch_len = None
self.forecast_length = params.get('forecast_length', 1)
self.forecast_length = self.params.get('forecast_length', 1)

# additional
self.print_training_progress = params.get(
'print_training_progress', False)
self._prediction_averaging_factor = params.get(
'prediction_averaging_factor', 17)
self.print_training_progress = self.params.get('print_training_progress', False)
self._prediction_averaging_factor = self.params.get('prediction_averaging_factor', 17)

def _init_model(self, ts) -> tuple:
self.loss_fn = DeepARModule._loss_fns[self.expected_distribution]()
Expand Down Expand Up @@ -408,7 +402,7 @@ def _train_loop(self, model,
val_loader,
optimizer,
val_interval=10):
train_steps = len(train_loader)
train_steps = max(1, len(train_loader))
early_stopping = EarlyStopping()

scheduler = lr_scheduler.OneCycleLR(optimizer=optimizer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,11 @@ class XCModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.num_classes = params.get('num_classes', 1)
self.epochs = params.get('epochs', 100)
self.batch_size = params.get('batch_size', 32)
self.num_classes = self.params.get('num_classes', 1)
self.epochs = self.params.get('epochs', 100)
self.batch_size = self.params.get('batch_size', 32)
self.is_regression_task = False

def _init_model(self, ts):
Expand All @@ -281,8 +281,5 @@ def _init_model(self, ts):
seq_len=ts.features.shape[2]).to(default_device())
self.model_for_inference = self.model
optimizer = optim.Adam(self.model.parameters(), lr=0.001)
if ts.num_classes == 2:
loss_fn = nn.CrossEntropyLoss()
else:
loss_fn = nn.BCEWithLogitsLoss()
loss_fn = self._get_loss_metric(ts)
return loss_fn, optimizer
4 changes: 2 additions & 2 deletions fedot_ind/core/models/nn/network_impl/inception.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ class InceptionTimeModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.num_classes = params.get('num_classes', 1)
self.num_classes = self.params.get('num_classes', 1)

def __repr__(self):
return "InceptionNN"
Expand Down
36 changes: 14 additions & 22 deletions fedot_ind/core/models/nn/network_impl/lora_nn.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@
}


def linear_layer_parameterization_with_info(
updated_weight, device, rank=1, lora_alpha=1):
def linear_layer_parameterization_with_info(updated_weight, device, rank=1, lora_alpha=1):
# Only add the parameterization to the weight matrix, ignore the Bias

# From section 4.2 of the paper:
Expand All @@ -46,8 +45,7 @@ def linear_layer_parameterization_with_info(
# We leave the empirical investigation of [...], and biases to a future
# work.

return LoRAParametrizationWithInfo(
updated_weight, rank=rank, alpha=lora_alpha)
return LoRAParametrizationWithInfo(updated_weight, rank=rank, alpha=lora_alpha)


class LoRAParametrizationWithInfo(nn.Module):
Expand Down Expand Up @@ -81,26 +79,26 @@ class LoraModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)

# hyperparams for LoRa learning
self.epochs = params.get('epochs', 1)
self.batch_size = params.get('epochs', 16)
self.model_type = params.get('neural_architecture', 'dummy')
self.pretrain = params.get('from_pretrain', False)
self.lora_init = params.get('lora_init', 'random')
self.epochs = self.params.get('epochs', 1)
self.batch_size = self.params.get('epochs', 16)
self.model_type = self.params.get('neural_architecture', 'dummy')
self.pretrain = self.params.get('from_pretrain', False)
self.lora_init = self.params.get('lora_init', 'random')
# hyperparams for SVD
self.sampling_share = params.get('sampling_share', 0.3)
self.rank = params.get('lora_rank', 1)
self.power_iter = params.get('power_iter', 3)
self.use_approx = params.get('use_rsvd', True)
self.sampling_share = self.params.get('sampling_share', 0.3)
self.rank = self.params.get('lora_rank', 1)
self.power_iter = self.params.get('power_iter', 3)
self.use_approx = self.params.get('use_rsvd', True)

svd_params = dict(rank=self.rank,
sampling_share=self.sampling_share,
power_iter=self.power_iter)
self.industrial_impl = NEURAL_MODEL[self.model_type]
self.rsvd = RSVDDecomposition(svd_params)
self.rsvd = RSVDDecomposition(OperationParameters(**svd_params))

def __repr__(self):
return f'LoRa - {self.model_type}'
Expand All @@ -114,13 +112,7 @@ def _init_model(self, input_data):
output_dim=self.num_classes).to(
default_device())
optimizer = optim.Adam(self.model.parameters(), lr=self.learning_rate)
if input_data.task.task_type.value == 'classification':
if input_data.num_classes == 2:
loss_fn = CROSS_ENTROPY()
else:
loss_fn = MULTI_CLASS_CROSS_ENTROPY()
else:
loss_fn = RMSE()
loss_fn = self._get_loss_metric(input_data)
return loss_fn, optimizer

def __init_lora(self, spectrum_by_layer):
Expand Down
37 changes: 18 additions & 19 deletions fedot_ind/core/models/nn/network_impl/nbeats.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,28 @@ class NBeatsModel(BaseNeuralModel):
Original code: https://github.com/ServiceNow/N-BEATS
"""

def __init__(self, params: Optional[OperationParameters] = {}):
self.is_generic_architecture = params.get(
"is_generic_architecture", True)
self.epochs = params.get("epochs", 10)
self.batch_size = params.get("batch_size", 16)
self.loss = params.get("loss", 'mse')
self.optimizer = params.get("optimizer", 'adam')
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.is_generic_architecture = self.params.get("is_generic_architecture", True)
self.epochs = self.params.get("epochs", 10)
self.batch_size = self.params.get("batch_size", 16)
self.loss = self.params.get("loss", 'mse')
self.optimizer = self.params.get("optimizer", 'adam')
self.activation = 'None'

self.n_stacks = params.get("n_stacks", 30)
self.layers = params.get("layers", 4)
self.layer_size = params.get("layer_size", 512)
self.n_stacks = self.params.get("n_stacks", 30)
self.layers = self.params.get("layers", 4)
self.layer_size = self.params.get("layer_size", 512)

self.n_trend_blocks = params.get("n_trend_blocks", 3)
self.n_trend_layers = params.get("n_trend_layers", 4)
self.trend_layer_size = params.get("trend_layer_size", 2)
self.degree_of_polynomial = params.get("degree_of_polynomial", 20)
self.n_trend_blocks = self.params.get("n_trend_blocks", 3)
self.n_trend_layers = self.params.get("n_trend_layers", 4)
self.trend_layer_size = self.params.get("trend_layer_size", 2)
self.degree_of_polynomial = self.params.get("degree_of_polynomial", 20)

self.n_seasonality_blocks = params.get("n_seasonality_blocks", 3)
self.n_seasonality_layers = params.get("n_seasonality_layers", 4)
self.seasonality_layer_size = params.get(
"seasonality_layer_size", 2048)
self.n_of_harmonics = params.get("n_of_harmonics", 1)
self.n_seasonality_blocks = self.params.get("n_seasonality_blocks", 3)
self.n_seasonality_layers = self.params.get("n_seasonality_layers", 4)
self.seasonality_layer_size = self.params.get("seasonality_layer_size", 2048)
self.n_of_harmonics = self.params.get("n_of_harmonics", 1)

def _init_model(self, ts):
self.forecast_length = ts.task.task_params.forecast_length
Expand Down
29 changes: 14 additions & 15 deletions fedot_ind/core/models/nn/network_impl/patch_tst.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,19 @@ class PatchTSTModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
self.epochs = params.get('epochs', 10)
self.batch_size = params.get('batch_size', 16)
self.activation = params.get('activation', 'GELU')
self.learning_rate = params.get('learning_rate', 0.001)
self.use_amp = params.get('use_amp', False)
self.horizon = params.get('forecast_length', None)
self.patch_len = params.get('patch_len', None)
self.output_attention = params.get('output_attention', False)
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.epochs = self.params.get('epochs', 10)
self.batch_size = self.params.get('batch_size', 16)
self.activation = self.params.get('activation', 'GELU')
self.learning_rate = self.params.get('learning_rate', 0.001)
self.use_amp = self.params.get('use_amp', False)
self.horizon = self.params.get('forecast_length', None)
self.patch_len = self.params.get('patch_len', None)
self.output_attention = self.params.get('output_attention', False)
self.test_patch_len = self.patch_len
self.preprocess_to_lagged = False
self.forecast_mode = params.get('forecast_mode', 'out_of_sample')
self.forecast_mode = self.params.get('forecast_mode', 'out_of_sample')
self.model_list = []

def _init_model(self, ts):
Expand All @@ -160,8 +161,7 @@ def _init_model(self, ts):
activation=self.activation).to(default_device())
optimizer = optim.Adam(model.parameters(), lr=self.learning_rate)
patch_pred_len = round(self.horizon / 4)
loss_fn = EXPONENTIAL_WEIGHTED_LOSS(
time_steps=patch_pred_len, tolerance=0.3)
loss_fn = EXPONENTIAL_WEIGHTED_LOSS(time_steps=patch_pred_len, tolerance=0.3)
return model, loss_fn, optimizer

def _fit_model(self, input_data: InputData, split_data: bool = True):
Expand Down Expand Up @@ -265,13 +265,12 @@ def _train_loop(self, model,
train_loader,
loss_fn,
optimizer):
train_steps = len(train_loader)
train_steps = max(1, len(train_loader))
early_stopping = EarlyStopping()
scheduler = lr_scheduler.OneCycleLR(optimizer=optimizer,
steps_per_epoch=train_steps,
epochs=self.epochs,
max_lr=self.learning_rate)
args = {'lradj': 'type2'}

for epoch in range(self.epochs):
iter_count = 0
Expand Down Expand Up @@ -299,7 +298,7 @@ def _train_loop(self, model,
adjust_learning_rate(optimizer=optimizer,
scheduler=scheduler,
epoch=epoch + 1,
lradj=args['lradj'],
lradj='type2',
printout=False,
learning_rate=self.learning_rate)
scheduler.step()
Expand Down
8 changes: 4 additions & 4 deletions fedot_ind/core/models/nn/network_impl/resnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,11 +116,11 @@ class ResNetModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.epochs = params.get('epochs', 25)
self.batch_size = params.get('batch_size', 64)
self.model_name = params.get('model_name', 'ResNet18')
self.epochs = self.params.get('epochs', 25)
self.batch_size = self.params.get('batch_size', 64)
self.model_name = self.params.get('model_name', 'ResNet18')

def __repr__(self):
return self.model_name
Expand Down
14 changes: 6 additions & 8 deletions fedot_ind/core/models/nn/network_impl/tst.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,20 +275,18 @@ class TSTModel(BaseNeuralModel):
"""

def __init__(self, params: Optional[OperationParameters] = {}):
self.num_classes = params.get('num_classes', 1)
self.epochs = params.get('epochs', 100)
self.batch_size = params.get('batch_size', 32)
def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.num_classes = self.params.get('num_classes', 1)
self.epochs = self.params.get('epochs', 100)
self.batch_size = self.params.get('batch_size', 32)

def _init_model(self, ts):
self.model = TST(input_dim=ts.features.shape[1],
output_dim=self.num_classes,
seq_len=ts.features.shape[2]).to(default_device())
optimizer = optim.Adam(self.model.parameters(), lr=0.001)
if ts.num_classes == 2:
loss_fn = nn.CrossEntropyLoss()
else:
loss_fn = nn.BCEWithLogitsLoss()
loss_fn = self._get_loss_metric(ts)
return loss_fn, optimizer


Expand Down
14 changes: 5 additions & 9 deletions fedot_ind/core/operation/transformation/basis/eigen_basis.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,9 @@
from fedot_ind.core.operation.transformation.regularization.spectrum import reconstruct_basis, \
singular_value_hard_threshold

class_type = TypeVar("T", bound="DataDrivenBasis")


class EigenBasisImplementation(BasisDecompositionImplementation):
"""Eigen basis decomposition implementation
"""Eigen Basis decomposition implementation
Example:
ts1 = np.random.rand(200)
ts2 = np.random.rand(200)
Expand All @@ -33,12 +31,10 @@ class EigenBasisImplementation(BasisDecompositionImplementation):

def __init__(self, params: Optional[OperationParameters] = None):
super().__init__(params)
self.window_size = params.get('window_size', 20)
self.low_rank_approximation = params.get(
'low_rank_approximation', True)
self.tensor_approximation = params.get('tensor_approximation', False)
self.rank_regularization = params.get(
'rank_regularization', 'hard_thresholding')
self.window_size = self.params.get('window_size', 20)
self.low_rank_approximation = self.params.get('low_rank_approximation', True)
self.tensor_approximation = self.params.get('tensor_approximation', False)
self.rank_regularization = self.params.get('rank_regularization', 'hard_thresholding')
self.logging_params.update({'WS': self.window_size})
self.explained_dispersion = None
self.SV_threshold = None
Expand Down

0 comments on commit 1521fde

Please sign in to comment.