def train(X, y, args, quantiles): num_ts, num_periods, num_features = X.shape num_quantiles = len(quantiles) model = MQRNN(args.seq_len, num_quantiles, num_features, args.embedding_size, args.encoder_hidden_size, args.n_layers, args.decoder_hidden_size) optimizer = Adam(model.parameters(), lr=args.lr) Xtr, ytr, Xte, yte = util.train_test_split(X, y) losses = [] yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) num_obs_to_train = args.num_obs_to_train seq_len = args.seq_len progress = ProgressBar() for epoch in progress(range(args.num_epoches)): # print("Epoch {} start...".format(epoch)) for step in range(args.step_per_epoch): X_train_batch, y_train_batch, Xf, yf = batch_generator( Xtr, ytr, num_obs_to_train, args.seq_len, args.batch_size) X_train_tensor = torch.from_numpy(X_train_batch).float() y_train_tensor = torch.from_numpy(y_train_batch).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() ypred = model(X_train_tensor, y_train_tensor, Xf) # quantile loss loss = torch.zeros_like(yf) num_ts = Xf.size(0) for q, rho in enumerate(quantiles): ypred_rho = ypred[:, :, q].view(num_ts, -1) e = ypred_rho - yf loss += torch.max(rho * e, (rho - 1) * e) loss = loss.mean() losses.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() mape_list = [] X_test = Xte[:, -seq_len - num_obs_to_train:-seq_len, :].reshape( (num_ts, -1, num_features)) Xf_test = Xte[:, -seq_len:, :].reshape((num_ts, -1, num_features)) y_test = yte[:, -seq_len - num_obs_to_train:-seq_len].reshape((num_ts, -1)) if yscaler is not None: y_test = yscaler.transform(y_test) yf_test = yte[:, -seq_len:] ypred = model(X_test, y_test, Xf_test) # (1, num_quantiles, output_horizon) ypred = ypred.data.numpy() if yscaler is not None: ypred = yscaler.inverse_transform(ypred) ypred = np.maximum(0, ypred) # P50 quantile MAPE mape = util.MAPE(yf_test, ypred[:, :, 1]) print("MAPE: {}".format(mape)) mape_list.append(mape) if args.show_plot: show_idx = 0 plt.figure(1) plt.plot([k + seq_len + num_obs_to_train - seq_len \ for k in range(seq_len)], ypred[show_idx, :, 1], "r-") plt.fill_between(x=[k + seq_len + num_obs_to_train - seq_len for k in range(seq_len)], \ y1=ypred[show_idx, :, 0], y2=ypred[show_idx, :, 2], alpha=0.5) plt.title('Prediction uncertainty') yplot = yte[show_idx, -seq_len - num_obs_to_train:] plt.plot(range(len(yplot)), yplot, "k-") plt.legend(["P50 forecast", "true", "P10-P90 quantile"], loc="upper left") plt.xlabel("Periods") plt.ylabel("Y") plt.show() return losses, mape_list
def train(Data, args): ''' Args: - X (array like): shape (num_samples, num_features, num_periods) - y (array like): shape (num_samples, num_periods) - epoches (int): number of epoches to run - step_per_epoch (int): steps per epoch to run - seq_len (int): output horizon - likelihood (str): what type of likelihood to use, default is gaussian - num_skus_to_show (int): how many skus to show in test phase - num_results_to_sample (int): how many samples in test phase as prediction ''' evaluateL2 = nn.MSELoss(size_average=False) evaluateL1 = nn.L1Loss(size_average=False) if args.L1Loss: criterion = nn.L1Loss(size_average=False) else: criterion = nn.MSELoss(size_average=False) yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() elif args.max_scaler: yscaler = util.MaxScaler() model = TPALSTM(1, args.seq_len, args.hidden_size, args.num_obs_to_train, args.n_layers) # modelPath = "/home/isabella/Documents/5331/tpaLSTM/model/electricity.pt" # # with open(modelPath, 'rb') as f: # model = torch.load(f) optimizer = Adam(model.parameters(), lr=args.lr) random.seed(2) # select sku with most top n quantities Xtr = np.asarray(Data.train[0].permute(2, 0, 1)) ytr = np.asarray(Data.train[1].permute(1, 0)) Xte = np.asarray(Data.test[0].permute(2, 0, 1)) yte = np.asarray(Data.test[1].permute(1, 0)) Xeva = np.asarray(Data.valid[0].permute(2, 0, 1)) yeva = np.asarray(Data.valid[1].permute(1, 0)) # print("\nRearranged Data") # print("Xtr.size", Xtr.shape) # print("ytr.size", ytr.shape) # print("Xte.size", Xte.shape) # print("yte.size", yte.shape) # print("Xeva.size", Xeva.shape) # print("yeva.size", yeva.shape) num_ts, num_periods, num_features = Xtr.shape if yscaler is not None: ytr = yscaler.fit_transform(ytr) # training seq_len = args.seq_len obs_len = args.num_obs_to_train progress = ProgressBar() best_val = np.inf total_loss = 0 n_samples = 0 losses = [] for epoch in progress(range(args.num_epoches)): epoch_start_time = time.time() model.train() total_loss = 0 n_samples = 0 # print("\n\nData.get_batches") # for X,Y in Data.get_batches(Data.train[0], Data.train[1], args.batch_size, True): # print("X.shape",X.shape) # print("Y.shape", Y.shape) for step in range(args.step_per_epoch): print(step) Xtrain, ytrain, Xf, yf, batch = util.batch_generator( Xtr, ytr, obs_len, seq_len, args.batch_size) Xtrain = torch.from_numpy(Xtrain).float() ytrain = torch.from_numpy(ytrain).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() for i in range(len(Xeva[0][0])): ytrain = Xtrain[:, :, i] yf = Xf[:, :, i] ypred = model(ytrain) scale = Data.scale[batch] scale = scale.view([scale.size(0), 1]) loss = criterion(ypred * scale, yf * scale) losses.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() total_loss += loss.item() n_samples += (ypred.size(0)) train_loss = total_loss / n_samples val_loss, val_rae, val_corr = evaluate(Data, Xeva, yeva, model, evaluateL2, evaluateL1, args.batch_size, args, yscaler) print( '| end of epoch {:3d} | time: {:5.2f}s | train_loss {:5.4f} | valid rse {:5.4f} | valid rae {:5.4f} | valid corr {:5.4f}' .format(epoch, (time.time() - epoch_start_time), train_loss, val_loss, val_rae, val_corr)) # Save the model if the validation loss is the best we've seen so far. if val_loss < best_val: with open(args.save, 'wb') as f: torch.save(model, f) best_val = val_loss if epoch % 5 == 0: test_acc, test_rae, test_corr = evaluate(Data, Xte, yte, model, evaluateL2, evaluateL1, args.batch_size, args, yscaler) print("test rse {:5.4f} | test rae {:5.4f} | test corr {:5.4f}". format(test_acc, test_rae, test_corr))
def train(X, y, args): ''' Args: - X (array like): shape (num_samples, num_features, num_periods) - y (array like): shape (num_samples, num_periods) - epoches (int): number of epoches to run - step_per_epoch (int): steps per epoch to run - seq_len (int): output horizon - likelihood (str): what type of likelihood to use, default is gaussian - num_skus_to_show (int): how many skus to show in test phase - num_results_to_sample (int): how many samples in test phase as prediction ''' # rho = args.quantile num_ts, num_periods, num_features = X.shape model = DFRNN(num_features, args.noise_nlayers, args.noise_hidden_size, args.global_nlayers, args.global_hidden_size, args.n_factors) optimizer = Adam(model.parameters(), lr=args.lr) random.seed(2) # select sku with most top n quantities Xtr, ytr, Xte, yte = util.train_test_split(X, y) losses = [] cnt = 0 yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) # training progress = ProgressBar() seq_len = args.seq_len num_obs_to_train = args.num_obs_to_train for epoch in progress(range(args.num_epoches)): # print("Epoch {} starts...".format(epoch)) for step in range(args.step_per_epoch): Xtrain, ytrain, Xf, yf = batch_generator(Xtr, ytr, num_obs_to_train, seq_len, args.batch_size) Xtrain_tensor = torch.from_numpy(Xtrain).float() ytrain_tensor = torch.from_numpy(ytrain).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() mu, sigma = model(Xtrain_tensor) loss = util.gaussian_likelihood_loss(ytrain_tensor, mu, sigma) losses.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() cnt += 1 # test mape_list = [] # select skus with most top K X_test = Xte[:, -seq_len - num_obs_to_train:-seq_len, :].reshape( (num_ts, -1, num_features)) Xf_test = Xte[:, -seq_len:, :].reshape((num_ts, -1, num_features)) y_test = yte[:, -seq_len - num_obs_to_train:-seq_len].reshape((num_ts, -1)) yf_test = yte[:, -seq_len:].reshape((num_ts, -1)) if yscaler is not None: y_test = yscaler.transform(y_test) result = [] n_samples = args.sample_size for _ in tqdm(range(n_samples)): y_pred = model.sample(Xf_test) y_pred = y_pred.data.numpy() if yscaler is not None: y_pred = yscaler.inverse_transform(y_pred) result.append(y_pred.reshape((-1, 1))) result = np.concatenate(result, axis=1) p50 = np.quantile(result, 0.5, axis=1) p90 = np.quantile(result, 0.9, axis=1) p10 = np.quantile(result, 0.1, axis=1) mape = util.MAPE(yf_test, p50) print("P50 MAPE: {}".format(mape)) mape_list.append(mape) if args.show_plot: plt.figure(1, figsize=(20, 5)) plt.plot([k + seq_len + num_obs_to_train - seq_len \ for k in range(seq_len)], p50, "r-") plt.fill_between(x=[k + seq_len + num_obs_to_train - seq_len for k in range(seq_len)], \ y1=p10, y2=p90, alpha=0.5) plt.title('Prediction uncertainty') yplot = yte[-1, -seq_len - num_obs_to_train:] plt.plot(range(len(yplot)), yplot, "k-") plt.legend(["P50 forecast", "true", "P10-P90 quantile"], loc="upper left") ymin, ymax = plt.ylim() plt.vlines(seq_len + num_obs_to_train - seq_len, ymin, ymax, color="blue", linestyles="dashed", linewidth=2) plt.ylim(ymin, ymax) plt.xlabel("Periods") plt.ylabel("Y") plt.show() return losses, mape_list
def train(X, y, args): num_ts, num_periods, num_features = X.shape Xtr, ytr, Xte, yte = util.train_test_split(X, y) yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) progress = ProgressBar() seq_len = args.seq_len num_obs_to_train = args.num_obs_to_train model = ExtremeModel(num_features) optimizer = Adam(model.parameters(), lr=args.lr) losses = [] cnt = 0 for epoch in progress(range(args.num_epoches)): # print("Epoch {} starts...".format(epoch)) for step in range(args.step_per_epoch): Xtrain, ytrain, Xf, yf = batch_generator(Xtr, ytr, num_obs_to_train, seq_len, args.batch_size) Xtrain_tensor = torch.from_numpy(Xtrain).float() ytrain_tensor = torch.from_numpy(ytrain).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() ypred = model(Xtrain_tensor, Xf) loss = F.mse_loss(ypred, yf) losses.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() cnt += 1 mape_list = [] mse_list = [] rmse_list = [] mae_list = [] # select skus with most top K X_test = Xte[:, -seq_len - num_obs_to_train:-seq_len, :].reshape( (num_ts, -1, num_features)) Xf_test = Xte[:, -seq_len:, :].reshape((num_ts, -1, num_features)) y_test = yte[:, -seq_len - num_obs_to_train:-seq_len].reshape((num_ts, -1)) yf_test = yte[:, -seq_len:].reshape((num_ts, -1)) if yscaler is not None: y_test = yscaler.transform(y_test) ypred = model(X_test, Xf_test) ypred = ypred.data.numpy() if yscaler is not None: ypred = yscaler.inverse_transform(ypred) mape = util.MAPE(yf_test, ypred) mae = util.MAE(yf_test, ypred) mse = util.MSE(yf_test, ypred) rmse = util.RMSE(yf_test, ypred) print("MAE: {}".format(mae)) print("RMSE: {}".format(rmse)) print("MSE: {}".format(mse)) print("MAPE: {}".format(mape)) mape_list.append(mape) mse_list.append(mse) mae_list.append(mae) rmse_list.append(rmse) if args.show_plot: plt.figure(1) plt.plot( [k + seq_len + num_obs_to_train - seq_len for k in range(seq_len)], ypred[-1], "r-") plt.title('Prediction uncertainty') yplot = yte[-1, -seq_len - num_obs_to_train:] plt.plot(range(len(yplot)), yplot, "k-") plt.legend(["forecast", "true"], loc="upper left") plt.xlabel("Periods") plt.ylabel("Y") plt.show() return losses, mape_list, mse_list, mae_list, rmse_list
def train(X, y, args): ''' Args: - X (array like): shape (num_samples, num_features, num_periods) - y (array like): shape (num_samples, num_periods) - epoches (int): number of epoches to run - step_per_epoch (int): steps per epoch to run - seq_len (int): output horizon - likelihood (str): what type of likelihood to use, default is gaussian - num_skus_to_show (int): how many skus to show in test phase - num_results_to_sample (int): how many samples in test phase as prediction ''' rho = args.quantile num_ts, num_periods, num_features = X.shape model = DeepAR(num_features, args.embedding_size, args.hidden_size, args.n_layers, args.lr, args.likelihood) optimizer = Adam(model.parameters(), lr=args.lr) random.seed(2) # select sku with most top n quantities Xtr, ytr, Xte, yte = util.train_test_split(X, y) losses = [] cnt = 0 yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) # training seq_len = args.seq_len num_obs_to_train = args.num_obs_to_train progress = ProgressBar() for epoch in progress(range(args.num_epoches)): # print("Epoch {} starts...".format(epoch)) for step in range(args.step_per_epoch): Xtrain, ytrain, Xf, yf = batch_generator(Xtr, ytr, num_obs_to_train, seq_len, args.batch_size) Xtrain_tensor = torch.from_numpy(Xtrain).float() ytrain_tensor = torch.from_numpy(ytrain).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() ypred, mu, sigma = model(Xtrain_tensor, ytrain_tensor, Xf) # ypred_rho = ypred # e = ypred_rho - yf # loss = torch.max(rho * e, (rho - 1) * e).mean() ## gaussian loss ytrain_tensor = torch.cat([ytrain_tensor, yf], dim=1) loss = util.gaussian_likelihood_loss(ytrain_tensor, mu, sigma) losses.append(loss.item()) optimizer.zero_grad() loss.backward() optimizer.step() cnt += 1 # test mape_list = [] # select skus with most top K X_test = Xte[:, -seq_len - num_obs_to_train:-seq_len, :].reshape( (num_ts, -1, num_features)) Xf_test = Xte[:, -seq_len:, :].reshape((num_ts, -1, num_features)) y_test = yte[:, -seq_len - num_obs_to_train:-seq_len].reshape((num_ts, -1)) yf_test = yte[:, -seq_len:].reshape((num_ts, -1)) if yscaler is not None: y_test = yscaler.transform(y_test) y_pred, _, _ = model(X_test, y_test, Xf_test) y_pred = y_pred.data.numpy() if yscaler is not None: y_pred = yscaler.inverse_transform(y_pred) mape = util.MAPE(yf_test, y_pred) print("MAPE: {}".format(mape)) mape_list.append(mape) if args.show_plot: plt.figure(1) plt.plot([k + seq_len + num_obs_to_train - seq_len \ for k in range(seq_len)], y_pred[-1], "r-") plt.title('Prediction uncertainty') yplot = yte[-1, -seq_len - num_obs_to_train:] plt.plot(range(len(yplot)), yplot, "k-") plt.legend(["P50 forecast", "true", "P10-P90 quantile"], loc="upper left") plt.xlabel("Periods") plt.ylabel("Y") plt.show() return losses, mape_list
def train(X, y, args): ''' Args: - X (array like): shape (num_samples, num_features, num_periods) - y (array like): shape (num_samples, num_periods) - epoches (int): number of epoches to run - step_per_epoch (int): steps per epoch to run - seq_len (int): output horizon - likelihood (str): what type of likelihood to use, default is gaussian - num_skus_to_show (int): how many skus to show in test phase - num_results_to_sample (int): how many samples in test phase as prediction ''' num_ts, num_periods, num_features = X.shape model = TPALSTM(1, args.seq_len, args.hidden_size, args.num_obs_to_train, args.n_layers) optimizer = Adam(model.parameters(), lr=args.lr) random.seed(2) # select sku with most top n quantities Xtr, ytr, Xte, yte = util.train_test_split(X, y) losses = [] cnt = 0 yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() elif args.max_scaler: yscaler = util.MaxScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) # training seq_len = args.seq_len obs_len = args.num_obs_to_train progress = ProgressBar() for epoch in progress(range(args.num_epoches)): # print("Epoch {} starts...".format(epoch)) for step in range(args.step_per_epoch): Xtrain, ytrain, Xf, yf = util.batch_generator( Xtr, ytr, obs_len, seq_len, args.batch_size) Xtrain = torch.from_numpy(Xtrain).float() ytrain = torch.from_numpy(ytrain).float() Xf = torch.from_numpy(Xf).float() yf = torch.from_numpy(yf).float() ypred = model(ytrain) # loss = util.RSE(ypred, yf) loss = F.mse_loss(ypred, yf) losses.append(loss.item()) optimizer.zero_grad() loss.backward() # torch.nn.utils.clip_grad_norm_(model.parameters(), 1) optimizer.step() # test mape_list = [] # select skus with most top K X_test = Xte[:, -seq_len - obs_len:-seq_len, :].reshape( (num_ts, -1, num_features)) Xf_test = Xte[:, -seq_len:, :].reshape((num_ts, -1, num_features)) y_test = yte[:, -seq_len - obs_len:-seq_len].reshape((num_ts, -1)) yf_test = yte[:, -seq_len:].reshape((num_ts, -1)) yscaler = None if args.standard_scaler: yscaler = util.StandardScaler() elif args.log_scaler: yscaler = util.LogScaler() elif args.mean_scaler: yscaler = util.MeanScaler() elif args.max_scaler: yscaler = util.MaxScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) if yscaler is not None: y_test = yscaler.fit_transform(y_test) X_test = torch.from_numpy(X_test).float() y_test = torch.from_numpy(y_test).float() Xf_test = torch.from_numpy(Xf_test).float() ypred = model(y_test) ypred = ypred.data.numpy() if yscaler is not None: ypred = yscaler.inverse_transform(ypred) ypred = ypred.ravel() loss = np.sqrt(np.sum(np.square(yf_test - ypred))) print("losses: ", loss) if args.show_plot: plt.figure(1, figsize=(20, 5)) plt.plot([k + seq_len + obs_len - seq_len \ for k in range(seq_len)], ypred, "r-") plt.title('Prediction uncertainty') yplot = yte[-1, -seq_len - obs_len:] plt.plot(range(len(yplot)), yplot, "k-") plt.legend(["prediction", "true", "P10-P90 quantile"], loc="upper left") ymin, ymax = plt.ylim() plt.vlines(seq_len + obs_len - seq_len, ymin, ymax, color="blue", linestyles="dashed", linewidth=2) plt.ylim(ymin, ymax) plt.xlabel("Periods") plt.ylabel("Y") plt.show() return losses, mape_list
num_layers=1, likelihood="nb") optimizer = Adam(model.parameters(), lr=1e-3) random.seed(2) Xtr, ytr, Xte, yte = util.train_test_split(X, y) losses = [] cnt = 0 yscaler = None # 数据放缩预处理 # if args.standard_scaler: # yscaler = util.StandardScaler() # elif args.log_scaler: # yscaler = util.LogScaler() # elif args.mean_scaler: yscaler = util.MeanScaler() if yscaler is not None: ytr = yscaler.fit_transform(ytr) seq_len = 12 num_obs_to_train = 168 progress = ProgressBar() num_epoches = 100 step_per_epoch = 3 batch_size = 64 likelihood = "nb" sample_size = 100 # 蒙特卡洛模拟次数 show_plot = True for epoch in progress(range(num_epoches)): # print("Epoch {} starts...".format(epoch))