import torch


def t_normalize_adj(t_adj):
    """Symmetrically normalize adjacency matrix."""
    rowsum = t_adj.sum(1)  # D-degree matrix
    d_inv_sqrt = 1 / torch.sqrt(rowsum)
    d_inv_sqrt[torch.isinf(d_inv_sqrt)] = 0.0
    d_mat_inv_sqrt = torch.diag(d_inv_sqrt)
    # D^0.5 .A. D^0.5=A对称，D对角=(A. D^0.5).T.(D^0.5)
    return t_adj.mm(d_mat_inv_sqrt).t().mm(d_mat_inv_sqrt)