更改

第258行: 第258行:     
python代码实现:<syntaxhighlight lang="python3">
 
python代码实现:<syntaxhighlight lang="python3">
 +
import torch
 +
 +
def Entropia(a):
 +
    b = torch.nonzero(a > 0).squeeze(1)
 +
    return -torch.sum(a[b]*torch.log2(a[b]))
 +
 +
 +
def effective_information(G1):
 +
    """
 +
    Calculate EI value of transition probability matrix
 +
    G1: TPM matrix
 +
    return the EI value
 +
    """
 +
    N = G1.shape[0]
 +
    G = torch.zeros_like(G1)
 +
    nozero_number = 0
 +
    for i in range(N):
 +
        if torch.sum(G1[i]) != 0:
 +
            G[i] = G1[i] / torch.sum(G1[i])
 +
            nozero_number += 1
 +
    Wout = torch.zeros(N)
 +
    Win = torch.zeros(N)
 +
 +
    for i in range(N):
 +
        if torch.sum(G[i]) != 0:
 +
            Wout[i] = torch.tensor(Entropia(G[i]))
 +
 +
    for i in range(N):
 +
        for j in range(N):
 +
            if G[i][j] != 0:
 +
                Win[j] += G[i][j] / nozero_number
 +
    Wout_average = torch.sum(Wout) / nozero_number
 +
    Win_entropy = Entropia(Win)
 +
    return Win_entropy - Wout_average
 +
 +
 
def calc_tpm(micro_tpm, group):
 
def calc_tpm(micro_tpm, group):
 
     """
 
     """
5,443

个编辑