Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle
提交
12416a24
P
Paddle
项目概览
PaddlePaddle
/
Paddle
大约 1 年 前同步成功
通知
2299
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看板
提交
12416a24
编写于
2月 21, 2019
作者:
D
dengkaipeng
提交者:
ceci3
3月 06, 2019
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add doc and test_layers. test=develop
上级
63d322f0
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
92 addition
and
43 deletion
+92
-43
paddle/fluid/operators/spectral_norm_op.cc
paddle/fluid/operators/spectral_norm_op.cc
+24
-2
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+55
-41
python/paddle/fluid/tests/unittests/test_layers.py
python/paddle/fluid/tests/unittests/test_layers.py
+13
-0
未找到文件。
paddle/fluid/operators/spectral_norm_op.cc
浏览文件 @
12416a24
...
@@ -109,10 +109,32 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -109,10 +109,32 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
.
SetDefault
(
1e-12
);
.
SetDefault
(
1e-12
);
AddComment
(
R"DOC(
AddComment
(
R"DOC(
This operator samples input X to given output shape by using specified
This layer calculate the spectral normalize value of weight of
fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
tensor.
Spectral normalization stabilizes the training of critis in GANs
(Generative Adversarial Networks). This layers rescaling weight tensor
wiht spectral normalize value.
For spectral normalization calculations, we rescaling weight
tensor with \sigma, while \sigma{\mathbf{W}} is
\sigma(\mathbf{W}) = \max_{\mathbf{h}: \mathbf{h} \ne 0} \dfrac{\|\mathbf{W} \mathbf{h}\|_2}{\|\mathbf{h}\|_2}
We calculate \sigma{\mathbf{W}} through power iterations as
\mathbf{v} = \mathbf{W}^{T} \mathbf{u}
\mathbf{v} = \frac{\mathbf{v}}{\|\mathbf{v}\|_2}
\mathbf{u} = \mathbf{W}^{T} \mathbf{v}
\mathbf{u} = \frac{\mathbf{u}}{\|\mathbf{u}\|_2}
And \sigma should be
\sigma{\mathbf{W}} = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
For details of spectral normalization, please refer to paper:
`Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
)DOC"
);
)DOC"
);
}
}
};
};
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
12416a24
...
@@ -3349,28 +3349,42 @@ def group_norm(input,
...
@@ -3349,28 +3349,42 @@ def group_norm(input,
@
templatedoc
()
@
templatedoc
()
def
spectral_norm
(
weight
,
def
spectral_norm
(
weight
,
dim
=
0
,
power_iters
=
1
,
eps
=
1e-12
,
name
=
None
):
dim
=
0
,
power_iters
=
1
,
eps
=
1e-12
,
u_attr
=
None
,
v_attr
=
None
,
name
=
None
):
"""
"""
**Spectral Normalization Layer**
**Spectral Normalization Layer**
This layer calculate the spectral normalize value of weight parameters of
fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
Parameters. Calculations are showed as followings.
.. code-block:: text
Step 1:
Generate vector u in shape of [h], and v in shape of [w].
While h is the attr:`dim`th dimension of the input weights,
and w is the product result of remain dimensions.
Step 2:
While attr:`power_iters` is a positive interger, do following
iteration calculations with u and v for attr:`power_iters`
round.
\mathbf{v} = \mathbf{W}^{T} \mathbf{u}
\mathbf{v} =
\f
rac{\mathbf{v}}{\|\mathbf{v}\|_2}
\mathbf{u} = \mathbf{W}^{T} \mathbf{v}
\mathbf{u} =
\f
rac{\mathbf{u}}{\|\mathbf{u}\|_2}
Step 3:
Calculate \sigma{W} and scale weight values.
\sigma{\mathbf{W}} = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
\mathbf{W} :=
\f
rac{\mathbf{W}}{\sigma{\mathbf{W}}}
Refer to `Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
Refer to `Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
Args:
Args:
weight(${weight_type}): ${weight_comment}
weight(${weight_type}): ${weight_comment}
dim(${dim_type}): ${dim_comment}
dim(${dim_type}): ${dim_comment}
eps(${eps_type}): ${eps_comment}
eps(${eps_type}): ${eps_comment}
u_attr(ParamAttr|None): The parameter attribute for vector u in
spectral calculatings, set None to use default attribute, which
generates random values in normal distribution N(0, 1). Default: None.
v_attr(ParamAttr|None): The parameter attribute for vector v in
spectral calculatings, set None to use default attribute, which
generates random values in normal distribution N(0, 1). Default: None.
name (str): The name of this layer. It is optional.
name (str): The name of this layer. It is optional.
Returns:
Returns:
...
@@ -3383,43 +3397,43 @@ def spectral_norm(weight,
...
@@ -3383,43 +3397,43 @@ def spectral_norm(weight,
>>> x = fluid.layers.spectral_norm(weight=data, dim=1, power_iters=2)
>>> x = fluid.layers.spectral_norm(weight=data, dim=1, power_iters=2)
"""
"""
helper
=
LayerHelper
(
'spectral_norm'
,
**
locals
())
helper
=
LayerHelper
(
'spectral_norm'
,
**
locals
())
dtype
=
helper
.
input_dtype
()
dtype
=
weight
.
dtype
# create intput and parameters
# create intput and parameters
inputs
=
{
'Weight'
:
weight
}
inputs
=
{
'Weight'
:
weight
}
input_shape
=
input
.
shape
input_shape
=
weight
.
shape
if
data_layout
!=
'NCHW'
:
h
=
input_shape
[
dim
]
raise
ValueError
(
"unsupported data layout:"
+
data_layout
)
w
=
np
.
prod
(
input_shape
)
//
h
param_shape
=
[
input_shape
[
1
]]
if
param_attr
:
u
=
helper
.
create_parameter
(
scale
=
helper
.
create_parameter
(
attr
=
ParamAttr
(),
attr
=
helper
.
param_attr
,
shape
=
[
h
],
shape
=
param_shape
,
dtype
=
dtype
,
dtype
=
dtype
,
default_initializer
=
Constant
(
1.0
))
default_initializer
=
Normal
(
0.
,
1.
))
inputs
[
'Scale'
]
=
scale
u
.
stop_gradient
=
True
if
bias_attr
:
inputs
[
'U'
]
=
u
bias
=
helper
.
create_parameter
(
v
=
helper
.
create_parameter
(
attr
=
helper
.
bias_attr
,
shape
=
param_shape
,
dtype
=
dtype
,
is_bias
=
True
)
attr
=
ParamAttr
(),
inputs
[
'Bias'
]
=
bias
shape
=
[
w
],
dtype
=
dtype
,
default_initializer
=
Normal
(
0.
,
1.
))
inputs
[
'V'
]
=
v
v
.
stop_gradient
=
True
# create output
# create output
mean_out
=
helper
.
create_variable
(
dtype
=
dtype
,
stop_gradient
=
True
)
out
=
helper
.
create_variable
(
dtype
=
dtype
)
variance_out
=
helper
.
create_variable
(
dtype
=
dtype
,
stop_gradient
=
True
)
group_norm_out
=
helper
.
create_variable
(
dtype
=
dtype
)
helper
.
append_op
(
helper
.
append_op
(
type
=
"
group
_norm"
,
type
=
"
spectral
_norm"
,
inputs
=
inputs
,
inputs
=
inputs
,
outputs
=
{
outputs
=
{
"Out"
:
out
,
},
"Y"
:
group_norm_out
,
attrs
=
{
"Mean"
:
mean_out
,
"dim"
:
dim
,
"Variance"
:
variance_out
,
"power_iters"
:
power_iters
,
},
"eps"
:
eps
,
attrs
=
{
"epsilon"
:
epsilon
,
})
"groups"
:
groups
})
return
helper
.
append_activation
(
group_norm_out
)
return
out
def
conv2d_transpose
(
input
,
def
conv2d_transpose
(
input
,
...
...
python/paddle/fluid/tests/unittests/test_layers.py
浏览文件 @
12416a24
...
@@ -1035,6 +1035,19 @@ class TestBook(unittest.TestCase):
...
@@ -1035,6 +1035,19 @@ class TestBook(unittest.TestCase):
print
(
str
(
program
))
print
(
str
(
program
))
def
test_spectral_norm
(
self
):
program
=
Program
()
with
program_guard
(
program
):
weight
=
layers
.
data
(
name
=
'weight'
,
shape
=
[
2
,
3
,
32
,
32
],
dtype
=
"float32"
,
append_batch_size
=
False
)
out
=
layers
.
spectral_norm
(
weight
,
dim
=
1
,
power_iters
=
1
)
self
.
assertIsNotNone
(
out
)
print
(
str
(
program
))
def
test_shuffle_channel
(
self
):
def
test_shuffle_channel
(
self
):
program
=
Program
()
program
=
Program
()
with
program_guard
(
program
):
with
program_guard
(
program
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录