Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
magicwindyyd
mindspore
提交
67057d13
M
mindspore
项目概览
magicwindyyd
/
mindspore
与 Fork 源项目一致
Fork自
MindSpore / mindspore
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
mindspore
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
67057d13
编写于
4月 22, 2020
作者:
M
mindspore-ci-bot
提交者:
Gitee
4月 22, 2020
浏览文件
操作
浏览文件
下载
差异文件
!541 add average pooling 1D
Merge pull request !541 from JichenZhao/avgpooling
上级
3fa31b34
faf95e40
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
99 addition
and
2 deletion
+99
-2
mindspore/nn/layer/__init__.py
mindspore/nn/layer/__init__.py
+2
-2
mindspore/nn/layer/pooling.py
mindspore/nn/layer/pooling.py
+81
-0
tests/ut/python/nn/test_pooling.py
tests/ut/python/nn/test_pooling.py
+16
-0
未找到文件。
mindspore/nn/layer/__init__.py
浏览文件 @
67057d13
...
...
@@ -24,7 +24,7 @@ from .conv import Conv2d, Conv2dTranspose
from
.lstm
import
LSTM
from
.basic
import
Dropout
,
Flatten
,
Dense
,
ClipByNorm
,
Norm
,
OneHot
,
Pad
,
Unfold
from
.embedding
import
Embedding
from
.pooling
import
AvgPool2d
,
MaxPool2d
from
.pooling
import
AvgPool2d
,
MaxPool2d
,
AvgPool1d
from
.image
import
ImageGradients
,
SSIM
,
PSNR
__all__
=
[
'Softmax'
,
'LogSoftmax'
,
'ReLU'
,
'ReLU6'
,
'Tanh'
,
'GELU'
,
'Sigmoid'
,
...
...
@@ -35,6 +35,6 @@ __all__ = ['Softmax', 'LogSoftmax', 'ReLU', 'ReLU6', 'Tanh', 'GELU', 'Sigmoid',
'LSTM'
,
'Dropout'
,
'Flatten'
,
'Dense'
,
'ClipByNorm'
,
'Norm'
,
'OneHot'
,
'Embedding'
,
'AvgPool2d'
,
'MaxPool2d'
,
'Pad'
,
'Unfold'
,
'AvgPool2d'
,
'MaxPool2d'
,
'
AvgPool1d'
,
'
Pad'
,
'Unfold'
,
'ImageGradients'
,
'SSIM'
,
'PSNR'
,
]
mindspore/nn/layer/pooling.py
浏览文件 @
67057d13
...
...
@@ -14,9 +14,12 @@
# ============================================================================
"""pooling"""
from
mindspore.ops
import
operations
as
P
from
mindspore.ops
import
functional
as
F
from
mindspore._checkparam
import
Validator
as
validator
from
...
import
context
from
..cell
import
Cell
from
..._checkparam
import
Rel
from
..._checkparam
import
ParamValidator
class
_PoolNd
(
Cell
):
...
...
@@ -208,3 +211,81 @@ class AvgPool2d(_PoolNd):
def
construct
(
self
,
x
):
return
self
.
avg_pool
(
x
)
class
AvgPool1d
(
_PoolNd
):
r
"""
Average pooling for temporal data.
Applies a 1D average pooling over an input Tensor which can be regarded as a composition of 1D input planes.
Typically the input is of shape :math:`(N_{in}, C_{in}, H_{in}, W_{in})`, AvgPool1d outputs
regional average in the :math:`(W_{in})`-dimension. Given kernel size
:math:`ks = w_{ker}` and stride :math:`s = s_0`, the operation is as follows.
.. math::
\text{output}(N_i, C_j, h_k, w) = \frac{1}{w_{ker}} \sum_{n=0}^{w_{ker}-1}
\text{input}(N_i, C_j, h_k, s_0 \times w + n)
Note:
pad_mode for training only supports "same" and "valid".
Args:
kernel_size (int): The size of kernel window used to take the average value, Default: 1.
stride (int): The distance of kernel moving, an int number that represents
the width of movement is strides, Default: 1.
pad_mode (str): The optional values for pad mode, is "same" or "valid", not case sensitive.
Default: "valid".
- same: Adopts the way of completion. Output height and width will be the same as
the input. Total number of padding will be calculated for horizontal and vertical
direction and evenly distributed to top and bottom, left and right if possible.
Otherwise, the last extra padding will be done from the bottom and the right side.
- valid: Adopts the way of discarding. The possibly largest height and width of output
will be return without padding. Extra pixels will be discarded.
Inputs:
- **input** (Tensor) - Tensor of shape :math:`(N, C_{in}, H_{in}, W_{in})`.
Outputs:
Tensor of shape :math:`(N, C_{out}, H_{out}, W_{out})`.
Examples:
>>> pool = nn.AvgPool1d(kernel_size=3, strides=1)
>>> x = Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), mindspore.float32)
>>> output = pool(x)
>>> output.shape()
(1, 2, 4, 2)
"""
def
__init__
(
self
,
kernel_size
=
1
,
stride
=
1
,
pad_mode
=
"valid"
):
super
(
AvgPool1d
,
self
).
__init__
(
kernel_size
,
stride
,
pad_mode
)
ParamValidator
.
check_type
(
'kernel_size'
,
kernel_size
,
[
int
,])
ParamValidator
.
check_type
(
'stride'
,
stride
,
[
int
,])
self
.
pad_mode
=
ParamValidator
.
check_string
(
'pad_mode'
,
pad_mode
.
upper
(),
[
'VALID'
,
'SAME'
])
ParamValidator
.
check_integer
(
"kernel_size"
,
kernel_size
,
1
,
Rel
.
GE
)
ParamValidator
.
check_integer
(
"stride"
,
stride
,
1
,
Rel
.
GE
)
self
.
kernel_size
=
(
1
,
kernel_size
)
self
.
stride
=
(
1
,
stride
)
self
.
avg_pool
=
P
.
AvgPool
(
ksize
=
self
.
kernel_size
,
strides
=
self
.
stride
,
padding
=
self
.
pad_mode
)
self
.
shape
=
F
.
shape
self
.
reduce_mean
=
P
.
ReduceMean
(
keep_dims
=
True
)
self
.
slice
=
P
.
Slice
()
def
construct
(
self
,
x
):
batch
,
channel
,
high
,
width
=
self
.
shape
(
x
)
if
width
==
self
.
kernel_size
[
1
]:
x
=
self
.
reduce_mean
(
x
,
3
)
elif
width
-
self
.
kernel_size
[
1
]
<
self
.
stride
[
1
]:
x
=
self
.
slice
(
x
,
(
0
,
0
,
0
,
0
),
(
batch
,
channel
,
high
,
self
.
kernel_size
[
1
]))
x
=
self
.
reduce_mean
(
x
,
3
)
else
:
x
=
self
.
avg_pool
(
x
)
return
x
tests/ut/python/nn/test_pooling.py
浏览文件 @
67057d13
...
...
@@ -56,3 +56,19 @@ def test_compile_max():
net
=
MaxNet
(
3
,
stride
=
1
,
padding
=
0
)
x
=
Tensor
(
np
.
random
.
randint
(
0
,
255
,
[
1
,
3
,
6
,
6
]).
astype
(
np
.
float32
))
_executor
.
compile
(
net
,
x
)
class
Avg1dNet
(
nn
.
Cell
):
def
__init__
(
self
,
kernel_size
,
stride
=
None
):
super
(
Avg1dNet
,
self
).
__init__
()
self
.
avg1d
=
nn
.
AvgPool1d
(
kernel_size
,
stride
)
def
construct
(
self
,
x
):
return
self
.
avg1d
(
x
)
def
test_avg1d
():
net
=
Avg1dNet
(
3
,
1
)
input
=
Tensor
(
np
.
random
.
randint
(
0
,
255
,
[
1
,
3
,
6
,
6
]).
astype
(
np
.
float32
))
_executor
.
compile
(
net
,
input
)
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录