-
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
【PaddlePaddle Hackathon 2】23、为 Paddle 新增 Softmax2D 组网API #40910
Conversation
你的PR提交成功,感谢你对开源项目的贡献! |
请先通过CI噢~ |
PR-CI-Windows这个CI是需要通过的哦~ |
@dingjiaweiww 你好,已通过 |
请注意代码PR中还需要和中文文档关联才可合入,可以参考这个PR:#40472 |
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.
代码整体完整,部分细节需要完善
@@ -0,0 +1,90 @@ | |||
# Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved. |
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.
空格对齐
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from __future__ import print_function |
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.
这个不需要
from test_softmax_op import ref_softmax | ||
|
||
paddle.enable_static() | ||
np.random.seed(2022) |
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.
不需要固定随机种子
import paddle.fluid.core as core | ||
from test_softmax_op import ref_softmax | ||
|
||
paddle.enable_static() |
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.
这个可以放到 def test_static_api(self) 中
else paddle.CPUPlace() | ||
|
||
def test_extra_repr(self): | ||
paddle.disable_static(self.place) |
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.
这里相应修改
self.x_np = np.random.uniform(-1, 1, self.shape).astype('float64') | ||
self.axis = -3 | ||
self.place = paddle.CPUPlace() | ||
|
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.
增加GPU上单独测试、shape不是3或4维度的错误测试(可以使用assertRaises(error, func,input))
|
||
|
||
class TestSoftmax2DAPI(unittest.TestCase): | ||
# test paddle.nn.Softmax2D |
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.
不必要的注释可以删去
# test paddle.nn.Softmax2D | ||
def setUp(self): | ||
self.shape = [2, 6, 5, 4] | ||
self.dtype = 'float64' |
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.
输入默认需要支持float32,可以增加一个float32的测试
已修改并添加中文文档 |
PR-CI-Kunlun和PR-CI-Windows-Inference这两个CI需要通过的噢~ |
@dingjiaweiww 你好,已通过 |
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.
LGTM
x_np = np.random.randn(2, 3, 4, 2, 3) | ||
x = paddle.to_tensor(x_np, dtype='float64') | ||
m = paddle.nn.Softmax2D() | ||
self.assertRaises(AssertionError, m, x) |
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.
这里为什么是个AssertionError?
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.
因为代码中使用了assert
assert x.ndim == 3 or x.ndim == 4
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.
开发者在python层面实现避免输入维度不符合期望使用了assert,因此维度不匹配会触发AssertionError。
def setUp(self): | ||
self.shape = [2, 3, 4] | ||
self.x_np = np.random.uniform(-1, 1, self.shape).astype('float32') | ||
self.axis = -3 |
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.
axis可以覆盖不同的值,不需要都是-3,比如0, 正数
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.
对于softmax2d来说,需要将softmax的axis固定为-3,这里只是显式的写出来了,实际上不需要变动
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.
这里主要因为 softmax2D 的功能需求是固定了axis=-3的,如果不设置成-3,numpy的实现ref_softmax的就不是实现softmax2D的功能了。那么不能用于和softmax2D的对比测试了。
PR格式检查通过,你的PR将接受Paddle专家以及开源社区的review,请及时关注PR动态。 |
Sorry to inform you that d0484ba's CIs have passed for more than 7 days. To prevent PR conflicts, you need to re-run all CIs manually. |
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.
LG API
PR types
Others
PR changes
APIs
Describe
解决了issue: #40310
增加了paddle.nn.Softmax2D。具体地,
paddle.nn.Softmax2D()
在空间维度计算softmax,从而使输出 tensor 在每个空间维度(channels,hi,wj)的 tensor 求和为1,即表示空间位置上的某个通道向量求和为1。设计文档:PaddlePaddle/community#50
中文文档:PaddlePaddle/docs#4543