示例#1
0
    def get_weights(self, ctx):
        weights = nd.dot(
            self.w_p.data(ctx),
            nd.dot(
                (self.w_l.data(ctx) * self.l_mask.data(ctx) +
                 self.l_eye.data(ctx)),
                ((self.w_u.data(ctx) * self.u_mask.data(ctx)) +
                 nd.diag(self.s_sign.data(ctx) * nd.exp(self.w_s.data(ctx))))))

        weights = weights.reshape(0, 0, 1, 1)  # expand dims

        return weights
示例#2
0
    def forward(self, adj, feat, lambda_max=None):
        r"""

        Description
        -----------
        Compute (Dense) Chebyshev Spectral Graph Convolution layer.

        Parameters
        ----------
        adj : mxnet.NDArray
            The adjacency matrix of the graph to apply Graph Convolution on,
            should be of shape :math:`(N, N)`, where a row represents the destination
            and a column represents the source.
        feat : mxnet.NDArray
            The input feature of shape :math:`(N, D_{in})` where :math:`D_{in}`
            is size of input feature, :math:`N` is the number of nodes.
        lambda_max : float or None, optional
            A float value indicates the largest eigenvalue of given graph.
            Default: None.

        Returns
        -------
        mxnet.NDArray
            The output feature of shape :math:`(N, D_{out})` where :math:`D_{out}`
            is size of output feature.
        """
        A = adj.astype(feat.dtype).as_in_context(feat.context)
        num_nodes = A.shape[0]

        in_degree = 1. / nd.clip(A.sum(axis=1), 1, float('inf')).sqrt()
        D_invsqrt = nd.diag(in_degree)
        I = nd.eye(num_nodes, ctx=A.context)
        L = I - nd.dot(D_invsqrt, nd.dot(A, D_invsqrt))

        if lambda_max is None:
            # NOTE(zihao): this only works for directed graph.
            lambda_max = (nd.linalg.syevd(L)[1]).max()

        L_hat = 2 * L / lambda_max - I
        Z = [nd.eye(num_nodes, ctx=A.context)]
        Zh = self.fc[0](feat)
        for i in range(1, self._k):
            if i == 1:
                Z.append(L_hat)
            else:
                Z.append(2 * nd.dot(L_hat, Z[-1]) - Z[-2])
            Zh = Zh + nd.dot(Z[i], self.fc[i](feat))

        if self.bias is not None:
            Zh = Zh + self.bias.data(feat.context)
        return Zh
示例#3
0
def cos_mat(vecs):
    ##  dot product divided by the norms
    xtx = nd.dot(vecs, vecs.T)
    nmx = nd.sqrt(nd.diag(xtx)).reshape((-1, 1))
    cnm = nd.dot(nmx, nmx.T)
    return xtx / cnm