mirror of
https://github.com/wassname/ETSformer.git
synced 2026-06-27 16:59:02 +08:00
multivar 2 single, an plot
This commit is contained in:
@@ -229,6 +229,7 @@ class Dataset_Custom(Dataset):
|
||||
cols = list(df_raw.columns)
|
||||
cols.remove(self.target)
|
||||
cols.remove('date')
|
||||
self.cols = ['date'] + cols + [self.target]
|
||||
df_raw = df_raw[['date'] + cols + [self.target]]
|
||||
# print(cols)
|
||||
num_train = int(len(df_raw) * 0.7)
|
||||
@@ -244,6 +245,11 @@ class Dataset_Custom(Dataset):
|
||||
df_data = df_raw[cols_data]
|
||||
elif self.features == 'S':
|
||||
df_data = df_raw[[self.target]]
|
||||
elif self.features == 'M2S':
|
||||
df_data = df_raw[cols + [self.target]]
|
||||
self.n_dims = 1 # len(cols + [self.target])
|
||||
else:
|
||||
raise NotImplementedError(self.features)
|
||||
|
||||
if self.scale:
|
||||
train_data = df_data[border1s[0]:border2s[0]]
|
||||
@@ -265,8 +271,15 @@ class Dataset_Custom(Dataset):
|
||||
data_stamp = data_stamp.transpose(1, 0)
|
||||
|
||||
self.data_x = data[border1:border2]
|
||||
self.data_y = data[border1:border2]
|
||||
self.dates = df_raw['date'][border1:border2]
|
||||
# self.data_y = data[border1:border2]
|
||||
# y is just the col we predict
|
||||
self.data_y = data[border1:border2][:, [-1]]
|
||||
self.data_stamp = data_stamp
|
||||
|
||||
# TODO check this is right. For example check that it makes the df
|
||||
o = self.seq_len - self.label_len # + self.label_len
|
||||
self.index = self.dates.iloc[o:].iloc[:len(self)]
|
||||
|
||||
def __getitem__(self, index):
|
||||
s_begin = index
|
||||
|
||||
+52
-44
@@ -5,6 +5,8 @@ from utils.tools import EarlyStopping, adjust_learning_rate
|
||||
from utils.metrics import metric
|
||||
from utils.Adam import Adam
|
||||
|
||||
from tqdm.auto import tqdm
|
||||
|
||||
import numpy as np
|
||||
import torch
|
||||
import torch.nn as nn
|
||||
@@ -121,58 +123,64 @@ class Exp_Main(Exp_Basic):
|
||||
model_optim = self._select_optimizer()
|
||||
criterion = self._select_criterion()
|
||||
|
||||
for epoch in range(self.args.train_epochs):
|
||||
iter_count = 0
|
||||
train_loss = []
|
||||
with tqdm(unit='epoch', total=self.args.train_epochs) as p:
|
||||
for epoch in range(self.args.train_epochs):
|
||||
iter_count = 0
|
||||
train_loss = []
|
||||
|
||||
self.model.train()
|
||||
epoch_time = time.time()
|
||||
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(train_loader):
|
||||
iter_count += 1
|
||||
model_optim.zero_grad()
|
||||
batch_x = batch_x.float().to(self.device)
|
||||
self.model.train()
|
||||
epoch_time = time.time()
|
||||
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(tqdm(train_loader, leave=False, desc='train')):
|
||||
iter_count += 1
|
||||
model_optim.zero_grad()
|
||||
batch_x = batch_x.float().to(self.device)
|
||||
|
||||
batch_y = batch_y.float().to(self.device)
|
||||
batch_x_mark = batch_x_mark.float().to(self.device)
|
||||
batch_y_mark = batch_y_mark.float().to(self.device)
|
||||
batch_y = batch_y.float().to(self.device)
|
||||
batch_x_mark = batch_x_mark.float().to(self.device)
|
||||
batch_y_mark = batch_y_mark.float().to(self.device)
|
||||
|
||||
# decoder input
|
||||
dec_inp = torch.zeros_like(batch_y[:, -self.args.pred_len:, :]).float()
|
||||
dec_inp = torch.cat([batch_y[:, :self.args.label_len, :], dec_inp], dim=1).float().to(self.device)
|
||||
# decoder input
|
||||
dec_inp = torch.zeros_like(batch_y[:, -self.args.pred_len:, :]).float()
|
||||
dec_inp = torch.cat([batch_y[:, :self.args.label_len, :], dec_inp], dim=1).float().to(self.device)
|
||||
|
||||
# encoder - decoder
|
||||
outputs = self.model(batch_x, batch_x_mark, dec_inp, batch_y_mark)
|
||||
# encoder - decoder
|
||||
outputs = self.model(batch_x, batch_x_mark, dec_inp, batch_y_mark)
|
||||
|
||||
f_dim = -1 if self.args.features == 'MS' else 0
|
||||
batch_y = batch_y[:, -self.args.pred_len:, f_dim:].to(self.device)
|
||||
loss = criterion(outputs, batch_y)
|
||||
train_loss.append(loss.item())
|
||||
f_dim = -1 if self.args.features == 'MS' else 0
|
||||
batch_y = batch_y[:, -self.args.pred_len:, f_dim:].to(self.device)
|
||||
loss = criterion(outputs, batch_y)
|
||||
train_loss.append(loss.item())
|
||||
|
||||
if (i + 1) % 100 == 0:
|
||||
print("\titers: {0}, epoch: {1} | loss: {2:.7f}".format(i + 1, epoch + 1, loss.item()))
|
||||
speed = (time.time() - time_now) / iter_count
|
||||
left_time = speed * ((self.args.train_epochs - epoch) * train_steps - i)
|
||||
print('\tspeed: {:.4f}s/iter; left time: {:.4f}s'.format(speed, left_time))
|
||||
iter_count = 0
|
||||
time_now = time.time()
|
||||
# if (i + 1) % 10 == 0:
|
||||
# print("\titers: {0}, epoch: {1} | loss: {2:.7f}".format(i + 1, epoch + 1, loss.item()))
|
||||
# speed = (time.time() - time_now) / iter_count
|
||||
# left_time = speed * ((self.args.train_epochs - epoch) * train_steps - i)
|
||||
# print('\tspeed: {:.4f}s/iter; left time: {:.4f}s'.format(speed, left_time))
|
||||
# iter_count = 0
|
||||
# time_now = time.time()
|
||||
|
||||
# p.desc = f'loss: {loss.item():.7f}'
|
||||
|
||||
|
||||
loss.backward()
|
||||
torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0)
|
||||
model_optim.step()
|
||||
loss.backward()
|
||||
torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0)
|
||||
model_optim.step()
|
||||
|
||||
print("Epoch: {} cost time: {}".format(epoch + 1, time.time() - epoch_time))
|
||||
train_loss = np.average(train_loss)
|
||||
vali_loss = self.vali(vali_data, vali_loader, criterion)
|
||||
test_loss = self.vali(test_data, test_loader, criterion)
|
||||
print("Epoch: {} cost time: {}".format(epoch + 1, time.time() - epoch_time))
|
||||
train_loss = np.average(train_loss)
|
||||
vali_loss = self.vali(vali_data, vali_loader, criterion)
|
||||
test_loss = self.vali(test_data, test_loader, criterion)
|
||||
|
||||
print("Epoch: {0}, Steps: {1} | Train Loss: {2:.7f} Vali Loss: {3:.7f} Test Loss: {4:.7f}".format(
|
||||
epoch + 1, train_steps, train_loss, vali_loss, test_loss))
|
||||
early_stopping(vali_loss, self.model, path)
|
||||
if early_stopping.early_stop:
|
||||
print("Early stopping")
|
||||
break
|
||||
p.update()
|
||||
p.desc = f'Train Loss: {test_loss:.7f} Vali Loss: {vali_loss:.7f}'
|
||||
print("Epoch: {0}, Steps: {1} | Train Loss: {2:.7f} Vali Loss: {3:.7f} Test Loss: {4:.7f}".format(
|
||||
epoch + 1, train_steps, train_loss, vali_loss, test_loss))
|
||||
early_stopping(vali_loss, self.model, path)
|
||||
if early_stopping.early_stop:
|
||||
print("Early stopping")
|
||||
break
|
||||
|
||||
adjust_learning_rate(model_optim, epoch + 1, self.args)
|
||||
adjust_learning_rate(model_optim, epoch + 1, self.args)
|
||||
|
||||
best_model_path = path + '/' + 'checkpoint.pth'
|
||||
self.model.load_state_dict(torch.load(best_model_path))
|
||||
@@ -191,7 +199,7 @@ class Exp_Main(Exp_Basic):
|
||||
|
||||
self.model.eval()
|
||||
with torch.no_grad():
|
||||
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(test_loader):
|
||||
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(tqdm(test_loader, leave=False)):
|
||||
batch_x = batch_x.float().to(self.device)
|
||||
batch_y = batch_y.float().to(self.device)
|
||||
|
||||
@@ -237,4 +245,4 @@ class Exp_Main(Exp_Basic):
|
||||
np.save(folder_path + 'pred.npy', preds)
|
||||
np.save(folder_path + 'true.npy', trues)
|
||||
|
||||
return
|
||||
return preds, trues
|
||||
|
||||
+3
-3
@@ -2,6 +2,6 @@
|
||||
|
||||
|
||||
TODO:
|
||||
- [ ] run on stocks data
|
||||
- [ ] multivariate in, univariate out
|
||||
- [ ] graph
|
||||
- [x] run on stocks data
|
||||
- [x] multivariate in, univariate out
|
||||
- [x] graph
|
||||
|
||||
@@ -183,6 +183,10 @@ class EncoderLayer(nn.Module):
|
||||
self.dropout2 = nn.Dropout(dropout)
|
||||
|
||||
def forward(self, res, level, attn_mask=None):
|
||||
|
||||
# when c_in!=c_out assume target columns are at end of channels
|
||||
level = level[:, :, -self.c_out:]
|
||||
|
||||
season, season_attn = self._season_block(res)
|
||||
res = res - season[:, :-self.pred_len]
|
||||
growth, growth_attn = self._growth_block(res)
|
||||
|
||||
@@ -73,6 +73,7 @@ class ETSformer(nn.Module):
|
||||
if self.training:
|
||||
x_enc = self.transform.transform(x_enc)
|
||||
res = self.enc_embedding(x_enc)
|
||||
|
||||
level, growths, seasons, season_attns, growth_attns = self.encoder(res, x_enc, attn_mask=enc_self_mask)
|
||||
|
||||
growth, season, growth_dampings = self.decoder(growths, seasons)
|
||||
|
||||
+1435
-181
File diff suppressed because one or more lines are too long
BIN
Binary file not shown.
Reference in New Issue
Block a user