def test_grad_clip_value():
    """
    **Overview**:
        Test function of grad clip with a fixed value.
    """
    # Prepare hyper-parameters, batch size=4, action=32
    B, N = 4, 32
    # Set clip_value as 1e-3
    clip_value = 1e-3
    # Generate regression logit and label, in practice, logit is the output of the whole network and requires gradient.
    logit = torch.randn(B, N).requires_grad_(True)
    label = torch.randn(B, N)
    # Define criterion, and compute loss.
    criterion = torch.nn.MSELoss()
    output = criterion(logit, label)
    # Loss backward and compute gradient.
    output.backward()
    # Clip the gradient with a fixed value.
    grad_clip_value(logit, clip_value)
    # Assert that the value of the clipped gradient is reasonable after clipping.
    assert isinstance(logit.grad, torch.Tensor)
    for g in logit.grad:
        assert (g <= clip_value).all()
        assert (g >= -clip_value).all()

    **Overview**:
        Test function of grad clip with a fixed value.
    
chapter7_tricks/grad_clip_value.py