This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LSTMModel(nn.Module): | |
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim, dropout_prob): | |
super(LSTMModel, self).__init__() | |
# Defining the number of layers and the nodes in each layer | |
self.hidden_dim = hidden_dim | |
self.layer_dim = layer_dim | |
# LSTM layers | |
self.lstm = nn.LSTM( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class GRUModel(nn.Module): | |
def __init__(self, input_dim, hidden_dim, layer_dim, output_dim, dropout_prob): | |
super(GRUModel, self).__init__() | |
# Defining the number of layers and the nodes in each layer | |
self.layer_dim = layer_dim | |
self.hidden_dim = hidden_dim | |
# GRU layers | |
self.gru = nn.GRU( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Optimization: | |
"""Optimization is a helper class that allows training, validation, prediction. | |
Optimization is a helper class that takes model, loss function, optimizer function | |
learning scheduler (optional), early stopping (optional) as inputs. In return, it | |
provides a framework to train and validate the models, and to predict future values | |
based on the models. | |
Attributes: | |
model (RNNModel, LSTMModel, GRUModel): Model class created for the type of RNN |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df_features = ( | |
df | |
.assign(hour = df.index.hour) | |
.assign(day = df.index.day) | |
.assign(month = df.index.month) | |
.assign(day_of_week = df.index.dayofweek) | |
.assign(week_of_year = df.index.week) | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def onehot_encode_pd(df, col_name): | |
dummies = pd.get_dummies(df[col_name], prefix=col_name) | |
return pd.concat([df, dummies], axis=1).drop(columns=[col_name]) | |
df_features = onehot_encode_pd(df_features, ['month','day','day_of_week','week_of_year']) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.compose import ColumnTransformer | |
from sklearn.preprocessing import OneHotEncoder | |
def onehot_encode(df, onehot_columns): | |
ct = ColumnTransformer( | |
[('onehot', OneHotEncoder(drop='first'), onehot_columns)], | |
remainder='passthrough' | |
) | |
return ct.fit_transform(df) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def generate_cyclical_features(df, col_name, period, start_num=0): | |
kwargs = { | |
f'sin_{col_name}' : lambda x: np.sin(2*np.pi*(df[col_name]-start_num)/period), | |
f'cos_{col_name}' : lambda x: np.cos(2*np.pi*(df[col_name]-start_num)/period) | |
} | |
return df.assign(**kwargs).drop(columns=[col_name]) | |
df_features = generate_cyclical_features(df_features, 'hour', 24, 0) | |
# df_features = generate_cyclical_features(df_features, 'day_of_week', 7, 0) | |
# df_features = generate_cyclical_features(df_features, 'month', 12, 1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from datetime import date | |
import holidays | |
us_holidays = holidays.US() | |
def is_holiday(date): | |
date = date.replace(hour = 0) | |
return 1 if (date in us_holidays) else 0 | |
def add_holiday_col(df, holidays): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sklearn.preprocessing import MinMaxScaler, StandardScaler, MaxAbsScaler, RobustScaler | |
def get_scaler(scaler): | |
scalers = { | |
"minmax": MinMaxScaler, | |
"standard": StandardScaler, | |
"maxabs": MaxAbsScaler, | |
"robust": RobustScaler, | |
} | |
return scalers.get(scaler.lower())() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Optimization: | |
def __init__(self, model, loss_fn, optimizer): | |
self.model = model | |
self.loss_fn = loss_fn | |
self.optimizer = optimizer | |
self.train_losses = [] | |
self.val_losses = [] | |
def train_step(self, x, y): | |
# Sets model to train mode |