Skip to content

Instantly share code, notes, and snippets.

View hrzn's full-sized avatar

Julien Herzen hrzn

View GitHub Profile
@hrzn
hrzn / state.py
Last active February 7, 2020 21:04
State = namedtuple('State', ('W', 'coords', 'partial_solution'))
@hrzn
hrzn / q_learning.py
Last active February 14, 2020 12:58
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)]
Experience = namedtuple('Experience', ('state', 'action', 'reward', 'next_state'))
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
# 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,
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,
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)
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,
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,
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)