def update(self, x0, lnprob, n_iter = 1000, stepsize = 1e-3, bandwidth = -1, alpha = 0.9, debug = False):
# Check input
if x0 is None or lnprob is None:
raise ValueError('x0 or lnprob cannot be None!')
theta = np.copy(x0)
# adagrad with momentum
fudge_factor = 1e-6
historical_grad = 0
for iter in range(n_iter):
if debug and (iter+1) % 1000 == 0:
print 'iter ' + str(iter+1)
lnpgrad = lnprob(theta)
# calculating the kernel matrix
kxy, dxkxy = self.svgd_kernel(theta, h = -1)
grad_theta = (np.matmul(kxy, lnpgrad) + dxkxy) / x0.shape[0]
# adagrad
if iter == 0:
historical_grad = historical_grad + grad_theta ** 2
else:
historical_grad = alpha * historical_grad + (1 - alpha) * (grad_theta ** 2)
adj_grad = np.divide(grad_theta, fudge_factor+np.sqrt(historical_grad))
theta = theta + stepsize * adj_grad
return theta
评论列表
文章目录