Function 'SymeigBackward' returned nan values in its 0th output

I use

        with autograd.detect_anomaly():

and error detected

Function 'SymeigBackward' returned nan values in its 0th output.

I use symeig function

# ReEig Layer
def cal_rect_cov(features):
    weight1, weight2 = variable_with_orth_weight_decay(features.shape)
    features = torch.bmm(torch.bmm(weight2, features), weight1)
    result=[]
    for i in range(features.shape[0]):
        s_f,v_f=torch.symeig(features[i], eigenvectors=True)
        s_f_clamp=torch.clamp(s_f,0.0001,10000)
        s_f_clamp2=torch.diag(s_f_clamp)
        sv_m=torch.matmul(v_f,s_f_clamp2)
        features_t=torch.matmul(sv_m,v_f.t())
        result.append(features_t)
    result = torch.stack(result)
    return result

# LogEig Layer
def cal_log_cov(features):
#     features=features.detach().cpu()
    result=[]
    for i in range(features.shape[0]):
        s_f,v_f=torch.symeig(features[i], eigenvectors=True)
        s_f_log = torch.log(s_f)
        s_f_log2=torch.diag(s_f_log)
        sv_m=torch.matmul(v_f,s_f_log2)
        features_t=torch.matmul(sv_m,v_f.t())
        result.append(features_t)
    result = torch.stack(result)
    return result

when get cnn feature, and make covariance matrix using torch.symeig

but this error detected.

plz help me how to get no nan values in ‘SymeigBackward’

Thank you
-James

I think the symeig method is very sensitive to the backpropogation. The better way to avoid the non gradient issue is to make your matrix more robust. It means that you need to check the matrix conditional number. And may add some jitters to the diagnal elements or change your model which can model the noises.