This file contains hidden or 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
| State = namedtuple('State', ('W', 'coords', 'partial_solution')) |
This file contains hidden or 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
| Q_func = init_model() # We create our Q-function neural net, to be trained | |
| memory = Memory() # We create a replay memory that we'll use to store experiences (state/action/reward tuples) | |
| for episode in range(NR_EPISODES): | |
| # sample a new random graph (coordinates and distance matrix) | |
| coords, W = get_graph_mat(n=NR_NODES) | |
| # initialize a partial solution - start from a random node | |
| solution = [random.randint(0, NR_NODES-1)] |
This file contains hidden or 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
| Experience = namedtuple('Experience', ('state', 'action', 'reward', 'next_state')) |
This file contains hidden or 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 QNet(nn.Module): | |
| """ The neural net that will parameterize the function Q(s, a) | |
| The input is the state (containing the graph and visited nodes), | |
| and the output is a vector of size N containing Q(s, a) for each of the N actions a. | |
| """ | |
| def __init__(self, emb_dim, T=4): | |
| """ emb_dim: embedding dimension p | |
| T: number of iterations for the graph embedding |
This file contains hidden or 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
| # First, some imports: | |
| import numpy as np | |
| from darts.utils import timeseries_generation as tg | |
| np.random.seed(42) | |
| LENGTH = 3 * 365 # 3 years of daily data | |
| # Melting: a sine with yearly periodicity and additive white noise | |
| melting = (tg.sine_timeseries(length=LENGTH, |
This file contains hidden or 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 darts.metrics import rmse | |
| # We first set aside the first 80% as training series: | |
| flow_train, _ = flow.split_before(0.8) | |
| def eval_model(model, past_covariates=None, future_covariates=None): | |
| # Past and future covariates are optional because they won't always be used in our tests | |
| # We backtest the model on the last 20% of the flow series, with a horizon of 10 steps: | |
| backtest = model.historical_forecasts(series=flow, |
This file contains hidden or 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 darts.models import BlockRNNModel | |
| brnn_no_cov = BlockRNNModel(input_chunk_length=30, | |
| output_chunk_length=10, | |
| n_rnn_layers=2) | |
| brnn_no_cov.fit(flow_train, | |
| epochs=100, | |
| verbose=True) |
This file contains hidden or 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
| brnn_melting = BlockRNNModel(input_chunk_length=30, | |
| output_chunk_length=10, | |
| n_rnn_layers=2) | |
| brnn_melting.fit(flow_train, | |
| past_covariates=melting, | |
| epochs=100, | |
| verbose=True) | |
| eval_model(brnn_melting, |
This file contains hidden or 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
| brnn_melting_and_rain = BlockRNNModel(input_chunk_length=30, | |
| output_chunk_length=10, | |
| n_rnn_layers=2) | |
| brnn_melting_and_rain.fit(flow_train, | |
| past_covariates=melting.stack(rainfalls), | |
| epochs=100, | |
| verbose=True) | |
| eval_model(brnn_melting_and_rain, |
This file contains hidden or 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 darts.models import RNNModel | |
| rnn_rain = RNNModel(input_chunk_length=30, | |
| training_length=40, | |
| n_rnn_layers=2) | |
| rnn_rain.fit(flow_train, | |
| future_covariates=rainfalls, | |
| epochs=100, | |
| verbose=True) |
OlderNewer