Note_weight amatrix

Author

GUEBIN CHOI

Published

April 25, 2023

weight matrix

import numpy as np

- 하이퍼파라메터

T = 5 # time
N = 3 # number of nodes 

- wt,ws,f

wt = 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
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.]])
ws = np.array([[0,1,1],[1,0,0],[1,0,0]]) + np.eye(N)
ws
array([[1., 1., 1.],
       [1., 1., 0.],
       [1., 0., 1.]])
f = np.random.randn(T,N)
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_flatten = f.reshape(-1,1)
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):
  N = len(ws)
  T = len(wt)
  Is = np.eye(N,N)
  lst = [[0]*T for t in range(T)]
  for i in range(T):
    for j in range(T):
      if i==j: 
        lst[i][j] = ws 
      elif abs(i-j)==1:
        lst[i][j] = Is
      else:
        lst[i][j] = Is*0
  return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
W_flatten = flatten_weight(ws,wt)
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

ftrimed_flatten = trim(f_flatten,W_flatten)

임의로 ftrimed_flattenf_flatten과 같다고 생각하자.

ftrimed_flatten = f_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 = ftrimed_flatten.reshape(T,N) 
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]])
f # 와 비교.. 잘 reconstructiond 되어씀 
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
loader1 = ChickenpoxDatasetLoader()
a = loader1.get_dataset(lags=1)

time,number of nodes

T,N,_ = np.array(a.features).shape

- wt,ws,f

wt = np.zeros((T,T))
for i in range(T):
    for j in range(T):
        if i==j :
            wt[i,j] = 0
        elif np.abs(i-j) <= 1 : 
            wt[i,j] = 1
mtr = a.edge_index
mtr2 = a.edge_weight
ws = np.zeros((N,N))
for i in range(N):
    for j in range(mtr2.shape[0]):
        if mtr[0][j] == i :
            ws[i,mtr[1][j]] = mtr2[j]
np.array(ws).shape
(20, 20)
f = np.array(a.features).reshape(T,N)

- f를 펼침

f_flatten = f.reshape(-1,1)
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):
  N = len(ws)
  T = len(wt)
  Is = np.eye(N,N)
  lst = [[0]*T for t in range(T)]
  for i in range(T):
    for j in range(T):
      if i==j: 
        lst[i][j] = ws 
      elif abs(i-j)==1:
        lst[i][j] = Is
      else:
        lst[i][j] = Is*0
  return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
W_flatten = flatten_weight(ws,wt)
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.]])
np.save('./weight_st/W_chickenpox.npy', W_flatten)
np.load('./weight_st/W_chickenpox.npy')
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.]])
d = np.array(W_flatten.sum(axis=1))
D = np.diag(d)
L = np.array(np.diag(1/np.sqrt(d)) @ (D-W_flatten) @ np.diag(1/np.sqrt(d)))
lamb, Psi = np.linalg.eigh(L)
np.save('./weight_st/Psi_chickenpox.npy', Psi)
np.load('./weight_st/Psi_chickenpox.npy')
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

ftrimed_flatten = trim(f_flatten,W_flatten)

Pedalme

from torch_geometric_temporal.dataset import PedalMeDatasetLoader
loader2 = PedalMeDatasetLoader()
a = loader2.get_dataset(lags=1)

time,number of nodes

T,N,_ = np.array(a.features).shape

- wt,ws,f

wt = np.zeros((T,T))
for i in range(T):
    for j in range(T):
        if i==j :
            wt[i,j] = 0
        elif np.abs(i-j) <= 1 : 
            wt[i,j] = 1
mtr = a.edge_index
mtr2 = a.edge_weight
ws = np.zeros((N,N))
for i in range(N):
    for j in range(mtr2.shape[0]):
        if mtr[0][j] == i :
            ws[i,mtr[1][j]] = mtr2[j]
np.array(wt).shape
(34, 34)
np.array(ws).shape
(15, 15)
34*15
510
f = np.array(a.features).reshape(T,N)

- f를 펼침

f_flatten = f.reshape(-1,1)
# f_flatten

- 펼쳐진 f에 대응하는 W 생성

def flatten_weight(ws,wt):
  N = len(ws)
  T = len(wt)
  Is = np.eye(N,N)
  lst = [[0]*T for t in range(T)]
  for i in range(T):
    for j in range(T):
      if i==j: 
        lst[i][j] = ws 
      elif abs(i-j)==1:
        lst[i][j] = Is
      else:
        lst[i][j] = Is*0
  return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
