import numpy as np
Note_weight amatrix
weight matrix
-
하이퍼파라메터
= 5 # time
T = 3 # number of nodes N
-
wt,ws,f
= np.array([abs(i-j) < 2 for i in range(T) for j in range(T)]).reshape(T,T)*1.0 - np.eye(T)
wt wt
array([[0., 1., 0., 0., 0.],
[1., 0., 1., 0., 0.],
[0., 1., 0., 1., 0.],
[0., 0., 1., 0., 1.],
[0., 0., 0., 1., 0.]])
= np.array([[0,1,1],[1,0,0],[1,0,0]]) + np.eye(N)
ws ws
array([[1., 1., 1.],
[1., 1., 0.],
[1., 0., 1.]])
= np.random.randn(T,N)
f f
array([[-0.73176413, -0.95155223, -0.00206147],
[ 1.39680239, 0.84786031, 0.0152849 ],
[ 0.06111198, -1.37704116, -0.28557273],
[-0.88306776, -0.65056859, 0.05926914],
[ 0.3802191 , -0.76405896, 0.90468637]])
-
f를 펼침
= f.reshape(-1,1)
f_flatten f_flatten
array([[-0.73176413],
[-0.95155223],
[-0.00206147],
[ 1.39680239],
[ 0.84786031],
[ 0.0152849 ],
[ 0.06111198],
[-1.37704116],
[-0.28557273],
[-0.88306776],
[-0.65056859],
[ 0.05926914],
[ 0.3802191 ],
[-0.76405896],
[ 0.90468637]])
-
펼쳐진 f에 대응하는 W 생성
def flatten_weight(ws,wt):
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)]
lst for i in range(T):
for j in range(T):
if i==j:
= ws
lst[i][j] elif abs(i-j)==1:
= Is
lst[i][j] else:
= Is*0
lst[i][j] return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
= flatten_weight(ws,wt)
W_flatten W_flatten
array([[1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0., 0., 0.],
[1., 0., 0., 1., 1., 1., 1., 0., 0., 0., 0., 0., 0., 0., 0.],
[0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0., 0.],
[0., 0., 1., 1., 0., 1., 0., 0., 1., 0., 0., 0., 0., 0., 0.],
[0., 0., 0., 1., 0., 0., 1., 1., 1., 1., 0., 0., 0., 0., 0.],
[0., 0., 0., 0., 1., 0., 1., 1., 0., 0., 1., 0., 0., 0., 0.],
[0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 0., 1., 0., 0., 0.],
[0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 1., 1., 1., 0., 0.],
[0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0., 0., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1., 0., 0., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 0., 1., 1., 1.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 0., 1., 1., 0.],
[0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 0., 1., 1., 0., 1.]])
-
trim
= trim(f_flatten,W_flatten) ftrimed_flatten
임의로 ftrimed_flatten
이 f_flatten
과 같다고 생각하자.
= f_flatten
ftrimed_flatten ftrimed_flatten
array([[-0.73176413],
[-0.95155223],
[-0.00206147],
[ 1.39680239],
[ 0.84786031],
[ 0.0152849 ],
[ 0.06111198],
[-1.37704116],
[-0.28557273],
[-0.88306776],
[-0.65056859],
[ 0.05926914],
[ 0.3802191 ],
[-0.76405896],
[ 0.90468637]])
-
ftrimed
= ftrimed_flatten.reshape(T,N)
ftrimed ftrimed
array([[-0.73176413, -0.95155223, -0.00206147],
[ 1.39680239, 0.84786031, 0.0152849 ],
[ 0.06111198, -1.37704116, -0.28557273],
[-0.88306776, -0.65056859, 0.05926914],
[ 0.3802191 , -0.76405896, 0.90468637]])
# 와 비교.. 잘 reconstructiond 되어씀 f
array([[-0.73176413, -0.95155223, -0.00206147],
[ 1.39680239, 0.84786031, 0.0152849 ],
[ 0.06111198, -1.37704116, -0.28557273],
[-0.88306776, -0.65056859, 0.05926914],
[ 0.3802191 , -0.76405896, 0.90468637]])
Chickenpox
from torch_geometric_temporal.dataset import ChickenpoxDatasetLoader
= ChickenpoxDatasetLoader() loader1
= loader1.get_dataset(lags=1) a
time,number of nodes
= np.array(a.features).shape T,N,_
-
wt,ws,f
= np.zeros((T,T))
wt for i in range(T):
for j in range(T):
if i==j :
= 0
wt[i,j] elif np.abs(i-j) <= 1 :
= 1 wt[i,j]
= a.edge_index
mtr = a.edge_weight mtr2
= np.zeros((N,N))
ws for i in range(N):
for j in range(mtr2.shape[0]):
if mtr[0][j] == i :
1][j]] = mtr2[j] ws[i,mtr[
np.array(ws).shape
(20, 20)
= np.array(a.features).reshape(T,N) f
-
f를 펼침
= f.reshape(-1,1)
f_flatten f_flatten
array([[-1.08135724e-03],
[-7.11136085e-01],
[-3.22808515e+00],
...,
[ 4.71099041e-02],
[ 2.45684924e+00],
[-3.44296107e-01]])
-
펼쳐진 f에 대응하는 W 생성
def flatten_weight(ws,wt):
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)]
lst for i in range(T):
for j in range(T):
if i==j:
= ws
lst[i][j] elif abs(i-j)==1:
= Is
lst[i][j] else:
= Is*0
lst[i][j] return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
= flatten_weight(ws,wt)
W_flatten W_flatten
array([[1., 1., 0., ..., 0., 0., 0.],
[1., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 1., 1.],
[0., 0., 0., ..., 1., 1., 1.],
[0., 0., 0., ..., 1., 1., 1.]])
'./weight_st/W_chickenpox.npy', W_flatten) np.save(
'./weight_st/W_chickenpox.npy') np.load(
array([[1., 1., 0., ..., 0., 0., 0.],
[1., 1., 0., ..., 0., 0., 0.],
[0., 0., 1., ..., 0., 0., 0.],
...,
[0., 0., 0., ..., 1., 1., 1.],
[0., 0., 0., ..., 1., 1., 1.],
[0., 0., 0., ..., 1., 1., 1.]])
= np.array(W_flatten.sum(axis=1)) d
= np.diag(d) D
= np.array(np.diag(1/np.sqrt(d)) @ (D-W_flatten) @ np.diag(1/np.sqrt(d))) L
= np.linalg.eigh(L) lamb, Psi
'./weight_st/Psi_chickenpox.npy', Psi) np.save(
'./weight_st/Psi_chickenpox.npy') np.load(
array([[ 1.04115841e-02, -1.47242159e-02, 1.47242532e-02, ...,
6.41186713e-04, -4.27546925e-04, 2.13800213e-04],
[ 8.23107997e-03, -1.16404883e-02, 1.16404382e-02, ...,
-4.77715858e-04, 3.18549731e-04, -1.59296626e-04],
[ 8.23107997e-03, -1.16404502e-02, 1.16402861e-02, ...,
2.85275946e-04, -1.90235274e-04, 9.51330388e-05],
...,
[ 8.23107997e-03, 1.16404565e-02, 1.16403112e-02, ...,
-6.65738542e-04, -4.43946869e-04, -2.22009806e-04],
[ 1.04115841e-02, 1.47242028e-02, 1.47242009e-02, ...,
1.35543431e-04, 9.03781585e-05, 4.51938438e-05],
[ 8.23107997e-03, 1.16404680e-02, 1.16403570e-02, ...,
5.65995233e-04, 3.77431091e-04, 1.88745843e-04]])
-
trim
= trim(f_flatten,W_flatten) ftrimed_flatten
Pedalme
from torch_geometric_temporal.dataset import PedalMeDatasetLoader
= PedalMeDatasetLoader() loader2
= loader2.get_dataset(lags=1) a
time,number of nodes
= np.array(a.features).shape T,N,_
-
wt,ws,f
= np.zeros((T,T))
wt for i in range(T):
for j in range(T):
if i==j :
= 0
wt[i,j] elif np.abs(i-j) <= 1 :
= 1 wt[i,j]
= a.edge_index
mtr = a.edge_weight mtr2
= np.zeros((N,N))
ws for i in range(N):
for j in range(mtr2.shape[0]):
if mtr[0][j] == i :
1][j]] = mtr2[j] ws[i,mtr[
np.array(wt).shape
(34, 34)
np.array(ws).shape
(15, 15)
34*15
510
= np.array(a.features).reshape(T,N) f
-
f를 펼침
= f.reshape(-1,1)
f_flatten # f_flatten
-
펼쳐진 f에 대응하는 W 생성
def flatten_weight(ws,wt):
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)]
lst for i in range(T):
for j in range(T):
if i==j:
= ws
lst[i][j] elif abs(i-j)==1:
= Is
lst[i][j] else:
= Is*0
lst[i][j] return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
= flatten_weight(ws,wt)
W_flatten W_flatten
array([[1. , 0.42545896, 0.15735536, ..., 0. , 0. ,
0. ],
[0.42545896, 1. , 0.06751402, ..., 0. , 0. ,
0. ],
[0.15735536, 0.06751402, 1. , ..., 0. , 0. ,
0. ],
...,
[0. , 0. , 0. , ..., 1. , 0.07069877,
0.06899971],
[0. , 0. , 0. , ..., 0.07069877, 1. ,
0.32983841],
[0. , 0. , 0. , ..., 0.06899971, 0.32983841,
1. ]])
-
trim
= trim(f_flatten,W_flatten) ftrimed_flatten
'./weight_st/W_pedalme.npy', W_flatten) np.save(
'./weight_st/W_pedalme.npy') np.load(
array([[1. , 0.42545896, 0.15735536, ..., 0. , 0. ,
0. ],
[0.42545896, 1. , 0.06751402, ..., 0. , 0. ,
0. ],
[0.15735536, 0.06751402, 1. , ..., 0. , 0. ,
0. ],
...,
[0. , 0. , 0. , ..., 1. , 0.07069877,
0.06899971],
[0. , 0. , 0. , ..., 0.07069877, 1. ,
0.32983841],
[0. , 0. , 0. , ..., 0.06899971, 0.32983841,
1. ]])
= np.array(W_flatten.sum(axis=1)) d
= np.diag(d) D
= np.array(np.diag(1/np.sqrt(d)) @ (D-W_flatten) @ np.diag(1/np.sqrt(d))) L
= np.linalg.eigh(L) lamb, Psi
Psi.T.shape
(510, 510)
Psi.shape
(510, 510)
'./weight_st/Psi_pedalme.npy', Psi) np.save(
'./weight_st/Psi_pedalme.npy') np.load(
array([[ 4.61219573e-02, -6.52404719e-02, 6.52791904e-02, ...,
-4.33862149e-04, -2.17206920e-04, 8.97548015e-05],
[ 4.44297451e-02, -6.28451968e-02, 6.28773329e-02, ...,
-2.56832039e-05, -1.49624707e-05, 7.00873447e-06],
[ 3.51291880e-02, -4.95907787e-02, 4.93238061e-02, ...,
-2.71803598e-02, -1.77647577e-02, 8.79413561e-03],
...,
[ 3.75563137e-02, 5.30576275e-02, 5.28917644e-02, ...,
5.85830960e-03, -4.16888716e-03, -2.15565030e-03],
[ 3.87057680e-02, 5.47153694e-02, 5.46437177e-02, ...,
1.08555123e-03, -6.10976014e-04, -2.76608545e-04],
[ 4.03127107e-02, 5.70025038e-02, 5.69740769e-02, ...,
-2.05662084e-04, 9.91534370e-05, 4.05213281e-05]])
Wikimath
from torch_geometric_temporal.dataset import WikiMathsDatasetLoader
= WikiMathsDatasetLoader() loader3
= loader3.get_dataset(lags=1) a
time,number of nodes
= np.array(a.features).shape T,N,_
-
wt,ws,f
= np.zeros((T,T))
wt for i in range(T):
for j in range(T):
if i==j :
= 0
wt[i,j] elif np.abs(i-j) <= 1 :
= 1 wt[i,j]
= a.edge_index
mtr = a.edge_weight mtr2
np.array(mtr).shape
(2, 27079)
np.array(mtr2).shape
(27079,)
mtr
array([[ 0, 0, 0, ..., 1056, 1063, 1065],
[ 1, 2, 3, ..., 1059, 1064, 1066]])
0].unique() pd.DataFrame(mtr2).iloc[:,
array([ 1, 4, 2, 5, 3, 6, 7, 9, 8, 12, 10, 13, 16, 11])
= np.zeros((N,N))
ws for i in range(N):
for j in range(mtr2.shape[0]):
if mtr[0][j] == i :
1][j]] = mtr2[j] ws[i,mtr[
np.array(ws).shape
(1068, 1068)
= np.array(a.features).reshape(T,N) f
-
f를 펼침
= f.reshape(-1,1)
f_flatten # f_flatten
-
펼쳐진 f에 대응하는 W 생성
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)] lst
def flatten_weight(ws,wt):
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)]
lst for i in range(T):
for j in range(T):
if i==j:
= ws
lst[i][j] elif abs(i-j)==1:
= Is
lst[i][j] else:
= Is*0
lst[i][j] return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
= flatten_weight(ws,wt)
W_flatten W_flatten
-
trim
= trim(f_flatten,W_flatten) ftrimed_flatten
'./weight_st/W_wikimath.npy', W_flatten) np.save(
'./weight_st/W_wikimath.npy') np.load(
Windmillsmall
from torch_geometric_temporal.dataset import WindmillOutputSmallDatasetLoader
= WindmillOutputSmallDatasetLoader() loader6
= loader6.get_dataset(lags=1) a
time,number of nodes
= np.array(a.features).shape T,N,_
-
wt,ws,f
= np.zeros((T,T))
wt for i in range(T):
for j in range(T):
if i==j :
= 0
wt[i,j] elif np.abs(i-j) <= 1 :
= 1 wt[i,j]
= a.edge_index
mtr = a.edge_weight mtr2
= np.zeros((N,N))
ws for i in range(N):
for j in range(mtr2.shape[0]):
if mtr[0][j] == i :
1][j]] = mtr2[j] ws[i,mtr[
np.array(ws).shape
(11, 11)
= np.array(a.features).reshape(T,N) f
-
f를 펼침
= f.reshape(-1,1)
f_flatten # f_flatten
-
펼쳐진 f에 대응하는 W 생성
def flatten_weight(ws,wt):
= len(ws)
N = len(wt)
T = np.eye(N,N)
Is = [[0]*T for t in range(T)]
lst for i in range(T):
for j in range(T):
if i==j:
= ws
lst[i][j] elif abs(i-j)==1:
= Is
lst[i][j] else:
= Is*0
lst[i][j] return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
= flatten_weight(ws,wt)
W_flatten W_flatten
-
trim
= trim(f_flatten,W_flatten) ftrimed_flatten
'./weight_st/W_windmillsmall.npy', W_flatten) np.save(
'./weight_st/W_windmillsmall.npy') np.load(