Example #1
0
def _test_clamp_scalar_max(test_case, shape, device):
    input = flow.Tensor(np.random.randn(*shape),
                        dtype=flow.float32,
                        device=flow.device(device))
    of_out = flow.clamp(input, None, 0.5)
    np_out = np.clip(input.numpy(), None, 0.5)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))
Example #2
0
def _test_clamp_backward(test_case, shape, device):
    x = flow.Tensor(
        np.random.randn(*shape),
        dtype=flow.float32,
        device=flow.device(device),
        requires_grad=True,
    )
    y = flow.clamp(x, 0.1, 0.5).sum()
    y.backward()
    test_case.assertTrue(
        np.allclose(x.grad.numpy(), _numpy_clamp_grad(x.numpy(), 0.1, 0.5),
                    1e-5, 1e-5))
Example #3
0
def _test_clamp_integral(test_case, shape, device):
    input = flow.Tensor(np.random.randint(3, 10, (shape)),
                        device=flow.device(device))
    of_out = flow.clamp(input, 1, 5)
    np_out = np.clip(input.numpy(), 1, 5)
    test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5))