-
Notifications
You must be signed in to change notification settings - Fork 5.9k
add norm 2.0 api, test=develop #26465
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
306cd66
add norm 2.0 api, test=develop
frankwhzhang 9661c59
add norm 2.0 api, test=develop
frankwhzhang a61dc4d
add norm 2.0 api, test=develop
frankwhzhang 2799339
add norm 2.0 api, test=develop
frankwhzhang 2337889
add norm 2.0 api, test=develop
frankwhzhang d966f31
add norm 2.0 api, test=develop
frankwhzhang f733ec6
add norm 2.0 api, test=develop
frankwhzhang d4a9b02
add norm 2.0 api, test=develop
frankwhzhang 99ec8b9
add norm 2.0 api, test=develop
frankwhzhang 32863d6
add norm 2.0 api, test=develop
frankwhzhang 5a7eca6
add norm 2.0 api, test=develop
frankwhzhang 59d75bc
add norm 2.0 api, test=develop
frankwhzhang ed45c00
add norm 2.0 api, test=develop
frankwhzhang 338e6c3
add norm 2.0 api, test=develop
frankwhzhang 24e2cc7
add norm 2.0 api, test=develop
frankwhzhang e563081
add norm 2.0 api, test=develop
frankwhzhang 67c48d8
add norm 2.0 api, test=develop
frankwhzhang 72c4d1f
merge
frankwhzhang 5fa037c
add norm 2.0 api, test=develop
frankwhzhang d679e79
add norm 2.0 api, test=develop
frankwhzhang 4940e47
add norm 2.0 api, test=develop
frankwhzhang 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
173 changes: 173 additions & 0 deletions
173
python/paddle/fluid/tests/unittests/test_batch_norm_op_v2.py
This file contains hidden or 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,173 @@ | ||
| # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
|
|
||
| import os | ||
| import unittest | ||
| import numpy as np | ||
| import paddle.fluid.core as core | ||
| from paddle.fluid.op import Operator | ||
| import paddle.fluid as fluid | ||
| from op_test import OpTest, _set_use_system_allocator | ||
| from paddle.fluid.framework import grad_var_name | ||
| import paddle.fluid as fluid | ||
| from paddle.fluid import Program, program_guard | ||
| import paddle | ||
|
|
||
|
|
||
| class TestBatchNorm(unittest.TestCase): | ||
| def test_functional_dygraph(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| with fluid.dygraph.guard(p): | ||
| x_data = np.random.random(size=(2, 1, 2, 3)).astype('float32') | ||
| running_mean = np.random.random(size=1).astype('float32') | ||
| running_variance = np.random.random(size=1).astype('float32') | ||
| x = fluid.dygraph.to_variable(x_data) | ||
| rm = fluid.dygraph.to_variable(running_mean) | ||
| rv = fluid.dygraph.to_variable(running_variance) | ||
| batch_norm_out = paddle.nn.functional.batch_norm(x, rm, rv) | ||
|
|
||
| def test_functional_static(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| exe = fluid.Executor(p) | ||
| with program_guard(Program(), Program()): | ||
| x_data = np.random.random(size=(2, 1, 2, 3)).astype('float32') | ||
| running_mean = np.random.random(size=1).astype('float32') | ||
| running_variance = np.random.random(size=1).astype('float32') | ||
| x = fluid.data(name='x', shape=x_data.shape, dtype=x_data.dtype) | ||
| rm = fluid.data( | ||
| name='rm', | ||
| shape=running_mean.shape, | ||
| dtype=running_mean.dtype) | ||
| rv = fluid.data( | ||
| name='rv', | ||
| shape=running_variance.shape, | ||
| dtype=running_variance.dtype) | ||
| batch_norm_out = paddle.nn.functional.batch_norm(x, rm, rv) | ||
| exe.run(fluid.default_startup_program()) | ||
| r = exe.run(feed={ | ||
| 'x': x_data, | ||
| 'rm': running_mean, | ||
| 'rv': running_variance | ||
| }, | ||
| fetch_list=[batch_norm_out])[0] | ||
|
|
||
| batch_norm_out = paddle.nn.functional.batch_norm(x, rm, rv) | ||
|
|
||
| def test_name(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| with fluid.dygraph.guard(p): | ||
| batch_norm1d = paddle.nn.BatchNorm1d(1, name="test") | ||
|
|
||
| def test_error(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| #paddle.disable_static() | ||
| x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') | ||
| x_data_3 = np.random.random(size=(2, 1, 3)).astype('float32') | ||
|
|
||
| def error1d(): | ||
| x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') | ||
| batch_norm1d = paddle.nn.BatchNorm1d(1) | ||
| batch_norm1d(fluid.dygraph.to_variable(x_data_4)) | ||
|
|
||
| def error2d(): | ||
| x_data_3 = np.random.random(size=(2, 1, 3)).astype('float32') | ||
| batch_norm1d = paddle.nn.BatchNorm2d(1) | ||
| batch_norm1d(fluid.dygraph.to_variable(x_data_3)) | ||
|
|
||
| def error3d(): | ||
| x_data_4 = np.random.random(size=(2, 1, 3, 3)).astype('float32') | ||
| batch_norm1d = paddle.nn.BatchNorm3d(1) | ||
| batch_norm1d(fluid.dygraph.to_variable(x_data_4)) | ||
|
|
||
| with fluid.dygraph.guard(p): | ||
| self.assertRaises(ValueError, error1d) | ||
| self.assertRaises(ValueError, error2d) | ||
| self.assertRaises(ValueError, error3d) | ||
|
|
||
| def test_dygraph(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| shape = [4, 10, 4, 4] | ||
|
|
||
| def compute_v1(x, is_test, trainable_statistics): | ||
| with fluid.dygraph.guard(p): | ||
| bn = fluid.dygraph.BatchNorm( | ||
| shape[1], | ||
| is_test=is_test, | ||
| trainable_statistics=trainable_statistics) | ||
| y = bn(fluid.dygraph.to_variable(x)) | ||
| return y.numpy() | ||
|
|
||
| def compute_v2(x): | ||
| with fluid.dygraph.guard(p): | ||
| bn = paddle.nn.BatchNorm2d(shape[1]) | ||
| y = bn(fluid.dygraph.to_variable(x)) | ||
| return y.numpy() | ||
|
|
||
| x = np.random.randn(*shape).astype("float32") | ||
| y1 = compute_v1(x, False, False) | ||
| y2 = compute_v2(x) | ||
| self.assertTrue(np.allclose(y1, y2)) | ||
|
|
||
| def test_static(self): | ||
| places = [fluid.CPUPlace()] | ||
| if core.is_compiled_with_cuda() and core.op_support_gpu("batch_norm"): | ||
| places.append(fluid.CUDAPlace(0)) | ||
| for p in places: | ||
| exe = fluid.Executor(p) | ||
| shape = [4, 10, 16, 16] | ||
|
|
||
| def compute_v1(x_np, is_test, trainable_statistics): | ||
| with program_guard(Program(), Program()): | ||
| bn = fluid.dygraph.BatchNorm( | ||
| shape[1], | ||
| is_test=is_test, | ||
| trainable_statistics=trainable_statistics) | ||
| x = fluid.data(name='x', shape=x_np.shape, dtype=x_np.dtype) | ||
| y = bn(x) | ||
| exe.run(fluid.default_startup_program()) | ||
| r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] | ||
| return r | ||
|
|
||
| def compute_v2(x_np): | ||
| with program_guard(Program(), Program()): | ||
| bn = paddle.nn.BatchNorm2d(shape[1]) | ||
| x = fluid.data(name='x', shape=x_np.shape, dtype=x_np.dtype) | ||
| y = bn(x) | ||
| exe.run(fluid.default_startup_program()) | ||
| r = exe.run(feed={'x': x_np}, fetch_list=[y])[0] | ||
| return r | ||
|
|
||
| x = np.random.randn(*shape).astype("float32") | ||
| y1 = compute_v1(x, False, False) | ||
| y2 = compute_v2(x) | ||
| self.assertTrue(np.allclose(y1, y2)) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() |
This file contains hidden or 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
This file contains hidden or 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
Oops, something went wrong.
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.
l2_normalize 还是要留着的吧?
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.
误删 fixed