-
Notifications
You must be signed in to change notification settings - Fork 5.7k
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
【Paddle Hackathon No.11】 #45595
Merged
Merged
【Paddle Hackathon No.11】 #45595
Changes from 4 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
373d598
2022-08-30_update nn.layer.loss nn.functional.loss, test_file
yangguohao f573b02
Merge branch 'PaddlePaddle:develop' into multi_margin_loss
yangguohao 9355a43
2022-08-30_update nn.layer.loss nn.functional.loss, test_file
yangguohao a317e41
fix: test_file
yangguohao eceeaab
fix: test_file, docs, multi_margin_loss
yangguohao 66d1b4e
Merge branch 'PaddlePaddle:develop' into multi_margin_loss
yangguohao f49bfc3
fix: doc weight function
yangguohao 0a042c8
fix: test_multi_margin_loss
yangguohao 8cb585b
fix: weight np.testing.assert_allclose
yangguohao ec06e40
Merge branch 'PaddlePaddle:develop' into multi_margin_loss
yangguohao 43c6226
fix: test_file
yangguohao 518c53c
fix: en_doc
yangguohao ef91530
2022-10-10
yangguohao a6eb056
Merge branch 'PaddlePaddle:develop' into multi_margin_loss
yangguohao 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
348 changes: 348 additions & 0 deletions
348
python/paddle/fluid/tests/unittests/test_multimarginloss.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,348 @@ | ||
# -*- coding: utf-8 -* | ||
# Copyright (c) 2022 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 paddle | ||
import numpy as np | ||
import unittest | ||
|
||
|
||
def call_MultiMarginLoss_layer( | ||
input, | ||
label, | ||
p=1, | ||
margin=1.0, | ||
weight=None, | ||
reduction='mean', | ||
): | ||
triplet_margin_loss = paddle.nn.MultiMarginLoss(p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
res = triplet_margin_loss( | ||
input=input, | ||
label=label, | ||
) | ||
return res | ||
|
||
|
||
def call_MultiMarginLoss_functional( | ||
input, | ||
label, | ||
p=1, | ||
margin=1.0, | ||
weight=None, | ||
reduction='mean', | ||
): | ||
res = paddle.nn.functional.multi_margin_loss(input=input, | ||
label=label, | ||
p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
return res | ||
|
||
|
||
def test_static(place, | ||
input_np, | ||
label_np, | ||
p=1, | ||
margin=1.0, | ||
weight_np=None, | ||
reduction='mean', | ||
functional=False): | ||
prog = paddle.static.Program() | ||
startup_prog = paddle.static.Program() | ||
with paddle.static.program_guard(prog, startup_prog): | ||
input = paddle.static.data(name='input', | ||
shape=input_np.shape, | ||
dtype=input_np.dtype) | ||
label = paddle.static.data(name='label', | ||
shape=label_np.shape, | ||
dtype=label_np.dtype) | ||
feed_dict = { | ||
"input": input_np, | ||
"label": label_np, | ||
} | ||
weight = None | ||
if weight_np is not None: | ||
weight = paddle.static.data(name='weight', | ||
shape=weight_np.shape, | ||
dtype=weight_np.dtype) | ||
feed_dict['weight'] = weight_np | ||
if functional: | ||
res = call_MultiMarginLoss_functional(input=input, | ||
label=label, | ||
p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
else: | ||
res = call_MultiMarginLoss_layer(input=input, | ||
label=label, | ||
p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
|
||
exe = paddle.static.Executor(place) | ||
static_result = exe.run(prog, feed=feed_dict, fetch_list=[res]) | ||
return static_result | ||
|
||
|
||
def test_dygraph(place, | ||
input, | ||
label, | ||
p=1, | ||
margin=1.0, | ||
weight=None, | ||
reduction='mean', | ||
functional=False): | ||
paddle.disable_static() | ||
input = paddle.to_tensor(input) | ||
label = paddle.to_tensor(label) | ||
|
||
if weight is not None: | ||
weight = paddle.to_tensor(weight) | ||
if functional: | ||
dy_res = call_MultiMarginLoss_functional(input=input, | ||
label=label, | ||
p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
else: | ||
dy_res = call_MultiMarginLoss_layer(input=input, | ||
label=label, | ||
p=p, | ||
margin=margin, | ||
weight=weight, | ||
reduction=reduction) | ||
dy_result = dy_res.numpy() | ||
paddle.enable_static() | ||
return dy_result | ||
|
||
|
||
def calc_multi_margin_loss( | ||
input, | ||
label, | ||
p=1, | ||
margin=1.0, | ||
weight=None, | ||
reduction='mean', | ||
): | ||
label = label.reshape(-1, 1) | ||
index_sample = [] | ||
for i in range(len(label)): | ||
index_sample.append(input[i, label[i]]) | ||
index_sample = np.array(index_sample).reshape(-1, 1) | ||
|
||
if weight is None: | ||
expected = np.mean(np.maximum(margin + input - index_sample, 0.0)**p, | ||
axis=1) - margin**p / input.shape[1] | ||
else: | ||
weight = weight.reshape(-1, 1) | ||
iclementine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
expected = np.mean(np.maximum(weight * (margin + input - index_sample), 0.0) ** p, axis=1) - margin ** p / \ | ||
input.shape[1] | ||
|
||
if reduction == 'mean': | ||
expected = np.mean(expected) | ||
elif reduction == 'sum': | ||
expected = np.sum(expected) | ||
else: | ||
expected = expected | ||
|
||
return expected | ||
|
||
|
||
class TestMultiMarginLoss(unittest.TestCase): | ||
|
||
def test_MultiMarginLoss(self): | ||
shape = (2, 2) | ||
input = np.random.uniform(0.1, 0.8, size=shape).astype(np.float64) | ||
label = np.random.uniform(0, input.shape[1], | ||
size=(2, )).astype(np.int32) | ||
|
||
places = [paddle.CPUPlace()] | ||
if paddle.device.is_compiled_with_cuda(): | ||
places.append(paddle.CUDAPlace(0)) | ||
reductions = ['sum', 'mean', 'none'] | ||
for place in places: | ||
for reduction in reductions: | ||
expected = calc_multi_margin_loss(input=input, | ||
label=label, | ||
reduction=reduction) | ||
|
||
dy_result = test_dygraph( | ||
place=place, | ||
input=input, | ||
label=label, | ||
reduction=reduction, | ||
) | ||
|
||
static_result = test_static( | ||
place=place, | ||
input_np=input, | ||
label_np=label, | ||
reduction=reduction, | ||
) | ||
self.assertTrue(np.allclose(static_result, expected)) | ||
self.assertTrue(np.allclose(static_result, dy_result)) | ||
self.assertTrue(np.allclose(dy_result, expected)) | ||
static_functional = test_static(place=place, | ||
input_np=input, | ||
label_np=label, | ||
reduction=reduction, | ||
functional=True) | ||
dy_functional = test_dygraph(place=place, | ||
input=input, | ||
label=label, | ||
reduction=reduction, | ||
functional=True) | ||
self.assertTrue(np.allclose(static_functional, expected)) | ||
self.assertTrue(np.allclose(static_functional, dy_functional)) | ||
self.assertTrue(np.allclose(dy_functional, expected)) | ||
|
||
def test_MultiMarginLoss_error(self): | ||
paddle.disable_static() | ||
self.assertRaises(ValueError, | ||
paddle.nn.MultiMarginLoss, | ||
reduction="unsupport reduction") | ||
input = paddle.to_tensor([[0.1, 0.3]], dtype='float32') | ||
label = paddle.to_tensor([0], dtype='int32') | ||
self.assertRaises(ValueError, | ||
paddle.nn.functional.multi_margin_loss, | ||
input=input, | ||
label=label, | ||
reduction="unsupport reduction") | ||
paddle.enable_static() | ||
|
||
def test_MultiMarginLoss_dimension(self): | ||
paddle.disable_static() | ||
|
||
input = paddle.to_tensor([[0.1, 0.3], [1, 2]], dtype='float32') | ||
label = paddle.to_tensor([0, 1, 1], dtype='int32') | ||
|
||
self.assertRaises( | ||
ValueError, | ||
paddle.nn.functional.multi_margin_loss, | ||
input=input, | ||
label=label, | ||
) | ||
MMLoss = paddle.nn.MultiMarginLoss() | ||
self.assertRaises( | ||
ValueError, | ||
MMLoss, | ||
input=input, | ||
label=label, | ||
) | ||
paddle.enable_static() | ||
|
||
def test_MultiMarginLoss_p(self): | ||
p = 2 | ||
shape = (2, 2) | ||
reduction = 'mean' | ||
place = paddle.CPUPlace() | ||
input = np.random.uniform(0.1, 0.8, size=shape).astype(np.float64) | ||
label = np.random.uniform(0, input.shape[1], | ||
size=(2, )).astype(np.int64) | ||
expected = calc_multi_margin_loss(input=input, | ||
p=p, | ||
label=label, | ||
reduction=reduction) | ||
|
||
dy_result = test_dygraph( | ||
place=place, | ||
p=p, | ||
input=input, | ||
label=label, | ||
reduction=reduction, | ||
) | ||
|
||
static_result = test_static( | ||
place=place, | ||
p=p, | ||
input_np=input, | ||
label_np=label, | ||
reduction=reduction, | ||
) | ||
self.assertTrue(np.allclose(static_result, expected)) | ||
self.assertTrue(np.allclose(static_result, dy_result)) | ||
self.assertTrue(np.allclose(dy_result, expected)) | ||
static_functional = test_static(place=place, | ||
p=p, | ||
input_np=input, | ||
label_np=label, | ||
reduction=reduction, | ||
functional=True) | ||
dy_functional = test_dygraph(place=place, | ||
p=p, | ||
input=input, | ||
label=label, | ||
reduction=reduction, | ||
functional=True) | ||
self.assertTrue(np.allclose(static_functional, expected)) | ||
self.assertTrue(np.allclose(static_functional, dy_functional)) | ||
self.assertTrue(np.allclose(dy_functional, expected)) | ||
|
||
def test_MultiMarginLoss_weight(self): | ||
shape = (2, 2) | ||
iclementine marked this conversation as resolved.
Show resolved
Hide resolved
|
||
reduction = 'mean' | ||
place = paddle.CPUPlace() | ||
input = np.random.uniform(0.1, 0.8, size=shape).astype(np.float64) | ||
label = np.random.uniform(0, input.shape[1], | ||
size=(2, )).astype(np.int64) | ||
weight = np.random.uniform(0, 2, size=(2, )).astype(np.float64) | ||
expected = calc_multi_margin_loss(input=input, | ||
label=label, | ||
weight=weight, | ||
reduction=reduction) | ||
|
||
dy_result = test_dygraph( | ||
place=place, | ||
input=input, | ||
label=label, | ||
weight=weight, | ||
reduction=reduction, | ||
) | ||
|
||
static_result = test_static( | ||
place=place, | ||
input_np=input, | ||
label_np=label, | ||
weight_np=weight, | ||
reduction=reduction, | ||
) | ||
self.assertTrue(np.allclose(static_result, expected)) | ||
self.assertTrue(np.allclose(static_result, dy_result)) | ||
self.assertTrue(np.allclose(dy_result, expected)) | ||
static_functional = test_static(place=place, | ||
input_np=input, | ||
label_np=label, | ||
weight_np=weight, | ||
reduction=reduction, | ||
functional=True) | ||
dy_functional = test_dygraph(place=place, | ||
input=input, | ||
label=label, | ||
weight=weight, | ||
reduction=reduction, | ||
functional=True) | ||
self.assertTrue(np.allclose(static_functional, expected)) | ||
self.assertTrue(np.allclose(static_functional, dy_functional)) | ||
self.assertTrue(np.allclose(dy_functional, expected)) | ||
|
||
|
||
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
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.
For test cases in static mode, you'd better add some cases that
paddle.static.data
creates data layer that has dynamic shape to ensure that it works as expected.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.
I add the new test cases of checking data shape in static mode. but I'm not quite sure about it.
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.
I mean, you can create an data layer with shape
[-1, -1]
and feed it with an array with shape[3, 4]
or[5, 6]
.Or create a data layer with shape
[-1, 3,4]
and feed it with an array with shape[7, 3, 4]
I the rank and all the static size matches, it is compatible.