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 = list(df_raw.columns)
|
||||||
cols.remove(self.target)
|
cols.remove(self.target)
|
||||||
cols.remove('date')
|
cols.remove('date')
|
||||||
|
self.cols = ['date'] + cols + [self.target]
|
||||||
df_raw = df_raw[['date'] + cols + [self.target]]
|
df_raw = df_raw[['date'] + cols + [self.target]]
|
||||||
# print(cols)
|
# print(cols)
|
||||||
num_train = int(len(df_raw) * 0.7)
|
num_train = int(len(df_raw) * 0.7)
|
||||||
@@ -244,6 +245,11 @@ class Dataset_Custom(Dataset):
|
|||||||
df_data = df_raw[cols_data]
|
df_data = df_raw[cols_data]
|
||||||
elif self.features == 'S':
|
elif self.features == 'S':
|
||||||
df_data = df_raw[[self.target]]
|
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:
|
if self.scale:
|
||||||
train_data = df_data[border1s[0]:border2s[0]]
|
train_data = df_data[border1s[0]:border2s[0]]
|
||||||
@@ -265,8 +271,15 @@ class Dataset_Custom(Dataset):
|
|||||||
data_stamp = data_stamp.transpose(1, 0)
|
data_stamp = data_stamp.transpose(1, 0)
|
||||||
|
|
||||||
self.data_x = data[border1:border2]
|
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
|
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):
|
def __getitem__(self, index):
|
||||||
s_begin = 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.metrics import metric
|
||||||
from utils.Adam import Adam
|
from utils.Adam import Adam
|
||||||
|
|
||||||
|
from tqdm.auto import tqdm
|
||||||
|
|
||||||
import numpy as np
|
import numpy as np
|
||||||
import torch
|
import torch
|
||||||
import torch.nn as nn
|
import torch.nn as nn
|
||||||
@@ -121,58 +123,64 @@ class Exp_Main(Exp_Basic):
|
|||||||
model_optim = self._select_optimizer()
|
model_optim = self._select_optimizer()
|
||||||
criterion = self._select_criterion()
|
criterion = self._select_criterion()
|
||||||
|
|
||||||
for epoch in range(self.args.train_epochs):
|
with tqdm(unit='epoch', total=self.args.train_epochs) as p:
|
||||||
iter_count = 0
|
for epoch in range(self.args.train_epochs):
|
||||||
train_loss = []
|
iter_count = 0
|
||||||
|
train_loss = []
|
||||||
|
|
||||||
self.model.train()
|
self.model.train()
|
||||||
epoch_time = time.time()
|
epoch_time = time.time()
|
||||||
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(train_loader):
|
for i, (batch_x, batch_y, batch_x_mark, batch_y_mark) in enumerate(tqdm(train_loader, leave=False, desc='train')):
|
||||||
iter_count += 1
|
iter_count += 1
|
||||||
model_optim.zero_grad()
|
model_optim.zero_grad()
|
||||||
batch_x = batch_x.float().to(self.device)
|
batch_x = batch_x.float().to(self.device)
|
||||||
|
|
||||||
batch_y = batch_y.float().to(self.device)
|
batch_y = batch_y.float().to(self.device)
|
||||||
batch_x_mark = batch_x_mark.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_mark = batch_y_mark.float().to(self.device)
|
||||||
|
|
||||||
# decoder input
|
# decoder input
|
||||||
dec_inp = torch.zeros_like(batch_y[:, -self.args.pred_len:, :]).float()
|
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)
|
dec_inp = torch.cat([batch_y[:, :self.args.label_len, :], dec_inp], dim=1).float().to(self.device)
|
||||||
|
|
||||||
# encoder - decoder
|
# encoder - decoder
|
||||||
outputs = self.model(batch_x, batch_x_mark, dec_inp, batch_y_mark)
|
outputs = self.model(batch_x, batch_x_mark, dec_inp, batch_y_mark)
|
||||||
|
|
||||||
f_dim = -1 if self.args.features == 'MS' else 0
|
f_dim = -1 if self.args.features == 'MS' else 0
|
||||||
batch_y = batch_y[:, -self.args.pred_len:, f_dim:].to(self.device)
|
batch_y = batch_y[:, -self.args.pred_len:, f_dim:].to(self.device)
|
||||||
loss = criterion(outputs, batch_y)
|
loss = criterion(outputs, batch_y)
|
||||||
train_loss.append(loss.item())
|
train_loss.append(loss.item())
|
||||||
|
|
||||||
if (i + 1) % 100 == 0:
|
# if (i + 1) % 10 == 0:
|
||||||
print("\titers: {0}, epoch: {1} | loss: {2:.7f}".format(i + 1, epoch + 1, loss.item()))
|
# print("\titers: {0}, epoch: {1} | loss: {2:.7f}".format(i + 1, epoch + 1, loss.item()))
|
||||||
speed = (time.time() - time_now) / iter_count
|
# speed = (time.time() - time_now) / iter_count
|
||||||
left_time = speed * ((self.args.train_epochs - epoch) * train_steps - i)
|
# left_time = speed * ((self.args.train_epochs - epoch) * train_steps - i)
|
||||||
print('\tspeed: {:.4f}s/iter; left time: {:.4f}s'.format(speed, left_time))
|
# print('\tspeed: {:.4f}s/iter; left time: {:.4f}s'.format(speed, left_time))
|
||||||
iter_count = 0
|
# iter_count = 0
|
||||||
time_now = time.time()
|
# time_now = time.time()
|
||||||
|
|
||||||
|
# p.desc = f'loss: {loss.item():.7f}'
|
||||||
|
|
||||||
|
|
||||||
loss.backward()
|
loss.backward()
|
||||||
torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0)
|
torch.nn.utils.clip_grad_norm(self.model.parameters(), 1.0)
|
||||||
model_optim.step()
|
model_optim.step()
|
||||||
|
|
||||||
print("Epoch: {} cost time: {}".format(epoch + 1, time.time() - epoch_time))
|
print("Epoch: {} cost time: {}".format(epoch + 1, time.time() - epoch_time))
|
||||||
train_loss = np.average(train_loss)
|
train_loss = np.average(train_loss)
|
||||||
vali_loss = self.vali(vali_data, vali_loader, criterion)
|
vali_loss = self.vali(vali_data, vali_loader, criterion)
|
||||||
test_loss = self.vali(test_data, test_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(
|
p.update()
|
||||||
epoch + 1, train_steps, train_loss, vali_loss, test_loss))
|
p.desc = f'Train Loss: {test_loss:.7f} Vali Loss: {vali_loss:.7f}'
|
||||||
early_stopping(vali_loss, self.model, path)
|
print("Epoch: {0}, Steps: {1} | Train Loss: {2:.7f} Vali Loss: {3:.7f} Test Loss: {4:.7f}".format(
|
||||||
if early_stopping.early_stop:
|
epoch + 1, train_steps, train_loss, vali_loss, test_loss))
|
||||||
print("Early stopping")
|
early_stopping(vali_loss, self.model, path)
|
||||||
break
|
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'
|
best_model_path = path + '/' + 'checkpoint.pth'
|
||||||
self.model.load_state_dict(torch.load(best_model_path))
|
self.model.load_state_dict(torch.load(best_model_path))
|
||||||
@@ -191,7 +199,7 @@ class Exp_Main(Exp_Basic):
|
|||||||
|
|
||||||
self.model.eval()
|
self.model.eval()
|
||||||
with torch.no_grad():
|
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_x = batch_x.float().to(self.device)
|
||||||
batch_y = batch_y.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 + 'pred.npy', preds)
|
||||||
np.save(folder_path + 'true.npy', trues)
|
np.save(folder_path + 'true.npy', trues)
|
||||||
|
|
||||||
return
|
return preds, trues
|
||||||
|
|||||||
+3
-3
@@ -2,6 +2,6 @@
|
|||||||
|
|
||||||
|
|
||||||
TODO:
|
TODO:
|
||||||
- [ ] run on stocks data
|
- [x] run on stocks data
|
||||||
- [ ] multivariate in, univariate out
|
- [x] multivariate in, univariate out
|
||||||
- [ ] graph
|
- [x] graph
|
||||||
|
|||||||
@@ -183,6 +183,10 @@ class EncoderLayer(nn.Module):
|
|||||||
self.dropout2 = nn.Dropout(dropout)
|
self.dropout2 = nn.Dropout(dropout)
|
||||||
|
|
||||||
def forward(self, res, level, attn_mask=None):
|
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)
|
season, season_attn = self._season_block(res)
|
||||||
res = res - season[:, :-self.pred_len]
|
res = res - season[:, :-self.pred_len]
|
||||||
growth, growth_attn = self._growth_block(res)
|
growth, growth_attn = self._growth_block(res)
|
||||||
|
|||||||
@@ -73,6 +73,7 @@ class ETSformer(nn.Module):
|
|||||||
if self.training:
|
if self.training:
|
||||||
x_enc = self.transform.transform(x_enc)
|
x_enc = self.transform.transform(x_enc)
|
||||||
res = self.enc_embedding(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)
|
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)
|
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