dmlc--dgl
44089c8b4d
* Merge * [Graph][CUDA] Graph on GPU and many refactoring (#1791) * change edge_ids behavior and C++ impl * fix unittests; remove utils.Index in edge_id * pass mx and th tests * pass tf test * add aten::Scatter_ * Add nonzero; impl CSRGetDataAndIndices/CSRSliceMatrix * CSRGetData and CSRGetDataAndIndices passed tests * CSRSliceMatrix basic tests * fix bug in empty slice * CUDA CSRHasDuplicate * has_node; has_edge_between * predecessors, successors * deprecate send/recv; fix send_and_recv * deprecate send/recv; fix send_and_recv * in_edges; out_edges; all_edges; apply_edges * in deg/out deg * subgraph/edge_subgraph * adj * in_subgraph/out_subgraph * sample neighbors * set/get_n/e_repr * wip: working on refactoring all idtypes * pass ndata/edata tests on gpu * fix * stash * workaround nonzero issue * stash * nx conversion * test_hetero_basics except update routines * test_update_routines * test_hetero_basics for pytorch * more fixes * WIP: flatten graph * wip: flatten * test_flatten * test_to_device * fix bug in to_homo * fix bug in CSRSliceMatrix * pass subgraph test * fix send_and_recv * fix filter * test_heterograph * passed all pytorch tests * fix mx unittest * fix pytorch test_nn * fix all unittests for PyTorch * passed all mxnet tests * lint * fix tf nn test * pass all tf tests * lint * lint * change deprecation * try fix compile * lint * update METIDS * fix utest * fix * fix utests * try debug * revert * small fix * fix utests * upd * upd * upd * fix * upd * upd * upd * upd * upd * trigger * +1s * [kernel] Use heterograph index instead of unitgraph index (#1813) * upd * upd * upd * fix * upd * upd * upd * upd * upd * trigger * +1s * [Graph] Mutation for Heterograph (#1818) * mutation add_nodes and add_edges * Add support for remove_edges, remove_nodes, add_selfloop, remove_selfloop * Fix Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> * upd * upd * upd * fix * [Transfom] Mutable transform (#1833) * add nodesy * All three * Fix * lint * Add some test case * Fix * Fix * Fix * Fix * Fix * Fix * fix * triger * Fix * fix Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> * [Graph] Migrate Batch & Readout module to heterograph (#1836) * dgl.batch * unbatch * fix to device * reduce readout; segment reduce * change batch_num_nodes|edges to function * reduce readout/ softmax * broadcast * topk * fix * fix tf and mx * fix some ci * fix batch but unbatch differently * new checkk * upd * upd * upd * idtype behavior; code reorg * idtype behavior; code reorg * wip: test_basics * pass test_basics * WIP: from nx/ to nx * missing files * upd * pass test_basics:test_nx_conversion * Fix test * Fix inplace update * WIP: fixing tests * upd * pass test_transform cpu * pass gpu test_transform * pass test_batched_graph * GPU graph auto cast to int32 * missing file * stash * WIP: rgcn-hetero * Fix two datasety * upd * weird * Fix capsuley * fuck you * fuck matthias * Fix dgmg * fix bug in block degrees; pass rgcn-hetero * rgcn * gat and diffpool fix also fix ppi and tu dataset * Tree LSTM * pointcloud * rrn; wip: sgc * resolve conflicts * upd * sgc and reddit dataset * upd * Fix deepwalk, gindt and gcn * fix datasets and sign * optimization * optimization * upd * upd * Fix GIN * fix bug in add_nodes add_edges; tagcn * adaptive sampling and gcmc * upd * upd * fix geometric * fix * metapath2vec * fix agnn * fix pickling problem of block * fix utests * miss file * linegraph * upd * upd * upd * graphsage * stgcn_wave * fix hgt * on unittests * Fix transformer * Fix HAN * passed pytorch unittests * lint * fix * Fix cluster gcn * cluster-gcn is ready * on fixing block related codes * 2nd order derivative * Revert "2nd order derivative" This reverts commit 523bf6c249bee61b51b1ad1babf42aad4167f206. * passed torch utests again * fix all mxnet unittests * delete some useless tests * pass all tf cpu tests * disable * disable distributed unittest * fix * fix * lint * fix * fix * fix script * fix tutorial * fix apply edges bug * fix 2 basics * fix tutorial Co-authored-by: yzh119 <expye@outlook.com> Co-authored-by: xiang song(charlie.song) <classicxsong@gmail.com> Co-authored-by: Ubuntu <ubuntu@ip-172-31-51-214.ec2.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-7-42.us-west-2.compute.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-1-5.us-west-2.compute.internal> Co-authored-by: Ubuntu <ubuntu@ip-172-31-68-185.ec2.internal>
126 行
4.6 KiB
Python
126 行
4.6 KiB
Python
import dgl
|
|
import random
|
|
import torch
|
|
import numpy as np
|
|
import pandas as pd
|
|
from sklearn.preprocessing import StandardScaler
|
|
from load_data import *
|
|
from utils import *
|
|
from model import *
|
|
from sensors2graph import *
|
|
import torch.nn as nn
|
|
import argparse
|
|
import scipy.sparse as sp
|
|
|
|
parser = argparse.ArgumentParser(description='STGCN_WAVE')
|
|
parser.add_argument('--lr', default=0.001, type=float, help='learning rate')
|
|
parser.add_argument('--disablecuda', action='store_true', help='Disable CUDA')
|
|
parser.add_argument('--batch_size', type=int, default=50, help='batch size for training and validation (default: 50)')
|
|
parser.add_argument('--epochs', type=int, default=50, help='epochs for training (default: 50)')
|
|
parser.add_argument('--num_layers', type=int, default=9, help='number of layers')
|
|
parser.add_argument('--window', type=int, default=144, help='window length')
|
|
parser.add_argument('--sensorsfilepath', type=str, default='./data/sensor_graph/graph_sensor_ids.txt', help='sensors file path')
|
|
parser.add_argument('--disfilepath', type=str, default='./data/sensor_graph/distances_la_2012.csv', help='distance file path')
|
|
parser.add_argument('--tsfilepath', type=str, default='./data/metr-la.h5', help='ts file path')
|
|
parser.add_argument('--savemodelpath', type=str, default='stgcnwavemodel.pt', help='save model path')
|
|
parser.add_argument('--pred_len', type=int, default=5, help='how many steps away we want to predict')
|
|
parser.add_argument('--control_str', type=str, default='TNTSTNTST', help='model strcture controller, T: Temporal Layer, S: Spatio Layer, N: Norm Layer')
|
|
parser.add_argument('--channels', type=int, nargs='+', default=[1, 16, 32, 64, 32, 128], help='model strcture controller, T: Temporal Layer, S: Spatio Layer, N: Norm Layer')
|
|
args = parser.parse_args()
|
|
|
|
device = torch.device("cuda") if torch.cuda.is_available() and not args.disablecuda else torch.device("cpu")
|
|
|
|
with open(args.sensorsfilepath) as f:
|
|
sensor_ids = f.read().strip().split(',')
|
|
|
|
distance_df = pd.read_csv(args.disfilepath, dtype={'from': 'str', 'to': 'str'})
|
|
|
|
adj_mx = get_adjacency_matrix(distance_df, sensor_ids)
|
|
sp_mx = sp.coo_matrix(adj_mx)
|
|
G = dgl.from_scipy(sp_mx)
|
|
|
|
|
|
df = pd.read_hdf(args.tsfilepath)
|
|
num_samples, num_nodes = df.shape
|
|
|
|
tsdata = df.to_numpy()
|
|
|
|
|
|
n_his = args.window
|
|
|
|
save_path = args.savemodelpath
|
|
|
|
|
|
|
|
n_pred = args.pred_len
|
|
n_route = num_nodes
|
|
blocks = args.channels
|
|
# blocks = [1, 16, 32, 64, 32, 128]
|
|
drop_prob = 0
|
|
num_layers = args.num_layers
|
|
|
|
batch_size = args.batch_size
|
|
epochs = args.epochs
|
|
lr = args.lr
|
|
|
|
|
|
W = adj_mx
|
|
len_val = round(num_samples * 0.1)
|
|
len_train = round(num_samples * 0.7)
|
|
train = df[: len_train]
|
|
val = df[len_train: len_train + len_val]
|
|
test = df[len_train + len_val:]
|
|
|
|
scaler = StandardScaler()
|
|
train = scaler.fit_transform(train)
|
|
val = scaler.transform(val)
|
|
test = scaler.transform(test)
|
|
|
|
|
|
x_train, y_train = data_transform(train, n_his, n_pred, device)
|
|
x_val, y_val = data_transform(val, n_his, n_pred, device)
|
|
x_test, y_test = data_transform(test, n_his, n_pred, device)
|
|
|
|
train_data = torch.utils.data.TensorDataset(x_train, y_train)
|
|
train_iter = torch.utils.data.DataLoader(train_data, batch_size, shuffle=True)
|
|
val_data = torch.utils.data.TensorDataset(x_val, y_val)
|
|
val_iter = torch.utils.data.DataLoader(val_data, batch_size)
|
|
test_data = torch.utils.data.TensorDataset(x_test, y_test)
|
|
test_iter = torch.utils.data.DataLoader(test_data, batch_size)
|
|
|
|
|
|
loss = nn.MSELoss()
|
|
G = G.to(device)
|
|
model = STGCN_WAVE(blocks, n_his, n_route, G, drop_prob, num_layers, args.control_str).to(device)
|
|
optimizer = torch.optim.RMSprop(model.parameters(), lr=lr)
|
|
|
|
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.7)
|
|
|
|
min_val_loss = np.inf
|
|
for epoch in range(1, epochs + 1):
|
|
l_sum, n = 0.0, 0
|
|
model.train()
|
|
for x, y in train_iter:
|
|
y_pred = model(x).view(len(x), -1)
|
|
l = loss(y_pred, y)
|
|
optimizer.zero_grad()
|
|
l.backward()
|
|
optimizer.step()
|
|
l_sum += l.item() * y.shape[0]
|
|
n += y.shape[0]
|
|
scheduler.step()
|
|
val_loss = evaluate_model(model, loss, val_iter)
|
|
if val_loss < min_val_loss:
|
|
min_val_loss = val_loss
|
|
torch.save(model.state_dict(), save_path)
|
|
print("epoch", epoch, ", train loss:", l_sum / n, ", validation loss:", val_loss)
|
|
|
|
|
|
best_model = STGCN_WAVE(blocks, n_his, n_route, G, drop_prob, num_layers).to(device)
|
|
best_model.load_state_dict(torch.load(save_path))
|
|
|
|
|
|
l = evaluate_model(best_model, loss, test_iter)
|
|
MAE, MAPE, RMSE = evaluate_metric(best_model, test_iter, scaler)
|
|
print("test loss:", l, "\nMAE:", MAE, ", MAPE:", MAPE, ", RMSE:", RMSE)
|