本文整理汇总了Python中caffe2.python.gradient_checker.GradientChecker方法的典型用法代码示例。如果您正苦于以下问题:Python gradient_checker.GradientChecker方法的具体用法?Python gradient_checker.GradientChecker怎么用?Python gradient_checker.GradientChecker使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类caffe2.python.gradient_checker
的用法示例。
在下文中一共展示了gradient_checker.GradientChecker方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _run_test
# 需要导入模块: from caffe2.python import gradient_checker [as 别名]
# 或者: from caffe2.python.gradient_checker import GradientChecker [as 别名]
def _run_test(self, A, B, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('SpatialNarrowAs', ['A', 'B'], ['C'])
workspace.FeedBlob('A', A)
workspace.FeedBlob('B', B)
workspace.RunOperatorOnce(op)
C = workspace.FetchBlob('C')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.005,
threshold=0.005,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [A, B], 0, [0])
self.assertTrue(res, 'Grad check failed')
dims = C.shape
C_ref = A[:dims[0], :dims[1], :dims[2], :dims[3]]
np.testing.assert_allclose(C, C_ref, rtol=1e-5, atol=1e-08)
示例2: _run_op_test
# 需要导入模块: from caffe2.python import gradient_checker [as 别名]
# 或者: from caffe2.python.gradient_checker import GradientChecker [as 别名]
def _run_op_test(self, X, I, check_grad=False):
with core.DeviceScope(core.DeviceOption(caffe2_pb2.CUDA, 0)):
op = core.CreateOperator('BatchPermutation', ['X', 'I'], ['Y'])
workspace.FeedBlob('X', X)
workspace.FeedBlob('I', I)
workspace.RunOperatorOnce(op)
Y = workspace.FetchBlob('Y')
if check_grad:
gc = gradient_checker.GradientChecker(
stepsize=0.1,
threshold=0.001,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(op, [X, I], 0, [0])
self.assertTrue(res, 'Grad check failed')
Y_ref = X[I]
np.testing.assert_allclose(Y, Y_ref, rtol=1e-5, atol=1e-08)
示例3: test_forward_and_gradient
# 需要导入模块: from caffe2.python import gradient_checker [as 别名]
# 或者: from caffe2.python.gradient_checker import GradientChecker [as 别名]
def test_forward_and_gradient(self):
Y = np.random.randn(128, 4 * 21).astype(np.float32)
Y_hat = np.random.randn(128, 4 * 21).astype(np.float32)
inside_weights = np.random.randn(128, 4 * 21).astype(np.float32)
inside_weights[inside_weights < 0] = 0
outside_weights = np.random.randn(128, 4 * 21).astype(np.float32)
outside_weights[outside_weights < 0] = 0
scale = np.random.random()
beta = np.random.random()
op = core.CreateOperator(
'SmoothL1Loss', ['Y_hat', 'Y', 'inside_weights', 'outside_weights'],
['loss'],
scale=scale,
beta=beta
)
gc = gradient_checker.GradientChecker(
stepsize=0.005,
threshold=0.005,
device_option=core.DeviceOption(caffe2_pb2.CUDA, 0)
)
res, grad, grad_estimated = gc.CheckSimple(
op, [Y_hat, Y, inside_weights, outside_weights], 0, [0]
)
self.assertTrue(
grad.shape == grad_estimated.shape,
'Fail check: grad.shape != grad_estimated.shape'
)
# To inspect the gradient and estimated gradient:
# np.set_printoptions(precision=3, suppress=True)
# print('grad:')
# print(grad)
# print('grad_estimated:')
# print(grad_estimated)
self.assertTrue(res)