Custom Optimizations

flood_forecast.custom.custom_opt.warmup_cosine(x, warmup=0.002)[source]
flood_forecast.custom.custom_opt.warmup_constant(x, warmup=0.002)[source]

Linearly increases learning rate over warmup`*`t_total (as provided to BertAdam) training steps. Learning rate is 1. afterwards.

flood_forecast.custom.custom_opt.warmup_linear(x, warmup=0.002)[source]

Specifies a triangular learning rate schedule where peak is reached at warmup`*`t_total-th (as provided to BertAdam) training step. After t_total-th training step, learning rate is zero.

class flood_forecast.custom.custom_opt.MASELoss(baseline_method)[source]
__init__(baseline_method)[source]

This implements the MASE loss function (e.g. MAE_MODEL/MAE_NAIEVE)

forward(target: torch.Tensor, output: torch.Tensor, train_data: torch.Tensor, m=1)torch.Tensor[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.RMSELoss(variance_penalty=0.0)[source]

Returns RMSE using: target -> True y output -> Prediction by model source: https://discuss.pytorch.org/t/rmse-loss-function/16540/3

__init__(variance_penalty=0.0)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(output: torch.Tensor, target: torch.Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.MAPELoss(variance_penalty=0.0)[source]

Returns MAPE using: target -> True y output -> Predtion by model

__init__(variance_penalty=0.0)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(output: torch.Tensor, target: torch.Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.PenalizedMSELoss(variance_penalty=0.0)[source]

Returns MSE using: target -> True y output -> Predtion by model source: https://discuss.pytorch.org/t/rmse-loss-function/16540/3

__init__(variance_penalty=0.0)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(output: torch.Tensor, target: torch.Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.GaussianLoss(mu=0, sigma=0)[source]
__init__(mu=0, sigma=0)[source]

Compute the negative log likelihood of Gaussian Distribution From https://arxiv.org/abs/1907.00235

forward(x: torch.Tensor)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.QuantileLoss(quantiles)[source]

From https://medium.com/the-artificial-impostor/quantile-regression-part-2-6fdbc26b2629

__init__(quantiles)[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(preds, target)[source]

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

training: bool
class flood_forecast.custom.custom_opt.BertAdam(params, lr=<required parameter>, warmup=-1, t_total=-1, schedule='warmup_linear', b1=0.9, b2=0.999, e=1e-06, weight_decay=0.01, max_grad_norm=1.0)[source]

Implements BERT version of Adam algorithm with weight decay fix. Params:

lr: learning rate warmup: portion of t_total for the warmup, -1 means no warmup. Default: -1 t_total: total number of training steps for the learning

rate schedule, -1 means constant learning rate. Default: -1

schedule: schedule to use for the warmup (see above). Default: ‘warmup_linear’ b1: Adams b1. Default: 0.9 b2: Adams b2. Default: 0.999 e: Adams epsilon. Default: 1e-6 weight_decay: Weight decay. Default: 0.01 max_grad_norm: Maximum norm for the gradients (-1 means no clipping). Default: 1.0

__init__(params, lr=<required parameter>, warmup=-1, t_total=-1, schedule='warmup_linear', b1=0.9, b2=0.999, e=1e-06, weight_decay=0.01, max_grad_norm=1.0)[source]

Initialize self. See help(type(self)) for accurate signature.

get_lr()List[source]
step(closure=None)[source]

Performs a single optimization step. Arguments:

closure (callable, optional): A closure that reevaluates the model

and returns the loss.

class flood_forecast.custom.custom_opt.NegativeLogLikelihood[source]

target -> True y output -> predicted distribution

__init__()[source]

Initializes internal Module state, shared by both nn.Module and ScriptModule.

forward(output: <module 'torch.distributions' from '/home/docs/checkouts/readthedocs.org/user_builds/flow-forecast/envs/stable/lib/python3.7/site-packages/torch/distributions/__init__.py'>, target: torch.Tensor)[source]

calculates NegativeLogLikelihood

training: bool
flood_forecast.custom.custom_opt.l1_regularizer(model, lambda_l1=0.01)[source]

source: https://stackoverflow.com/questions/58172188/how-to-add-l1-regularization-to-pytorch-nn-model

flood_forecast.custom.custom_opt.orth_regularizer(model, lambda_orth=0.01)[source]

source: https://stackoverflow.com/questions/58172188/how-to-add-l1-regularization-to-pytorch-nn-model

sss