Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
8032d57e
P
Paddle
项目概览
PaddlePaddle
/
Paddle
1 年多 前同步成功
通知
2302
Star
20931
Fork
5422
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1423
列表
看板
标记
里程碑
合并请求
543
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1,423
Issue
1,423
列表
看板
标记
里程碑
合并请求
543
合并请求
543
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
8032d57e
编写于
5月 24, 2023
作者:
Z
zhangkaihuo
提交者:
GitHub
5月 24, 2023
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[Sparse]sparse BatchNorm support 2D input (#53893)
上级
f55f9d79
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
38 addition
and
3 deletion
+38
-3
python/paddle/fluid/tests/unittests/test_sparse_norm_op.py
python/paddle/fluid/tests/unittests/test_sparse_norm_op.py
+33
-0
python/paddle/sparse/nn/layer/norm.py
python/paddle/sparse/nn/layer/norm.py
+5
-3
未找到文件。
python/paddle/fluid/tests/unittests/test_sparse_norm_op.py
浏览文件 @
8032d57e
...
...
@@ -88,6 +88,39 @@ class TestSparseBatchNorm(unittest.TestCase):
assert
np
.
allclose
(
dense_out
.
numpy
(),
batch_norm_out
.
values
().
numpy
())
# [1, 6, 6, 6, 3]
def
check
(
self
,
shape
):
np
.
random
.
seed
(
0
)
data
=
np
.
random
.
uniform
(
-
0.01
,
0.01
,
shape
).
astype
(
"float32"
)
x
=
paddle
.
to_tensor
(
data
)
x
.
stop_gradient
=
False
dim
=
len
(
shape
)
data_format
=
"NHWC"
if
dim
==
4
else
"NDHWC"
if
dim
==
4
:
bn
=
paddle
.
nn
.
BatchNorm2D
(
shape
[
-
1
],
data_format
=
data_format
)
else
:
bn
=
paddle
.
nn
.
BatchNorm3D
(
shape
[
-
1
],
data_format
=
data_format
)
y
=
bn
(
x
)
y
.
backward
()
sp_x
=
paddle
.
to_tensor
(
data
).
to_sparse_coo
(
dim
-
1
)
sp_x
.
stop_gradient
=
False
sp_bn
=
paddle
.
sparse
.
nn
.
BatchNorm
(
shape
[
-
1
],
data_format
=
data_format
)
sp_y
=
sp_bn
(
sp_x
)
sp_y
.
backward
()
np
.
testing
.
assert_allclose
(
y
.
numpy
(),
sp_y
.
to_dense
().
numpy
(),
rtol
=
1e-5
)
np
.
testing
.
assert_allclose
(
x
.
grad
.
numpy
(),
sp_x
.
grad
.
to_dense
().
numpy
(),
rtol
=
1e-5
)
def
test_nd
(
self
):
# 2D
self
.
check
([
2
,
8
,
8
,
3
])
# 3D
self
.
check
([
2
,
8
,
8
,
3
,
4
])
class
TestSyncBatchNorm
(
unittest
.
TestCase
):
def
test_sync_batch_norm
(
self
):
...
...
python/paddle/sparse/nn/layer/norm.py
浏览文件 @
8032d57e
...
...
@@ -73,7 +73,7 @@ class BatchNorm(paddle.nn.BatchNorm1D):
name(str, optional): Name for the BatchNorm, default is None. For more information, please refer to :ref:`api_guide_Name`..
Shape:
- x: A SparseCooTensor with layout = 'NDHWC'.
- x: A SparseCooTensor with layout = 'NDHWC'
or 'NHWC'
.
- output: SparseCooTensor with same shape as input x.
Returns:
...
...
@@ -119,8 +119,10 @@ class BatchNorm(paddle.nn.BatchNorm1D):
)
def
_check_data_format
(
self
,
input
):
if
input
!=
"NDHWC"
:
raise
ValueError
(
'sparse BatchNorm only support layout of "NDHWC"'
)
if
input
not
in
[
"NDHWC"
,
"NHWC"
]:
raise
ValueError
(
'sparse BatchNorm only support layout of "NDHWC" and "NHWC"'
)
def
forward
(
self
,
input
):
self
.
_check_data_format
(
self
.
_data_format
)
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录