W_flatten = flatten_weight(ws,wt)
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

ftrimed_flatten = trim(f_flatten,W_flatten)
np.save('./weight_st/W_pedalme.npy', W_flatten)
np.load('./weight_st/W_pedalme.npy')
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.        ]])
d = np.array(W_flatten.sum(axis=1))
D = np.diag(d)
L = np.array(np.diag(1/np.sqrt(d)) @ (D-W_flatten) @ np.diag(1/np.sqrt(d)))
lamb, Psi = np.linalg.eigh(L)
Psi.T.shape
(510, 510)
Psi.shape
(510, 510)
np.save('./weight_st/Psi_pedalme.npy', Psi)
np.load('./weight_st/Psi_pedalme.npy')
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
loader3 = WikiMathsDatasetLoader()
a = loader3.get_dataset(lags=1)

time,number of nodes

T,N,_ = np.array(a.features).shape

- wt,ws,f

wt = np.zeros((T,T))
for i in range(T):
    for j in range(T):
        if i==j :
            wt[i,j] = 0
        elif np.abs(i-j) <= 1 : 
            wt[i,j] = 1
mtr = a.edge_index
mtr2 = a.edge_weight
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]])
pd.DataFrame(mtr2).iloc[:,0].unique()
array([ 1,  4,  2,  5,  3,  6,  7,  9,  8, 12, 10, 13, 16, 11])
ws = np.zeros((N,N))
for i in range(N):
    for j in range(mtr2.shape[0]):
        if mtr[0][j] == i :
            ws[i,mtr[1][j]] = mtr2[j]
np.array(ws).shape
(1068, 1068)
f = np.array(a.features).reshape(T,N)

- f를 펼침

f_flatten = f.reshape(-1,1)
# f_flatten

- 펼쳐진 f에 대응하는 W 생성

N = len(ws)
T = len(wt)
Is = np.eye(N,N)
lst = [[0]*T for t in range(T)]
def flatten_weight(ws,wt):
  N = len(ws)
  T = len(wt)
  Is = np.eye(N,N)
  lst = [[0]*T for t in range(T)]
  for i in range(T):
    for j in range(T):
      if i==j: 
        lst[i][j] = ws 
      elif abs(i-j)==1:
        lst[i][j] = Is
      else:
        lst[i][j] = Is*0
  return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
W_flatten = flatten_weight(ws,wt)
W_flatten

- trim

ftrimed_flatten = trim(f_flatten,W_flatten)
np.save('./weight_st/W_wikimath.npy', W_flatten)
np.load('./weight_st/W_wikimath.npy')

Windmillsmall

from torch_geometric_temporal.dataset import WindmillOutputSmallDatasetLoader
loader6 = WindmillOutputSmallDatasetLoader()
a = loader6.get_dataset(lags=1)

time,number of nodes

T,N,_ = np.array(a.features).shape

- wt,ws,f

wt = np.zeros((T,T))
for i in range(T):
    for j in range(T):
        if i==j :
            wt[i,j] = 0
        elif np.abs(i-j) <= 1 : 
            wt[i,j] = 1
mtr = a.edge_index
mtr2 = a.edge_weight
ws = np.zeros((N,N))
for i in range(N):
    for j in range(mtr2.shape[0]):
        if mtr[0][j] == i :
            ws[i,mtr[1][j]] = mtr2[j]
np.array(ws).shape
(11, 11)
f = np.array(a.features).reshape(T,N)

- f를 펼침

f_flatten = f.reshape(-1,1)
# f_flatten

- 펼쳐진 f에 대응하는 W 생성

def flatten_weight(ws,wt):
  N = len(ws)
  T = len(wt)
  Is = np.eye(N,N)
  lst = [[0]*T for t in range(T)]
  for i in range(T):
    for j in range(T):
      if i==j: 
        lst[i][j] = ws 
      elif abs(i-j)==1:
        lst[i][j] = Is
      else:
        lst[i][j] = Is*0
  return np.concatenate([np.concatenate(l,axis=1) for l in lst],axis=0)
W_flatten = flatten_weight(ws,wt)
W_flatten

- trim

ftrimed_flatten = trim(f_flatten,W_flatten)
np.save('./weight_st/W_windmillsmall.npy', W_flatten)
np.load('./weight_st/W_windmillsmall.npy')