STGCN复现第三弹:解读math_graph.py

STGCN复现第三弹:解读math_graph.py_第1张图片
STGCN复现第三弹:解读math_graph.py_第2张图片

scaled_laplacian函数:

def scaled_laplacian(W):
    '''
    Normalized graph Laplacian function. 标准化拉普拉斯矩阵
    :param W: np.ndarray, [n_route, n_route], weighted adjacency matrix of G.
    :return: np.matrix, [n_route, n_route].
    '''
    # d ->  diagonal degree matrix 度矩阵
    n, d = np.shape(W)[0], np.sum(W, axis=1)      #1.取出矩阵W的维度 2.将矩阵W的列向量相加
    ’‘’
    举例:
    c = np.array([[0, 2, 1], [3, 5, 6], [0, 1, 1]])
	print c.sum()
	print c.sum(axis=0)
	print c.sum(axis=1)
	结果分别是:19, [3 8 8], [ 3 14  2]
    ‘’‘
    # L -> graph Laplacian
    L = -W
    L[np.diag_indices_from(L)] = d  #返回索引以访问n维数组L的主对角线
    for i in range(n):
        for j in range(n):
            if (d[i] > 0) and (d[j] > 0):
                L[i, j] = L[i, j] / np.sqrt(d[i] * d[j])
    # lambda_max \approx(大约) 2.0, the largest eigenvalues(特征值) of L.
    lambda_max = eigs(L, k=1, which='LR')[0][0].real  # 1. k:int, 可选参数所需的特征值和特征向量的数量。 2.‘LR’:largest real part 3. x.real取得xd的实部
     #eigs()全称为scipy.sparse.linalg.eigs()用于求平方矩阵A的k个特征值和特征向量。
    return np.mat(2 * L / lambda_max - np.identity(n)) 	#np.identity(n)生成一个n行n列的单位矩阵

np.identity()函数详解
scipy.sparse.linalg.eigs()函数详解~

STGCN复现第三弹:解读math_graph.py_第3张图片

cheb_poly_approx函数:

def cheb_poly_approx(L, Ks, n):
    '''
    Chebyshev(切比雪夫) polynomials(多项式) approximation(逼近、近似) function.
    :param L: np.matrix, [n_route, n_route], graph Laplacian(拉普拉斯算子).
    :param Ks: int, kernel size of spatial convolution(空间卷积核大小).
    :param n: int, number of routes / size of graph.(图的大小)
    :return: np.ndarray, [n_route, Ks*n_route]. 返回一个np.ndarray的多维数组
    '''
    L0, L1 = np.mat(np.identity(n)), np.mat(np.copy(L))  	#np.copy():返回给定对象的数组副本

    if Ks > 1:
        L_list = [np.copy(L0), np.copy(L1)]
        for i in range(Ks - 2):
            Ln = np.mat(2 * L * L1 - L0)
            L_list.append(np.copy(Ln))
            L0, L1 = np.matrix(np.copy(L1)), np.matrix(np.copy(Ln))
        # L_lsit [Ks, n*n], Lk [n, Ks*n]
        return np.concatenate(L_list, axis=-1)
    elif Ks == 1:
        return np.asarray(L0) 	#将输入转换为数组
    else:
        raise ValueError(f'ERROR: the size of spatial kernel must be greater than 1, but received "{Ks}".')

np.copy()函数详解
np.concatenate()函数详解
numpy.asarray()函数详解

你可能感兴趣的:(复现STGCN)