Autograd为梯度返回零

时间:2019-09-08 21:49:26

标签: autograd

嗨,我正在尝试以放射状图的形式显示时间序列。为了计算theta的值,我试图使两个信号之间的角度差与1-cor(sig1,sig2)成比例,其中cor代表信号的相关性。因此,我实现了极距函数,如下所示:

import autograd.numpy as anp
from  autograd import grad

def polar_distance(theta_array):
  length = theta_array.shape[0]
  A = anp.tile(theta_array,(length,1))
  mat = A.T-A
  return 2*(1-anp.cos(mat))

该函数返回以角度theta_array排列在单位圆上的点的距离矩阵。 现在,为了计算最佳角度,我实现了以下功能:

def optim_order(sigmat):
    sig = anp.array(sigmat) #converting to autograd.numpy array
    corc = 1.0-anp.corrcoef(sig) # 1 - correlation
    def loss(thetas):
        difs = polar_distance(thetas)-corc
        mults = anp.multiply(difs,difs) 
        sums = anp.sum(mults)
        out = anp.sqrt(sums)
        return out
    loss_gradient_fun = grad(loss)
    thetas = anp.zeros(len(corc)) # initializing thetas (I've tried random too)
    print("Initial loss:", loss(thetas))
    for i in range(100):
        thetas -= loss_gradient_fun(thetas) * 0.01
        print("Trained loss:", loss(thetas))
    # do something with thetas

由于某些原因,loss_gradient_fun(thetas)返回0。我检查了代码的其他部分,它们均按预期工作。知道为什么会这样吗?

0 个答案:

没有答案
相关问题