import itstgcn
import torch
ITSTGCN-Tutorial
ST-GCN
edit
import
예제1: vanilla STGCN
-
데이터
= itstgcn.load_data('./data/fivenodes.pkl') data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.utils.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
-
학습
= itstgcn.StgcnLearner(train_dataset,dataset_name='five_nodes') lrnr
/home/csy/Dropbox/blog/posts/GCN/itstgcn/learners.py:84: UserWarning: Creating a tensor from a list of numpy.ndarrays is extremely slow. Please consider converting the list to a single numpy.ndarray with numpy.array() before converting to a tensor. (Triggered internally at /opt/conda/conda-bld/pytorch_1639180588308/work/torch/csrc/utils/tensor_new.cpp:201.)
self.lags = torch.tensor(train_dataset.features).shape[-1]
=4,epoch=5) lrnr.learn(filters
5/5
-
적합값
#lrnr(train_dataset)
#lrnr(test_dataset)
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset,test_dataset) evtor
= evtor.plot('--.',h=5,max_node=3,label='complete data',alpha=0.5)
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제2: padding missing values
-
데이터
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.utils.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
-
임의로 결측치 발생
= itstgcn.rand_mindex(train_dataset,mrate=0.5)
mindex = itstgcn.miss(train_dataset,mindex=mindex,mtype='rand') train_dataset_miss
= itstgcn.plot(torch.tensor(train_dataset_miss.targets),'o')
fig fig
-
적절한 method로 결측치를 채움 (default 는 linear)
= itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
= itstgcn.plot(torch.tensor(train_dataset_miss.targets),'o')
fig '--x',color='C1',alpha=0.5) itstgcn.plot_add(fig,torch.tensor(train_dataset_padded.targets),
다른 method로 결측치를 채울수도 있음. 사용할 수 있는 방법들은 아래에 정리되어 있음
= itstgcn.padding(train_dataset_miss,interpolation_method='nearest') train_dataset_padded
= itstgcn.plot(torch.tensor(train_dataset_miss.targets),'o')
fig '--x',color='C1',alpha=0.5) itstgcn.plot_add(fig,torch.tensor(train_dataset_padded.targets),
= itstgcn.padding(train_dataset_miss,interpolation_method='quadratic') train_dataset_padded
= itstgcn.plot(torch.tensor(train_dataset_miss.targets),'o')
fig '--x',color='C1',alpha=0.5) itstgcn.plot_add(fig,torch.tensor(train_dataset_padded.targets),
= itstgcn.padding(train_dataset_miss,interpolation_method='cubic') train_dataset_padded
= itstgcn.utils.plot(torch.tensor(train_dataset_miss.targets),'o')
fig '--x',color='C1',alpha=0.5) itstgcn.utils.plot_add(fig,torch.tensor(train_dataset_padded.targets),
-
블락으로 결측치 발생
= [list(range(10,100)),[],list(range(50,80)),[],[]]
mindex = itstgcn.miss(train_dataset,mindex,mtype='block') train_dataset_miss
= itstgcn.utils.plot(torch.tensor(train_dataset_miss.targets),'o')
fig fig
예제3: vanilla STGCN with random missing
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.utils.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
= itstgcn.rand_mindex(train_dataset,mrate=0.5)
mindex = itstgcn.miss(train_dataset,mindex,mtype='rand')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.learners.StgcnLearner(train_dataset_padded) lrnr
=4,epoch=5) lrnr.learn(filters
5/5
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
= evtor.plot('--.',h=5,max_node=5,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제4: vanilla STGCN with block missing
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.utils.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
= [list(range(10,100)),[],list(range(50,80)),[],[]]
mindex = itstgcn.miss(train_dataset,mindex,mtype='block')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.StgcnLearner(train_dataset_padded) lrnr
=4,epoch=5) lrnr.learn(filters
5/5
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
= evtor.plot('--.',h=5,max_node=5,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제5: threshold example (random)
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.utils.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
-
결측치 발생 및 패딩
=itstgcn.rand_mindex(train_dataset,mrate=0.5)
mindex= itstgcn.miss(train_dataset,mindex,mtype='rand')
train_dataset_miss = itstgcn.padding(train_dataset_miss) train_dataset_padded
= itstgcn.convert_train_dataset(train_dataset_miss)
f_miss,_ = itstgcn.convert_train_dataset(train_dataset_padded) f_padded,_
= itstgcn.utils.plot(f_miss,'o')
fig '--x',alpha=0.5) itstgcn.utils.plot_add(fig,f_padded,
-
update by frequency thresholding
= itstgcn.plot(f_miss,'o',alpha=0.5)
fig '--x',alpha=0.5)
itstgcn.plot_add(fig,f_padded,= itstgcn.update_from_freq_domain(f_padded,train_dataset_padded.mindex)
f_updated '-') itstgcn.plot_add(fig,f_updated,
예제6: threshold example (block)
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
-
결측치 발생 및 패딩
=[list(range(10,100)),[],list(range(50,80)),[],[]]
mindex= itstgcn.miss(train_dataset,mindex,mtype='block')
train_dataset_miss = itstgcn.padding(train_dataset_miss) train_dataset_padded
= itstgcn.convert_train_dataset(train_dataset_miss)
f_miss,_ = itstgcn.convert_train_dataset(train_dataset_padded) f_padded,_
= itstgcn.plot(f_miss,'o')
fig '--x',alpha=0.5) itstgcn.plot_add(fig,f_padded,
-
update by frequency thresholding
= itstgcn.plot(f_miss,'o',alpha=0.5)
fig '--x',alpha=0.5)
itstgcn.plot_add(fig,f_padded,= itstgcn.update_from_freq_domain(f_padded,train_dataset_padded.mindex)
f_updated '-') itstgcn.plot_add(fig,f_updated,
예제7: iterative thresholded STGCN (IT-STGCN) with random missing
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
= itstgcn.rand_mindex(train_dataset,mrate=0.5)
mindex = itstgcn.miss(train_dataset,mindex,mtype='rand')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.ITStgcnLearner(train_dataset_padded) lrnr
=4,epoch=5) lrnr.learn(filters
5/5
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)['yhat'].shape
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
= evtor.plot('--.',h=5,max_node=3,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제8: iterative thresholded STGCN (IT-STGCN) with block missing
-
data
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
= [list(range(10,100)),[],list(range(50,80)),[],[]]
mindex = itstgcn.miss(train_dataset,mindex,mtype='block')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.ITStgcnLearner(train_dataset_padded) lrnr
=4,epoch=5) lrnr.learn(filters
5/5
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)['yhat'].shape
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
= evtor.plot('--.',h=5,max_node=3,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제9: GNAR (random missing)
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict) loader
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
=itstgcn.rand_mindex(train_dataset,mrate=0.5)
mindex= itstgcn.miss(train_dataset,mindex,mtype='rand')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.GNARLearner(train_dataset_padded) lrnr
lrnr.learn()
WARNING: diagonal entries present in original matrix, these will be removed
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
WARNING: diagonal entries present in original matrix, these will be removed
WARNING: diagonal entries present in original matrix, these will be removed
= evtor.plot('--.',h=5,max_node=3,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig
예제10: GNAR (block missing)
= itstgcn.load_data('./data/fivenodes.pkl')
_data = torch.tensor(_data['edges']).nonzero().tolist()
_edges = _data['f'].tolist()
_FX = {'node1':0, 'node2':1, 'node3':2, 'node4':3, 'node5':4}
_node_ids = {'edges':_edges, 'node_ids':_node_ids, 'FX':_FX} data_dict
= itstgcn.DatasetLoader(data_dict)
loader = loader.get_dataset(lags=2)
dataset = itstgcn.temporal_signal_split(dataset, train_ratio=0.8) train_dataset, test_dataset
=[list(range(10,100)),[],list(range(50,80)),[],[]]
mindex= itstgcn.miss(train_dataset,mindex,mtype='block')
train_dataset_miss = itstgcn.padding(train_dataset_miss) # padding(train_dataset_miss,method='linear'와 같음) train_dataset_padded
-
학습
= itstgcn.GNARLearner(train_dataset_padded) lrnr
lrnr.learn()
WARNING: diagonal entries present in original matrix, these will be removed
-
적합값
#lrnr(train_dataset_padded)
#lrnr(test_dataset)
- 실행하면 X,y,yhat 출력
-
모형 평가 및 시각화
= itstgcn.Evaluator(lrnr,train_dataset_padded,test_dataset) evtor
WARNING: diagonal entries present in original matrix, these will be removed
WARNING: diagonal entries present in original matrix, these will be removed
= evtor.plot('--.',h=5,max_node=3,label='complete data',alpha=0.5) # max_nodes 는 1보다 커야함
fig 12)
fig.set_figwidth(
fig.tight_layout() fig