-
Notifications
You must be signed in to change notification settings - Fork 117
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PaddlePaddle hackathon] add test of paddle.nn.ClipGradByValue #228
Merged
Merged
Changes from 6 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
81110b6
add test of paddle.nn.ClipGradByValue
simuler 914df37
add test of paddle.nn.ClipGradByValue
simuler 92df39e
remove obj of paddle.nn.ClipGradByValue
simuler 096fbad
Merge branch 'develop' into develop
simuler b2a9bcd
add code annotation in test_clip_grad_by_value.py
simuler 9eea27c
Merge branch 'develop' into develop
simuler 4ee9c9c
Merge branch 'PaddlePaddle:develop' into develop
simuler ec168d2
Merge branch 'PaddlePaddle:develop' into develop
simuler add0384
Add test case of paddle.nn.clip_grad_by_value.py
simuler 18a759f
Merge branch 'develop' into develop
DDDivano 84bea17
Merge branch 'develop' into develop
DDDivano 7614dff
Merge branch 'develop' into develop
DDDivano a288d7a
Merge branch 'develop' into develop
DDDivano File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
#!/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# encoding=utf-8 vi:ts=4:sw=4:expandtab:ft=python | ||
""" | ||
test_clip_grad_by_value | ||
""" | ||
|
||
from apibase import APIBase | ||
from apibase import randtool, compare | ||
import paddle | ||
import pytest | ||
import numpy as np | ||
|
||
|
||
def numpy_clip_grad_by_value(test_data, clip_max, clip_min=None): | ||
""" | ||
ClipGradByValue implemented by numpy. | ||
""" | ||
cliped_data = [] | ||
for data, grad in test_data: | ||
grad = np.clip(grad, clip_min, clip_max) | ||
cliped_data.append((data, grad)) | ||
return cliped_data | ||
|
||
|
||
def generate_test_data(length, shape, dtype="float32"): | ||
""" | ||
generate test data | ||
""" | ||
tensor_data = [] | ||
numpy_data = [] | ||
np.random.seed(100) | ||
for i in range(length): | ||
np_weight = np.random.rand(*shape) | ||
np_weight_grad = np.random.rand(*shape) | ||
numpy_data.append((np_weight, np_weight_grad)) | ||
|
||
tensor_weight = paddle.to_tensor(np_weight) | ||
tensor_weight_grad = paddle.to_tensor(np_weight_grad) | ||
tensor_data.append((tensor_weight, tensor_weight_grad)) | ||
return numpy_data, tensor_data | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_vartype | ||
def test_clip_grad_by_value_base(): | ||
""" | ||
Test base. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [10, 10] | ||
length = 4 | ||
clip_min = None | ||
clip_max = 1.0 | ||
np_data, paddle_data = generate_test_data(length, shape) | ||
np_res = numpy_clip_grad_by_value(np_data, clip_max=clip_max, clip_min=clip_min) | ||
|
||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_cliped_data = paddle_clip(paddle_data) | ||
paddle_res = [] | ||
for w, g in paddle_cliped_data: | ||
paddle_res.append((w.numpy(), g.numpy())) | ||
|
||
for res, p_res in zip(np_res, paddle_res): | ||
compare(res[1], p_res[1]) | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_parameters | ||
def test_clip_grad_by_value_norm1(): | ||
""" | ||
Test ClipGradByValue when input shape changes. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Changes: | ||
input grad shape: [10, 10] -> [4, 10, 10] | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [4, 10, 10] | ||
length = 4 | ||
clip_min = None | ||
clip_max = 1.0 | ||
np_data, paddle_data = generate_test_data(length, shape) | ||
np_res = numpy_clip_grad_by_value(np_data, clip_max=clip_max, clip_min=clip_min) | ||
|
||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_cliped_data = paddle_clip(paddle_data) | ||
paddle_res = [] | ||
for w, g in paddle_cliped_data: | ||
paddle_res.append((w.numpy(), g.numpy())) | ||
|
||
for res, p_res in zip(np_res, paddle_res): | ||
compare(res[1], p_res[1]) | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_parameters | ||
def test_clip_grad_by_value_norm2(): | ||
""" | ||
Test ClipGradByValue when input data dtype changes. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Changes: | ||
input data dtype: float32 -> float64 | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [10, 10] | ||
length = 4 | ||
clip_min = None | ||
clip_max = 1.0 | ||
dtype = "float64" | ||
np_data, paddle_data = generate_test_data(length, shape, dtype=dtype) | ||
np_res = numpy_clip_grad_by_value(np_data, clip_max=clip_max, clip_min=clip_min) | ||
|
||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_cliped_data = paddle_clip(paddle_data) | ||
paddle_res = [] | ||
for w, g in paddle_cliped_data: | ||
paddle_res.append((w.numpy(), g.numpy())) | ||
|
||
for res, p_res in zip(np_res, paddle_res): | ||
compare(res[1], p_res[1]) | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_parameters | ||
def test_clip_grad_by_value_norm3(): | ||
""" | ||
Test ClipGradByValue when clip_min and clip_max changes. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Changes: | ||
input parameters: clip_min = None -> clip_min = -2.0, clip_max = 1.0 -> clip_max = -1.0 | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [10, 10] | ||
length = 4 | ||
clip_min = -2.0 | ||
clip_max = -1.0 | ||
np_data, paddle_data = generate_test_data(length, shape) | ||
np_res = numpy_clip_grad_by_value(np_data, clip_max=clip_max, clip_min=clip_min) | ||
|
||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_cliped_data = paddle_clip(paddle_data) | ||
paddle_res = [] | ||
for w, g in paddle_cliped_data: | ||
paddle_res.append((w.numpy(), g.numpy())) | ||
|
||
for res, p_res in zip(np_res, paddle_res): | ||
compare(res[1], p_res[1]) | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_parameters | ||
def test_clip_grad_by_value_norm4(): | ||
""" | ||
Test ClipGradByValue when clip_max value changes. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Changes: | ||
clip_max: 1.0 -> -1.0 | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [10, 10] | ||
length = 4 | ||
clip_min = None | ||
clip_max = -1.0 | ||
np_data, paddle_data = generate_test_data(length, shape) | ||
|
||
with pytest.raises(AssertionError): | ||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_clip(paddle_data) | ||
|
||
|
||
@pytest.mark.api_nn_ClipGradByValue_parameters | ||
def test_clip_grad_by_value_norm5(): | ||
""" | ||
Test ClipGradByValue when clip_max type changes. | ||
|
||
Test base config: | ||
input grad shape = [10, 10] | ||
input grad number = 4 | ||
input data dtype = 'float32' | ||
clip_min = None | ||
clip_max = 1.0 | ||
Changes: | ||
clip_max: float32 -> '1' | ||
Expected Results: | ||
The output of ClipGradByValue implemented by numpy and paddle should be equal. | ||
""" | ||
shape = [10, 10] | ||
length = 4 | ||
clip_min = None | ||
clip_max = "1" | ||
np_data, paddle_data = generate_test_data(length, shape) | ||
|
||
with pytest.raises(TypeError): | ||
paddle_clip = paddle.nn.ClipGradByValue(clip_max, clip_min) | ||
paddle_clip(paddle_data) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 增加下min和max相等情况下的case,输入为除了float32和float64外其他类型的case There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你好,已经增加min和max相等的case,输入为float16类型的case |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
处理异常时校验下框架抛出的异常信息,在不同异常下抛出的是不同的,这个需要校验下