• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Python gradients.hessians函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中tensorflow.python.ops.gradients.hessians函数的典型用法代码示例。如果您正苦于以下问题:Python hessians函数的具体用法?Python hessians怎么用?Python hessians使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了hessians函数的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。

示例1: testHessianInvalidDimension

 def testHessianInvalidDimension(self):
   for shape in [(10, 10), None]:
     with self.test_session(use_gpu=True):
       x = array_ops.placeholder(tf.float32, shape)
       # Expect a ValueError because the dimensions are wrong
       with self.assertRaises(ValueError):
         gradients.hessians(x, x)
开发者ID:DavidNemeskey,项目名称:tensorflow,代码行数:7,代码来源:gradients_test.py


示例2: testHessian1D

 def testHessian1D(self):
   # Manually compute the Hessian explicitly for a low-dimensional problem
   # and check that `hessian` matches. Specifically, the Hessian of 
   # f(x) = x^T A x is H = A + A^T.
   m = 4
   rng = np.random.RandomState([1, 2, 3])
   mat_value = rng.randn(m, m).astype("float32")
   x_value = rng.randn(m).astype("float32")
   hess_value = mat_value + mat_value.T
   with self.test_session(use_gpu=True):
     mat = constant_op.constant(mat_value)
     x = constant_op.constant(x_value)
     x_mat_x = math_ops.reduce_sum(x[:, None] * mat * x[None, :])
     hess = gradients.hessians(x_mat_x, x)[0]
     hess_actual = hess.eval()
   self.assertAllClose(hess_value, hess_actual)
开发者ID:DavidNemeskey,项目名称:tensorflow,代码行数:16,代码来源:gradients_test.py


示例3: testHessian1D_multi

 def testHessian1D_multi(self):
     # Test the computation of the hessian with respect to multiple tensors
     m = 4
     n = 3
     rng = np.random.RandomState([1, 2, 3])
     mat_values = [rng.randn(m, m).astype("float32") for _ in range(n)]
     x_values = [rng.randn(m).astype("float32") for _ in range(n)]
     hess_values = [mat_value + mat_value.T for mat_value in mat_values]
     with self.test_session(use_gpu=True):
         mats = [constant_op.constant(mat_value) for mat_value in mat_values]
         xs = [constant_op.constant(x_value) for x_value in x_values]
         xs_mats_xs = [math_ops.reduce_sum(x[:, None] * mat * x[None, :]) for x, mat in zip(xs, mats)]
         hessians = gradients.hessians(xs_mats_xs, xs)
         hessians_actual = [hess.eval() for hess in hessians]
     for hess_value, hess_actual in zip(hess_values, hessians_actual):
         self.assertAllClose(hess_value, hess_actual)
开发者ID:brchiu,项目名称:tensorflow,代码行数:16,代码来源:gradients_test.py


示例4: testHessian2D_non_square_matrix

 def testHessian2D_non_square_matrix(self):
   m = 3
   n = 4
   rng = np.random.RandomState([1, 2, 3])
   x_value = rng.randn(m, n).astype("float32")
   with self.test_session(use_gpu=True):
     x = constant_op.constant(x_value)
     x_square = math_ops.reduce_sum(
         math_ops.matmul(array_ops.transpose(x), x) * 0.5
     )
     hess = gradients.hessians(x_square, x)[0]
     hess_actual = hess.eval()
   hess_value = np.bmat([
       [elem*np.ones((n, n)) for elem in vec]
       for vec in np.eye(m)
   ]).astype("float32")
   self.assertAllEqual((m, n, m, n), hess_actual.shape)
   self.assertAllClose(hess_value, hess_actual.reshape((m * n, m * n)))
开发者ID:didukhle,项目名称:tensorflow,代码行数:18,代码来源:gradients_test.py


示例5: testHessian2D_square_matrix

 def testHessian2D_square_matrix(self):
   # Manually compute the Hessian explicitly for a low-dimensional problem
   # and check that `hessian` matches. Specifically, the Hessian of
   # f(x) = 1/2 * x^T * x is H = constant (block identity matrix)
   m = 3
   rng = np.random.RandomState([1, 2, 3])
   x_value = rng.randn(m, m).astype("float32")
   with self.test_session(use_gpu=True):
     x = constant_op.constant(x_value)
     x_square = math_ops.reduce_sum(
         math_ops.matmul(array_ops.transpose(x), x) * 0.5
     )
     hess = gradients.hessians(x_square, x)[0]
     hess_actual = hess.eval()
   hess_value = np.bmat([
       [elem*np.ones((m, m)) for elem in vec]
       for vec in np.eye(m)
   ]).astype("float32")
   self.assertAllEqual((m, m, m, m), hess_actual.shape)
   self.assertAllClose(hess_value, hess_actual.reshape((m * m, m * m)))
开发者ID:didukhle,项目名称:tensorflow,代码行数:20,代码来源:gradients_test.py



注:本文中的tensorflow.python.ops.gradients.hessians函数示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python gradients_impl._AggregateIndexedSlicesGradients函数代码示例发布时间:2022-05-27
下一篇:
Python gradients.gradients函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap