Comments (5)
@pnmartinez sorry for the late reply. When you train, do you find exactly the same results between runs? Or is it between 2 evaluations of the same model?
from n-beats.
If I run this (long) example, I get different values every time I train it:
import warnings
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
from torch import optim
from torch.nn import functional as F
from nbeats_pytorch.model import NBeatsNet
from trainer_pytorch import save
warnings.filterwarnings(action='ignore', message='Setting attributes')
# plot utils.
def plot_scatter(*args, **kwargs):
plt.plot(*args, **kwargs)
plt.scatter(*args, **kwargs)
# simple batcher.
def data_generator(x, y, size):
assert len(x) == len(y)
batches = []
for ii in range(0, len(x), size):
batches.append((x[ii:ii + size], y[ii:ii + size]))
for batch in batches:
yield batch
def main():
forecast_length = 5
backcast_length = 3 * forecast_length
batch_size = 10 # greater than 4 for viz
milk = pd.read_csv('data/milk.csv', index_col=0, parse_dates=True)
print(milk.head())
milk = milk.values.flatten() # just keep np array here for simplicity.
# data backcast/forecast generation.
x, y = [], []
for epoch in range(backcast_length, len(milk) - forecast_length):
x.append(milk[epoch - backcast_length:epoch])
y.append(milk[epoch:epoch + forecast_length])
x = np.array(x)
y = np.array(y)
# split train/test.
c = int(len(x) * 0.8)
x_train, y_train = x[:c], y[:c]
x_test, y_test = x[c:], y[c:]
# normalization.
norm_constant = np.max(x_train)
x_train, y_train = x_train / norm_constant, y_train / norm_constant
x_test, y_test = x_test / norm_constant, y_test / norm_constant
# model
net = NBeatsNet(
stack_types=(NBeatsNet.GENERIC_BLOCK, NBeatsNet.GENERIC_BLOCK),
forecast_length=forecast_length,
backcast_length=backcast_length,
hidden_layer_units=128,
)
optimiser = optim.Adam(lr=1e-4, params=net.parameters())
grad_step = 0
for epoch in range(1000):
# train.
net.train()
train_loss = []
for x_train_batch, y_train_batch in data_generator(x_train, y_train, batch_size):
grad_step += 1
optimiser.zero_grad()
_, forecast = net(torch.tensor(x_train_batch, dtype=torch.float).to(net.device))
loss = F.mse_loss(forecast, torch.tensor(y_train_batch, dtype=torch.float).to(net.device))
train_loss.append(loss.item())
loss.backward()
optimiser.step()
train_loss = np.mean(train_loss)
# test.
net.eval()
_, forecast = net(torch.tensor(x_test, dtype=torch.float))
test_loss = F.mse_loss(forecast, torch.tensor(y_test, dtype=torch.float)).item()
p = forecast.detach().numpy()
if epoch % 100 == 0:
subplots = [221, 222, 223, 224]
plt.figure(1)
for plot_id, i in enumerate(np.random.choice(range(len(x_test)), size=4, replace=False)):
ff, xx, yy = p[i] * norm_constant, x_test[i] * norm_constant, y_test[i] * norm_constant
plt.subplot(subplots[plot_id])
plt.grid()
plot_scatter(range(0, backcast_length), xx, color='b')
plot_scatter(range(backcast_length, backcast_length + forecast_length), yy, color='g')
plot_scatter(range(backcast_length, backcast_length + forecast_length), ff, color='r')
plt.show()
with torch.no_grad():
save(net, optimiser, grad_step)
print(f'epoch = {str(epoch).zfill(4)}, '
f'grad_step = {str(grad_step).zfill(6)}, '
f'tr_loss (epoch) = {1000 * train_loss:.3f}, '
f'te_loss (epoch) = {1000 * test_loss:.3f}')
if __name__ == '__main__':
main()
Output 1
epoch = 0000, grad_step = 000012, tr_loss (epoch) = 540.268, te_loss (epoch) = 645.095
[...]
Output 2
epoch = 0000, grad_step = 000012, tr_loss (epoch) = 463.423, te_loss (epoch) = 580.784
[...]
Could it be that you always load a model before doing something? That could be a possible explanation. Instead of re-init the weights from scratch you keep reading from an old checkpoint at epoch 0.
from n-beats.
Hi @philipperemy ,
I will test this in a new environment as soon as I have the time, so I can also test if the updated pip
package works well.
In the meantime, thanks in advance!
from n-beats.
@pnmartinez cool let me know.
from n-beats.
I'll close this issue for now. Let me know if you could fix it with the latest version.
from n-beats.
Related Issues (20)
- Is torchvision really a requirement? HOT 2
- GENERIC_BLOCK usage generating TypeError HOT 3
- Trend coefficients in pytorch implementation HOT 1
- Return backcast for Keras model HOT 4
- Incorporating deterministic exogenous features HOT 3
- Multidimensional input fails HOT 1
- How to work with multivariate data? HOT 1
- GenericBlock Module Issues HOT 8
- Polynomial basis for backcast HOT 2
- The parameters 'exo_dim' and 'exo_shape' in the class 'NBeatsNet' HOT 1
- how to use/deploy n-beats for real world problems when the algorithm uses teacher forcing? HOT 1
- Problem in reimplement m4 result in origin parper HOT 10
- Keras backend does not support input_dim > 1 HOT 1
- Is there a unsupervised version of n-beats? HOT 1
- Can we use a pandas dataframe with multiple dependent variables? HOT 11
- Installation issue with tensor flow 2.5.0 HOT 8
- No module named 'nbeats_keras' HOT 1
- Delete
- How to draw the picture in the paper? HOT 7
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from n-beats.