def training(epochs): global global_epoch lowest_val_loss = 1e6 for _ in range(epochs): t = time.time() info = [global_epoch] train_loader.reset() metric.reset() for idx, databatch in enumerate(train_loader): mod.forward_backward(databatch) mod.update_metric(metric, databatch.label) mod.update() metric_values = dict(zip(*metric.get())) print('training: Epoch: %s, RMSE: %.2f, MAE: %.2f, time: %.2f s' % (global_epoch, metric_values['rmse'], metric_values['mae'], time.time() - t), flush=True) info.append(metric_values['mae']) val_loader.reset() prediction = mod.predict(val_loader)[1].asnumpy() loss = masked_mae_np(val_y, prediction, 0) print('validation: Epoch: %s, loss: %.2f, time: %.2f s' % (global_epoch, loss, time.time() - t), flush=True) info.append(loss) if loss < lowest_val_loss: test_loader.reset() prediction = mod.predict(test_loader)[1].asnumpy() tmp_info = [] for idx in range(config['num_for_predict']): y, x = test_y[:, :idx + 1, :], prediction[:, :idx + 1, :] tmp_info.append((masked_mae_np(y, x, 0), masked_mape_np(y, x, 0), masked_mse_np(y, x, 0)**0.5)) mae, mape, rmse = tmp_info[-1] print('test: Epoch: {}, MAE: {:.2f}, MAPE: {:.2f}, RMSE: {:.2f}, ' 'time: {:.2f}s'.format(global_epoch, mae, mape, rmse, time.time() - t)) print(flush=True) info.extend((mae, mape, rmse)) info.append(tmp_info) all_info.append(info) lowest_val_loss = loss global_epoch += 1
def eval_metrics(y_true_np, y_predicted_np, standard_scaler): metrics = np.zeros(3) y_true_np = standard_scaler.inverse_transform(y_true_np) y_predicted_np = standard_scaler.inverse_transform(y_predicted_np) mae = utils.masked_mae_np(y_predicted_np, y_true_np, null_val=0.0) mape = utils.masked_mape_np(y_predicted_np, y_true_np, null_val=0.0) rmse = utils.masked_rmse_np(y_predicted_np, y_true_np, null_val=0.0) metrics[0] += mae metrics[1] += mape metrics[2] += rmse return metrics
""" y_preds = torch.FloatTensor([]).to(device) # [total_instances, horizon, nodes, dec_input_dim] y_truths = data['y_test'] y_truths = standard_scaler.inverse_transform(y_truths) predictions = [] groundtruth = list() with torch.no_grad(): for i, (x, y) in tqdm.tqdm(enumerate(test_data_loader.get_iterator()), total=num_test_iteration_per_epoch): x = torch.FloatTensor(x).to(device) y = torch.FloatTensor(y).to(device) outputs = model(x, y, 0) # (horizon, batch_size, num_nodes*output_dim) y_preds = torch.cat([y_preds, outputs], dim=1) # [horizon, total_instances, num_nodes*output_dim] --> [total_instances, horizon, num_nodes*output_dim] y_preds = torch.transpose(y_preds, 0, 1) y_preds = y_preds.detach().cpu().numpy() # cast to numpy array print("--------single-step testing results--------") for horizon_i in range(y_truths.shape[1]): y_truth = np.squeeze(y_truths[:, horizon_i, :, 0]) y_pred = standard_scaler.inverse_transform(y_preds[:, horizon_i, :]) predictions.append(y_pred) groundtruth.append(y_truth) mae = utils.masked_mae_np(y_pred[:y_truth.shape[0]], y_truth, null_val=0.0) mape = utils.masked_mape_np(y_pred[:y_truth.shape[0]], y_truth, null_val=0.0) rmse = utils.masked_rmse_np(y_pred[:y_truth.shape[0]], y_truth, null_val=0.0) print("Horizon {:02d}, MAE: {:.2f}, MAPE: {:.4f}, RMSE: {:.2f}".format( horizon_i + 1, mae, mape, rmse))
def training(epochs): global global_epoch lowest_val_loss = 1e6 for _ in range(epochs): t = time.time() info = [global_epoch] train_loader.reset() metric.reset() for idx, databatch in enumerate(train_loader): mod.forward_backward(databatch) mod.update_metric(metric, databatch.label) mod.update() metric_values = dict(zip(*metric.get())) print('training: Epoch: %s, RMSE: %.2f, MAE: %.2f, time: %.2f s' % (global_epoch, metric_values['rmse'], metric_values['mae'], time.time() - t), flush=True) info.append(metric_values['mae']) val_loader.reset() prediction = mod.predict(val_loader)[1].asnumpy() loss = masked_mae_np(val_y, prediction, 0) print('validation: Epoch: %s, loss: %.2f, time: %.2f s' % (global_epoch, loss, time.time() - t), flush=True) info.append(loss) if loss < lowest_val_loss: test_loader.reset() prediction = mod.predict(test_loader)[1].asnumpy() forcasting_2d = prediction[:, 0, :] forcasting_2d_target = test_y[:, 0, :] if not os.path.exists(debug_dir): os.makedirs(debug_dir) np.savetxt('{}/target.csv'.format(debug_dir), forcasting_2d_target, delimiter=",") np.savetxt('{}/predict.csv'.format(debug_dir), forcasting_2d, delimiter=",") np.savetxt('{}/predict_abs_error.csv'.format(debug_dir), np.abs(forcasting_2d - forcasting_2d_target), delimiter=",") np.savetxt('{}/predict_ape.csv'.format(debug_dir), np.abs((forcasting_2d - forcasting_2d_target) / forcasting_2d_target), delimiter=",") tmp_info = [] for idx in range(config['num_for_predict']): y, x = test_y[:, :idx + 1, :], prediction[:, :idx + 1, :] tmp_info.append((masked_mae_np(y, x, 0), masked_mape_np(y, x, 0), masked_mse_np(y, x, 0)**0.5)) mae, mape, rmse = tmp_info[-1] print('test: Epoch: {}, MAE: {:.2f}, MAPE: {:.2f}, RMSE: {:.2f}, ' 'time: {:.2f}s'.format(global_epoch, mae, mape, rmse, time.time() - t)) print(flush=True) info.extend((mae, mape, rmse)) info.append(tmp_info) all_info.append(info) lowest_val_loss = loss global_epoch += 1