本文整理汇总了Python中bayespy.nodes.GaussianARD.get_gradient方法的典型用法代码示例。如果您正苦于以下问题:Python GaussianARD.get_gradient方法的具体用法?Python GaussianARD.get_gradient怎么用?Python GaussianARD.get_gradient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类bayespy.nodes.GaussianARD
的用法示例。
在下文中一共展示了GaussianARD.get_gradient方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_annealing
# 需要导入模块: from bayespy.nodes import GaussianARD [as 别名]
# 或者: from bayespy.nodes.GaussianARD import get_gradient [as 别名]
def test_annealing(self):
X = GaussianARD(3, 4)
X.initialize_from_parameters(-1, 6)
Q = VB(X)
Q.set_annealing(0.1)
#
# Check that the gradient is correct
#
# Initial parameters
phi0 = X.phi
# Gradient
rg = X.get_riemannian_gradient()
g = X.get_gradient(rg)
# Numerical gradient of the first parameter
eps = 1e-6
p0 = X.get_parameters()
l0 = Q.compute_lowerbound(ignore_masked=False)
g_num = [(), ()]
e = eps
p1 = p0[0] + e
X.set_parameters([p1, p0[1]])
l1 = Q.compute_lowerbound(ignore_masked=False)
g_num[0] = (l1 - l0) / eps
# Numerical gradient of the second parameter
p1 = p0[1] + e
X.set_parameters([p0[0], p1])
l1 = Q.compute_lowerbound(ignore_masked=False)
g_num[1] = (l1 - l0) / (eps)
# Check
self.assertAllClose(g[0],
g_num[0])
self.assertAllClose(g[1],
g_num[1])
#
# Gradient should be zero after updating
#
X.update()
# Initial parameters
phi0 = X.phi
# Numerical gradient of the first parameter
eps = 1e-8
p0 = X.get_parameters()
l0 = Q.compute_lowerbound(ignore_masked=False)
g_num = [(), ()]
e = eps
p1 = p0[0] + e
X.set_parameters([p1, p0[1]])
l1 = Q.compute_lowerbound(ignore_masked=False)
g_num[0] = (l1 - l0) / eps
# Numerical gradient of the second parameter
p1 = p0[1] + e
X.set_parameters([p0[0], p1])
l1 = Q.compute_lowerbound(ignore_masked=False)
g_num[1] = (l1 - l0) / (eps)
# Check
self.assertAllClose(0,
g_num[0],
atol=1e-5)
self.assertAllClose(0,
g_num[1],
atol=1e-5)
# Not at the optimum
X.initialize_from_parameters(-1, 6)
# Initial parameters
phi0 = X.phi
# Gradient
g = X.get_riemannian_gradient()
# Parameters after VB-EM update
X.update()
phi1 = X.phi
# Check
self.assertAllClose(g[0],
phi1[0] - phi0[0])
self.assertAllClose(g[1],
phi1[1] - phi0[1])
pass