Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
c1a69e3e
P
Paddle
项目概览
BaiXuePrincess
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
c1a69e3e
编写于
3月 01, 2019
作者:
D
dengkaipeng
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refine doc. test=develop
上级
65d375a0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
53 addition
and
41 deletion
+53
-41
paddle/fluid/operators/spectral_norm_op.cc
paddle/fluid/operators/spectral_norm_op.cc
+29
-21
python/paddle/fluid/layers/nn.py
python/paddle/fluid/layers/nn.py
+24
-20
未找到文件。
paddle/fluid/operators/spectral_norm_op.cc
浏览文件 @
c1a69e3e
...
@@ -78,7 +78,7 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -78,7 +78,7 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
void
Make
()
override
{
void
Make
()
override
{
AddInput
(
"Weight"
,
AddInput
(
"Weight"
,
"The input weight tensor of spectral_norm operator, "
"The input weight tensor of spectral_norm operator, "
"This can be a 2-D, 3-D, 4-D, 5-D tensor which is the"
"This can be a 2-D, 3-D, 4-D, 5-D tensor which is the
"
"weights of fc, conv1d, conv2d, conv3d layer."
);
"weights of fc, conv1d, conv2d, conv3d layer."
);
AddInput
(
"U"
,
AddInput
(
"U"
,
"The weight_u tensor of spectral_norm operator, "
"The weight_u tensor of spectral_norm operator, "
...
@@ -90,29 +90,29 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -90,29 +90,29 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
"be in shape [C, 1]."
);
"be in shape [C, 1]."
);
AddInput
(
"V"
,
AddInput
(
"V"
,
"The weight_v tensor of spectral_norm operator, "
"The weight_v tensor of spectral_norm operator, "
"This can be a 1-D tensor in shape [W, 1],"
"This can be a 1-D tensor in shape [W, 1],
"
"W is the 2nd dimentions of Weight after reshape"
"W is the 2nd dimentions of Weight after reshape
"
"corresponding by Attr(dim). As for Attr(dim) = 1"
"corresponding by Attr(dim). As for Attr(dim) = 1
"
"in conv2d layer with weight shape [M, C, K1, K2]"
"in conv2d layer with weight shape [M, C, K1, K2]
"
"Weight will be reshape to [C, M*K1*K2], V will"
"Weight will be reshape to [C, M*K1*K2], V will
"
"be in shape [M*K1*K2, 1]."
);
"be in shape [M*K1*K2, 1]."
);
AddOutput
(
"Out"
,
AddOutput
(
"Out"
,
"The output weight tensor of spectral_norm operator, "
"The output weight tensor of spectral_norm operator, "
"This tensor is in same shape with Input(Weight)."
);
"This tensor is in same shape with Input(Weight)."
);
AddAttr
<
int
>
(
"dim"
,
AddAttr
<
int
>
(
"dim"
,
"dimension corresponding to number of outputs,"
"dimension corresponding to number of outputs,
"
"it should be set as 0 if Input(Weight) is the"
"it should be set as 0 if Input(Weight) is the
"
"weight of fc layer, and should be set as 1 if"
"weight of fc layer, and should be set as 1 if
"
"Input(Weight) is the weight of conv layer,"
"Input(Weight) is the weight of conv layer,
"
"default
is
0."
)
"default 0."
)
.
SetDefault
(
0
);
.
SetDefault
(
0
);
AddAttr
<
int
>
(
"power_iters"
,
AddAttr
<
int
>
(
"power_iters"
,
"number of power iterations to calculate"
"number of power iterations to calculate
"
"spectral norm, default
is
1."
)
"spectral norm, default 1."
)
.
SetDefault
(
1
);
.
SetDefault
(
1
);
AddAttr
<
float
>
(
"eps"
,
AddAttr
<
float
>
(
"eps"
,
"epsilob for numerical stability in"
"epsilob for numerical stability in
"
"calculating norms"
)
"calculating norms"
)
.
SetDefault
(
1e-12
);
.
SetDefault
(
1e-12
);
...
@@ -126,20 +126,28 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
...
@@ -126,20 +126,28 @@ class SpectralNormOpMaker : public framework::OpProtoAndCheckerMaker {
with spectral normalize value.
with spectral normalize value.
For spectral normalization calculations, we rescaling weight
For spectral normalization calculations, we rescaling weight
tensor with
\sigma, while \sigma{\mathbf{W}}
is
tensor with
:math:`\sigma`, while :math:`\sigma{\mathbf{W}}`
is
\sigma(\mathbf{W}) = \max_{\mathbf{h}: \mathbf{h} \ne 0} \dfrac{\|\mathbf{W} \mathbf{h}\|_2}{\|\mathbf{h}\|_2}
$$\sigma(\mathbf{W}) = \max_{\mathbf{h}: \mathbf{h} \ne 0} \\frac{\|\mathbf{W} \mathbf{h}\|_2}{\|\mathbf{h}\|_2}$$
We calculate
\sigma{\mathbf{W}}
through power iterations as
We calculate
:math:`\sigma{\mathbf{W}}`
through power iterations as
$$
\mathbf{v} = \mathbf{W}^{T} \mathbf{u}
\mathbf{v} = \mathbf{W}^{T} \mathbf{u}
\mathbf{v} = \frac{\mathbf{v}}{\|\mathbf{v}\|_2}
$$
$$
\mathbf{v} = \\frac{\mathbf{v}}{\|\mathbf{v}\|_2}
$$
$$
\mathbf{u} = \mathbf{W}^{T} \mathbf{v}
\mathbf{u} = \mathbf{W}^{T} \mathbf{v}
\mathbf{u} = \frac{\mathbf{u}}{\|\mathbf{u}\|_2}
$$
$$
\mathbf{u} = \\frac{\mathbf{u}}{\|\mathbf{u}\|_2}
$$
And
\sigma
should be
And
:math:`\sigma`
should be
\sigma{\mathbf{W}} = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
$$\sigma{\mathbf{W}} = \mathbf{u}^{T} \mathbf{W} \mathbf{v}$$
For details of spectral normalization, please refer to paper:
For details of spectral normalization, please refer to paper:
`Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
`Spectral Normalization <https://arxiv.org/abs/1802.05957>`_ .
...
...
python/paddle/fluid/layers/nn.py
浏览文件 @
c1a69e3e
...
@@ -3356,34 +3356,38 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None):
...
@@ -3356,34 +3356,38 @@ def spectral_norm(weight, dim=0, power_iters=1, eps=1e-12, name=None):
fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
fc, conv1d, conv2d, conv3d layers which should be 2-D, 3-D, 4-D, 5-D
Parameters. Calculations are showed as followings.
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 1:
Step 2:
Generate vector u in shape of [h], and v in shape of [w].
:attr:`power_iters` shoule be a positive interger, do following
While h is the attr:`dim`th dimension of the input weights,
calculations with U and V for :attr:`power_iters` rounds.
and w is the product result of remain dimensions.
Step 2:
.. math::
While attr:`power_iters` is a positive interger, do following
iteration calculations with u and v for attr:`power_iters`
\mathbf{v} :=
\\
frac{\mathbf{W}^{T} \mathbf{u}}{\|\mathbf{W}^{T} \mathbf{u}\|_2}
round.
\mathbf{v} = \mathbf{W}^{T} \mathbf{u}
\mathbf{u} :=
\\
frac{\mathbf{W}^{T} \mathbf{v}}{\|\mathbf{W}^{T} \mathbf{v}\|_2}
\mathbf{v} =
\f
rac{\mathbf{v}}{\|\mathbf{v}\|_2}
\mathbf{u} = \mathbf{W}^{T} \mathbf{v}
Step 3:
\mathbf{u} =
\f
rac{\mathbf{u}}{\|\mathbf{u}\|_2}
Calculate :math:`\sigma(\mathbf{W})` and scale weight values.
Step 3:
.. math::
Calculate \sigma{W} and scale weight values.
\sigma{\mathbf{W}} = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
\sigma(\mathbf{W}) = \mathbf{u}^{T} \mathbf{W} \mathbf{v}
\mathbf{W} :=
\f
rac{\mathbf{W}}{\sigma{\mathbf{W}}}
\mathbf{W} =
\\
frac{\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(int): ${dim_comment}
eps(${eps_type}): ${eps_comment}
power_iters(int): ${power_iters_comment}
eps(float): ${eps_comment}
name (str): The name of this layer. It is optional.
name (str): The name of this layer. It is optional.
Returns:
Returns:
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录