-
Notifications
You must be signed in to change notification settings - Fork 695
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 mseloss module #5116
Merged
+214
−0
Merged
add mseloss module #5116
Changes from 2 commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
3a32837
add mseloss module
YongtaoShi a4a4e88
add mseloss testcase
YongtaoShi 7da3e82
delete debug code
YongtaoShi f5982f0
add mseloss testcase
YongtaoShi e54a270
rename mseloss testcase
YongtaoShi 70dad43
Merge branch 'master' into shiyongtao/dev_mseloss
YongtaoShi 04ee467
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot 4f40c1c
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot 65dcf77
fix docstring warning
YongtaoShi a42d039
Merge branch 'shiyongtao/dev_mseloss' of https://github.com/Oneflow-I…
YongtaoShi 257b255
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot f74fac5
Merge branch 'shiyongtao/dev_mseloss' of https://github.com/Oneflow-I…
YongtaoShi ef7b376
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot 037d972
Merge branch 'master' of https://github.com/Oneflow-Inc/oneflow into …
YongtaoShi bcdaf7a
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot c36e3e1
format docstring
YongtaoShi 1d0d523
Merge branch 'shiyongtao/dev_mseloss' of https://github.com/Oneflow-I…
YongtaoShi 4a7d081
Merge branch 'master' into shiyongtao/dev_mseloss
oneflow-ci-bot 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
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
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,128 @@ | ||
""" | ||
Copyright 2020 The OneFlow 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 unittest | ||
from collections import OrderedDict | ||
|
||
import numpy as np | ||
|
||
import oneflow.experimental as flow | ||
from test_util import GenArgList | ||
|
||
|
||
def _np_mseloss(np_input, np_target): | ||
np_mse = np.square(np_target - np_input) | ||
np_mse_mean = np.mean(np_mse) | ||
np_mse_sum = np.sum(np_mse) | ||
|
||
return { | ||
"none": np_mse, | ||
"mean": np_mse_mean, | ||
"sum": np_mse_sum, | ||
} | ||
|
||
|
||
def _np_mseloss_grad(np_input, np_target): | ||
elem_cnt = np_input.size | ||
np_mse_grad_sum = -2 * (np_target - np_input) | ||
np_mse_grad_mean = np_mse_grad_sum / elem_cnt | ||
|
||
return { | ||
"none": np_mse_grad_sum, | ||
"mean": np_mse_grad_mean, | ||
"sum": np_mse_grad_sum, | ||
} | ||
|
||
|
||
def _test_mseloss_backward(test_case, device, reduction): | ||
x = np.random.randn(3, 5) | ||
y = np.random.randn(3, 5) | ||
input = flow.Tensor( | ||
x, dtype=flow.float32, requires_grad=True, device=flow.device(device) | ||
) | ||
target = flow.Tensor(y, dtype=flow.float32, device=flow.device(device)) | ||
|
||
loss = flow.nn.MSELoss(reduction=reduction) | ||
loss = loss.to(device) | ||
of_out = loss(input, target) | ||
np_out = _np_mseloss(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5)) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_grad = _np_mseloss_grad(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-5, 1e-5)) | ||
|
||
|
||
def _test_mseloss_high_dim_input_backward(test_case, device, reduction): | ||
x = np.random.randn(3, 2, 4, 16, 5) | ||
y = np.random.randn(3, 2, 4, 16, 5) | ||
input = flow.Tensor( | ||
x, dtype=flow.float32, requires_grad=True, device=flow.device(device) | ||
) | ||
target = flow.Tensor(y, dtype=flow.float32, device=flow.device(device)) | ||
|
||
loss = flow.nn.MSELoss(reduction=reduction) | ||
loss = loss.to(device) | ||
of_out = loss(input, target) | ||
np_out = _np_mseloss(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5)) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_grad = _np_mseloss_grad(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-5, 1e-5)) | ||
|
||
|
||
def _test_mseloss_one_elem_input_backward(test_case, device, reduction): | ||
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. 既然numpy实现了forward和backward,那么这些测试样例都可以合并,通过设置shape来统一测试。 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. 已修改 |
||
x = np.array([0]).astype(np.float) | ||
y = np.array([-1]).astype(np.float) | ||
input = flow.Tensor( | ||
x, dtype=flow.float32, requires_grad=True, device=flow.device(device) | ||
) | ||
target = flow.Tensor(y, dtype=flow.float32, device=flow.device(device)) | ||
|
||
loss = flow.nn.MSELoss(reduction=reduction) | ||
loss = loss.to(device) | ||
of_out = loss(input, target) | ||
np_out = _np_mseloss(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(of_out.numpy(), np_out, 1e-5, 1e-5)) | ||
|
||
of_out = of_out.sum() | ||
of_out.backward() | ||
np_grad = _np_mseloss_grad(x, y)[reduction] | ||
test_case.assertTrue(np.allclose(input.grad.numpy(), np_grad, 1e-5, 1e-5)) | ||
|
||
|
||
@unittest.skipIf( | ||
not flow.unittest.env.eager_execution_enabled(), | ||
".numpy() doesn't work in lazy mode", | ||
) | ||
class TestMSELossModule(flow.unittest.TestCase): | ||
def test_mseloss(test_case): | ||
arg_dict = OrderedDict() | ||
arg_dict["test_fun"] = [ | ||
_test_mseloss_backward, | ||
_test_mseloss_high_dim_input_backward, | ||
_test_mseloss_one_elem_input_backward, | ||
] | ||
arg_dict["device"] = ["cpu", "cuda"] | ||
arg_dict["reduction"] = ["none", "mean", "sum"] | ||
for arg in GenArgList(arg_dict): | ||
arg[0](test_case, *arg[1:]) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
这里修改一下,类似于
reduction parameter only support 'sum'/'mean'/'none'/None value now!
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.
已修改