-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
add weight_norm & remove_weight_norm #26131
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a54f1d6
add weight_norm, test=develop
ceci3 4027904
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
ceci3 4ca7eec
fix py2,test=develop
ceci3 20fe90d
fix import,test=develop
ceci3 c362450
fix, test=develop
ceci3 93abfe0
add utils in setup, test=develop
ceci3 c771ead
update, test=develop
ceci3 b3c307f
fix docs, test=develop
ceci3 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
183 changes: 183 additions & 0 deletions
183
python/paddle/fluid/tests/unittests/test_dygraph_weight_norm.py
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,183 @@ | ||
# 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. | ||
|
||
from __future__ import print_function | ||
|
||
import unittest | ||
import numpy | ||
import collections | ||
from functools import reduce | ||
import paddle | ||
import paddle.fluid as fluid | ||
import paddle.fluid.core as core | ||
from paddle.nn.utils import weight_norm, remove_weight_norm | ||
|
||
|
||
class TestDygraphWeightNorm(unittest.TestCase): | ||
def setUp(self): | ||
self.init_test_case() | ||
self.set_data() | ||
|
||
def init_test_case(self): | ||
self.batch_size = 3 | ||
self.data_desc = (['x', [2, 3, 3]], ) | ||
self.dim = None | ||
|
||
def set_data(self): | ||
self.data = collections.OrderedDict() | ||
for desc in self.data_desc: | ||
data_name = desc[0] | ||
data_shape = desc[1] | ||
data_value = numpy.random.random( | ||
size=[self.batch_size] + data_shape).astype('float32') | ||
self.data[data_name] = data_value | ||
|
||
def norm_except_dim(self, w, dim=None): | ||
shape = w.shape | ||
ndims = len(shape) | ||
shape_numel = reduce(lambda x, y: x * y, shape) | ||
if dim == -1: | ||
return numpy.linalg.norm(w, axis=None, keepdims=True) | ||
elif dim == 0: | ||
tile_shape = list(w.shape) | ||
tile_shape[0] = 1 | ||
w_matrix = numpy.reshape(w, (shape[0], shape_numel // shape[0])) | ||
return numpy.linalg.norm(w_matrix, axis=1, keepdims=True) | ||
elif dim == (ndims - 1): | ||
w_matrix = numpy.reshape(w, (shape_numel // shape[-1], shape[-1])) | ||
return numpy.linalg.norm(w_matrix, axis=0, keepdims=True) | ||
else: | ||
perm = list(range(ndims)) | ||
perm_ori = list(range(ndims)) | ||
perm[0] = dim | ||
perm[dim] = 0 | ||
p_transposed = numpy.transpose(w, perm) | ||
return self.norm_except_dim(p_transposed, 0) | ||
|
||
def weight_normalize(self, w, dim=None): | ||
shape = w.shape | ||
ndims = len(shape) | ||
shape_numel = reduce(lambda x, y: x * y, shape) | ||
v = w | ||
g = self.norm_except_dim(w, dim) | ||
g_mul = g | ||
|
||
if dim == -1: | ||
v_norm = v / (numpy.linalg.norm(v, axis=None, keepdims=True)) | ||
elif dim == 0: | ||
w_matrix = numpy.reshape(w, (shape[0], shape_numel // shape[0])) | ||
v_norm = v / numpy.linalg.norm(w_matrix, axis=1) | ||
v_norm = numpy.reshape(v_norm, shape) | ||
g = numpy.squeeze(g, axis=1) | ||
elif dim == (ndims - 1): | ||
w_matrix = numpy.reshape(w, (shape_numel // shape[-1], shape[-1])) | ||
v_norm = v / numpy.linalg.norm(w_matrix, axis=0, keepdims=True) | ||
v_norm = numpy.reshape(v_norm, shape) | ||
else: | ||
perm = list(range(ndims)) | ||
perm[0] = dim | ||
perm[dim] = 0 | ||
p_transposed = numpy.transpose(v, perm) | ||
transposed_shape = p_transposed.shape | ||
transposed_shape_numel = reduce(lambda x, y: x * y, | ||
transposed_shape) | ||
p_matrix = numpy.reshape( | ||
p_transposed, (p_transposed.shape[0], | ||
transposed_shape_numel // p_transposed.shape[0])) | ||
v_norm = v / numpy.expand_dims( | ||
numpy.expand_dims( | ||
numpy.linalg.norm( | ||
p_matrix, axis=1, keepdims=True), axis=0), | ||
axis=(ndims - 1)) | ||
v_norm = numpy.reshape(v_norm, transposed_shape) | ||
v_norm = numpy.transpose(v_norm, perm) | ||
g = numpy.squeeze(g, axis=1) | ||
if dim == 1: | ||
eaxis = 2 | ||
elif dim == 2: | ||
eaxis = 1 | ||
g_mul = numpy.expand_dims( | ||
numpy.expand_dims( | ||
numpy.expand_dims( | ||
g, axis=0), axis=eaxis), | ||
axis=(ndims - 1)) | ||
w = g_mul * v_norm | ||
return g, v | ||
|
||
def test_check_output(self): | ||
fluid.enable_imperative() | ||
linear = paddle.nn.Conv2D(2, 3, 3) | ||
before_weight = linear.weight.numpy() | ||
if self.dim == None: | ||
self.dim = -1 | ||
wn = weight_norm(linear, dim=self.dim) | ||
outputs = [] | ||
for name, data in self.data.items(): | ||
output = linear(fluid.dygraph.to_variable(data)) | ||
outputs.append(output.numpy()) | ||
after_weight = linear.weight | ||
self.actual_outputs = [linear.weight_g.numpy(), linear.weight_v.numpy()] | ||
|
||
expect_output = self.weight_normalize(before_weight, self.dim) | ||
|
||
for expect, actual in zip(expect_output, self.actual_outputs): | ||
self.assertTrue( | ||
numpy.allclose( | ||
numpy.array(actual), expect, atol=0.001)) | ||
|
||
|
||
class TestDygraphWeightNormCase1(TestDygraphWeightNorm): | ||
def init_test_case(self): | ||
self.batch_size = 3 | ||
self.data_desc = (['x', [2, 3, 3]], ) | ||
self.dim = 0 | ||
|
||
|
||
class TestDygraphWeightNormCase2(TestDygraphWeightNorm): | ||
def init_test_case(self): | ||
self.batch_size = 3 | ||
self.data_desc = (['x', [2, 3, 3]], ) | ||
self.dim = 1 | ||
|
||
|
||
class TestDygraphWeightNormCase3(TestDygraphWeightNorm): | ||
def init_test_case(self): | ||
self.batch_size = 3 | ||
self.data_desc = (['x', [2, 3, 3]], ) | ||
self.dim = 3 | ||
|
||
|
||
class TestDygraphRemoveWeightNorm(unittest.TestCase): | ||
def setUp(self): | ||
self.init_test_case() | ||
|
||
def init_test_case(self): | ||
self.batch_size = 3 | ||
self.data_desc = (['x', [2, 3, 3]], ) | ||
self.dim = None | ||
|
||
def test_check_output(self): | ||
fluid.enable_imperative() | ||
linear = paddle.nn.Conv2D(2, 3, 3) | ||
before_weight = linear.weight | ||
wn = weight_norm(linear, dim=self.dim) | ||
rwn = remove_weight_norm(linear) | ||
after_weight = linear.weight | ||
self.assertTrue( | ||
numpy.allclose( | ||
before_weight.numpy(), after_weight.numpy(), atol=0.001)) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |
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
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,16 @@ | ||
# 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. | ||
|
||
from . import weight_norm_hook | ||
from .weight_norm_hook import weight_norm, remove_weight_norm |
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.
move this line to the beginning of the docstring would be better. (i.e.: Line 206)
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.
done, thanks