Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
机器未来
Paddle
提交
26e9c4e2
P
Paddle
项目概览
机器未来
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
26e9c4e2
编写于
6月 05, 2017
作者:
D
dzhwinter
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
"add vector alias to make name clear"
上级
b4aa0eca
变更
10
隐藏空白更改
内联
并排
Showing
10 changed file
with
64 addition
and
59 deletion
+64
-59
paddle/optimizer/CMakeLists.txt
paddle/optimizer/CMakeLists.txt
+6
-8
paddle/optimizer/adadelta_optimizer.cc
paddle/optimizer/adadelta_optimizer.cc
+16
-13
paddle/optimizer/adadelta_optimizer.h
paddle/optimizer/adadelta_optimizer.h
+1
-1
paddle/optimizer/adagrad_optimizer.cc
paddle/optimizer/adagrad_optimizer.cc
+13
-13
paddle/optimizer/adagrad_optimizer.h
paddle/optimizer/adagrad_optimizer.h
+1
-1
paddle/optimizer/adam_optimizer.cc
paddle/optimizer/adam_optimizer.cc
+14
-11
paddle/optimizer/adam_optimizer.h
paddle/optimizer/adam_optimizer.h
+1
-1
paddle/optimizer/parameter_optimizer.h
paddle/optimizer/parameter_optimizer.h
+1
-1
paddle/optimizer/sgd_optimizer.h
paddle/optimizer/sgd_optimizer.h
+1
-1
paddle/optimizer/sgd_optmizer.cc
paddle/optimizer/sgd_optmizer.cc
+10
-9
未找到文件。
paddle/optimizer/CMakeLists.txt
浏览文件 @
26e9c4e2
include_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
include_directories
(
${
CMAKE_CURRENT_BINARY_DIR
}
)
set
(
OPITMIZER_SRCS
set
(
OPITMIZER_SRCS
#
adadelta_optimizer.cc
adadelta_optimizer.cc
#
adagrad_optimizer.cc
adagrad_optimizer.cc
#
adam_optimizer.cc
adam_optimizer.cc
optimizer.cc
optimizer.cc
parameter_optimizer.cc
parameter_optimizer.cc
sgd_optmizer.cc
sgd_optmizer.cc
regularizer.cc
)
)
set
(
OPITMIZER_Headers
set
(
OPITMIZER_Headers
#
adadelta_optimizer.h
adadelta_optimizer.h
#
adagrad_optimizer.h
adagrad_optimizer.h
#
adam_optimizer.h
adam_optimizer.h
lr_policy.h
lr_policy.h
optimizer.h
optimizer.h
parameter_optimizer.h
parameter_optimizer.h
regularizer.h
sgd_optimizer.h
sgd_optimizer.h
Tensor.h
Tensor.h
)
)
...
...
paddle/optimizer/adadelta_optimizer.cc
浏览文件 @
26e9c4e2
#include "adadelta_optimizer.h"
#include "adadelta_optimizer.h"
#include <algorithm>
#include <algorithm>
#include <cmath>
namespace
paddle
{
namespace
paddle
{
namespace
optimizer
{
namespace
optimizer
{
...
@@ -7,28 +8,30 @@ namespace optimizer {
...
@@ -7,28 +8,30 @@ namespace optimizer {
void
AdadeltaOptimizer
::
set_weight
(
Tensor
*
p
)
{
void
AdadeltaOptimizer
::
set_weight
(
Tensor
*
p
)
{
size_t
size
=
p
->
size
();
size_t
size
=
p
->
size
();
real
*
gptr
=
new
real
[
size
];
real
*
gptr
=
new
real
[
size
];
accum_gradient
=
Tensor
(
gptr
,
size
);
accum_gradient
=
new
Tensor
(
gptr
,
size
);
real
*
dptr
=
new
real
[
size
];
real
*
dptr
=
new
real
[
size
];
accum_delta
=
Tensor
(
dptr
,
size
);
accum_delta
=
new
Tensor
(
dptr
,
size
);
real
*
dptr_current
=
new
real
[
size
];
real
*
dptr_current
=
new
real
[
size
];
update_delta
=
Tensor
(
dptr_current
,
size
);
update_delta
=
new
Tensor
(
dptr_current
,
size
);
}
}
void
AdadeltaOptimizer
::
update
(
const
Tensor
&
gradient
)
{
void
AdadeltaOptimizer
::
update
(
const
Tensor
*
gradient
)
{
num_sample_passed
+=
1
;
num_sample_passed
+=
1
;
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
for
(
size_t
i
=
0
;
i
<
parameter_
->
size
();
++
i
)
{
Tensor
&
param
=
*
parameter_
;
accum_gradient
[
i
]
=
const
Tensor
&
grad
=
*
gradient
;
rho
*
accum_gradient
[
i
]
+
(
1.0
-
rho
)
*
gradient
[
i
]
*
gradient
[
i
];
Tensor
&
accum_g
=
*
accum_gradient
;
Tensor
&
accum_d
=
*
accum_delta
;
Tensor
&
update_d
=
*
update_delta
;
for
(
size_t
i
=
0
;
i
<
param
.
size
();
++
i
)
{
accum_g
[
i
]
=
rho
*
accum_g
[
i
]
+
(
1.0
-
rho
)
*
grad
[
i
]
*
grad
[
i
];
update_d
elta
[
i
]
=
std
::
sqrt
(
accum_delta
[
i
]
+
epsilon
)
/
update_d
[
i
]
=
std
::
sqrt
(
accum_d
[
i
]
+
epsilon
)
/
std
::
sqrt
(
accum_gradient
[
i
]
+
epsilon
)
*
gradient
[
i
];
std
::
sqrt
(
accum_g
[
i
]
+
epsilon
)
*
grad
[
i
];
accum_delta
[
i
]
=
accum_d
[
i
]
=
rho
*
accum_d
[
i
]
+
(
1.0
-
rho
)
*
update_d
[
i
]
*
update_d
[
i
];
rho
*
accum_delta
[
i
]
+
(
1.0
-
rho
)
*
update_delta
[
i
]
*
update_delta
[
i
];
parameter_
[
i
]
-=
param
[
i
]
-=
learning_rate
*
update_d
[
i
]
+
learning_rate
*
decay
*
param
[
i
];
learning_rate
*
update_delta
[
i
]
+
learning_rate
*
decay
*
parameter_
[
i
];
}
}
}
}
}
// namespace optimizer
}
// namespace optimizer
...
...
paddle/optimizer/adadelta_optimizer.h
浏览文件 @
26e9c4e2
...
@@ -19,7 +19,7 @@ public:
...
@@ -19,7 +19,7 @@ public:
if
(
accum_delta
)
delete
accum_delta
;
if
(
accum_delta
)
delete
accum_delta
;
if
(
update_delta
)
delete
update_delta
;
if
(
update_delta
)
delete
update_delta
;
}
}
void
update
(
const
Tensor
&
gradient
);
void
update
(
const
Tensor
*
gradient
);
void
set_weight
(
Tensor
*
p
);
void
set_weight
(
Tensor
*
p
);
real
*
get_weight
()
const
;
real
*
get_weight
()
const
;
...
...
paddle/optimizer/adagrad_optimizer.cc
浏览文件 @
26e9c4e2
#include <cmath>
#include "adagrad_optimizer.h"
#include "adagrad_optimizer.h"
namespace
paddle
{
namespace
paddle
{
namespace
optimizer
{
namespace
optimizer
{
void
AdagradOptimizer
::
set_weight
(
Tensor
*
p
)
{
void
AdagradOptimizer
::
set_weight
(
Tensor
*
p
)
{
size_t
size
=
p
->
width
();
size_t
size
=
p
->
size
();
real
*
gptr
=
new
real
[
size
];
real
*
gptr
=
new
real
[
size
];
accum_gradient
=
Tensor
(
gptr
,
size
);
accum_gradient
=
new
Tensor
(
gptr
,
size
);
real
*
dptr
=
new
real
[
size
];
accum_delta
=
Tensor
(
dtpr
,
size
);
real
*
dptr_current
=
new
real
[
size
];
update_delta
=
Tensor
(
dptr_current
,
size
);
}
}
void
AdagradOptimizer
::
update
(
const
Tensor
&
gradient
)
{
void
AdagradOptimizer
::
update
(
const
Tensor
*
gradient
)
{
num_sample_passed
+=
1
;
num_sample_passed
+=
1
;
double
learning_rate
=
lr_policy
->
get_learning_rate
();
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
for
(
size_t
i
=
0
;
i
<
parameter_
.
size
();
++
i
)
{
Tensor
&
param
=
*
parameter_
;
accum_gradient
[
i
]
+=
gradient
[
i
]
*
gradient
[
i
];
const
Tensor
&
grad
=
*
gradient
;
parameter_
[
i
]
+=
Tensor
&
accum_g
=
*
accum_gradient
;
learning_rate
*
(
gradient
[
i
]
/
std
::
sqrt
(
accum_gradient
[
i
]
+
epsilon
)
+
for
(
size_t
i
=
0
;
i
<
param
.
size
();
++
i
)
{
decay
*
parameter_
[
i
]);
accum_g
[
i
]
+=
grad
[
i
]
*
grad
[
i
];
param
[
i
]
+=
learning_rate
*
grad
[
i
]
/
std
::
sqrt
(
accum_g
[
i
]
+
epsilon
)
+
learning_rate
*
decay
*
param
[
i
];
}
}
}
}
...
...
paddle/optimizer/adagrad_optimizer.h
浏览文件 @
26e9c4e2
...
@@ -13,7 +13,7 @@ public:
...
@@ -13,7 +13,7 @@ public:
~
AdagradOptimizer
()
{
~
AdagradOptimizer
()
{
if
(
accum_gradient
)
delete
accum_gradient
;
if
(
accum_gradient
)
delete
accum_gradient
;
}
}
void
update
(
const
Tensor
&
gradient
);
void
update
(
const
Tensor
*
gradient
);
void
set_weight
(
Tensor
*
p
);
void
set_weight
(
Tensor
*
p
);
real
*
get_weight
()
const
;
real
*
get_weight
()
const
;
...
...
paddle/optimizer/adam_optimizer.cc
浏览文件 @
26e9c4e2
#include "adam_optimizer.h"
#include "adam_optimizer.h"
#include <cmath>
namespace
paddle
{
namespace
paddle
{
namespace
optimizer
{
namespace
optimizer
{
void
AdamOptimizer
::
set_weight
(
Tensor
*
p
)
{
void
AdamOptimizer
::
set_weight
(
Tensor
*
p
)
{
size_t
size
=
p
->
width
();
size_t
size
=
p
->
size
();
real
*
mptr
=
new
real
[
size
];
real
*
mptr
=
new
real
[
size
];
momentums_
=
Tensor
(
mptr
,
size
);
momentums_
=
new
Tensor
(
mptr
,
size
);
real
*
vptr
=
new
real
[
size
];
real
*
vptr
=
new
real
[
size
];
velocitys_
=
Tensor
(
vtp
r
,
size
);
velocitys_
=
new
Tensor
(
vpt
r
,
size
);
}
}
void
AdamOptimizer
::
update
(
const
Tensor
&
gradient
)
{
void
AdamOptimizer
::
update
(
const
Tensor
*
gradient
)
{
num_sample_passed
+=
1
;
num_sample_passed
+=
1
;
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
double
coef1
=
1.0
-
std
::
pow
(
beta_1
,
num_sample_passed
);
double
coef1
=
1.0
-
std
::
pow
(
beta_1
,
num_sample_passed
);
double
coef2
=
1.0
-
std
::
pow
(
beta_2
,
num_sample_passed
);
double
coef2
=
1.0
-
std
::
pow
(
beta_2
,
num_sample_passed
);
learning_rate
*=
std
::
sqrt
(
coef2
)
/
coef1
;
learning_rate
*=
std
::
sqrt
(
coef2
)
/
coef1
;
for
(
size_t
i
=
0
;
i
<
parameter_
->
size
();
++
i
)
{
Tensor
&
param
=
*
parameter_
;
momentums_
[
i
]
=
beta_1
*
momentums_
[
i
]
+
(
1.0
-
beta_1
)
*
gradient
[
i
];
const
Tensor
&
grad
=
*
gradient
;
velocitys_
[
i
]
=
Tensor
&
m
=
*
momentums_
;
beta_2
*
velocitys_
[
i
]
+
(
1.0
-
beta_2
)
*
gradient
[
i
]
*
gradient
[
i
];
Tensor
&
v
=
*
velocitys_
;
parameter_
[
i
]
-=
for
(
size_t
i
=
0
;
i
<
param
.
size
();
++
i
)
{
learning_rate
*
(
momentums_
[
i
]
/
std
::
sqrt
(
velocitys_
[
i
]
+
epsilon
)
+
m
[
i
]
=
beta_1
*
m
[
i
]
+
(
1.0
-
beta_1
)
*
grad
[
i
];
decay
*
parameter_
[
i
]);
v
[
i
]
=
beta_2
*
v
[
i
]
+
(
1.0
-
beta_2
)
*
grad
[
i
]
*
grad
[
i
];
param
[
i
]
-=
learning_rate
*
(
m
[
i
]
/
std
::
sqrt
(
v
[
i
]
+
epsilon
)
+
decay
*
param
[
i
]);
}
}
}
}
}
// namespace optimizer
}
// namespace optimizer
...
...
paddle/optimizer/adam_optimizer.h
浏览文件 @
26e9c4e2
...
@@ -19,7 +19,7 @@ public:
...
@@ -19,7 +19,7 @@ public:
if
(
momentums_
)
delete
momentums_
;
if
(
momentums_
)
delete
momentums_
;
if
(
velocitys_
)
delete
velocitys_
;
if
(
velocitys_
)
delete
velocitys_
;
}
}
void
update
(
const
Tensor
&
gradient
);
void
update
(
const
Tensor
*
gradient
);
void
set_weight
(
Tensor
*
p
);
void
set_weight
(
Tensor
*
p
);
real
*
get_weight
()
const
;
real
*
get_weight
()
const
;
...
...
paddle/optimizer/parameter_optimizer.h
浏览文件 @
26e9c4e2
...
@@ -24,7 +24,7 @@ public:
...
@@ -24,7 +24,7 @@ public:
virtual
~
ParameterOptimizer
()
{
delete
parameter_
;
};
virtual
~
ParameterOptimizer
()
{
delete
parameter_
;
};
static
ParameterOptimizer
*
create
(
const
::
std
::
string
&
config_proto
);
static
ParameterOptimizer
*
create
(
const
::
std
::
string
&
config_proto
);
virtual
void
update
(
const
Tensor
&
gradient
)
=
0
;
virtual
void
update
(
const
Tensor
*
gradient
)
=
0
;
virtual
real
*
get_weight
()
const
;
virtual
real
*
get_weight
()
const
;
virtual
void
set_weight
(
Tensor
*
parameter
);
virtual
void
set_weight
(
Tensor
*
parameter
);
...
...
paddle/optimizer/sgd_optimizer.h
浏览文件 @
26e9c4e2
...
@@ -15,7 +15,7 @@ public:
...
@@ -15,7 +15,7 @@ public:
SGDOptimizer
(
double
m
,
double
d
,
bool
n
,
BaseLr
*
lr
)
SGDOptimizer
(
double
m
,
double
d
,
bool
n
,
BaseLr
*
lr
)
:
ParameterOptimizer
(
lr
),
momentum
(
m
),
decay
(
d
),
nesterov
(
n
)
{}
:
ParameterOptimizer
(
lr
),
momentum
(
m
),
decay
(
d
),
nesterov
(
n
)
{}
virtual
~
SGDOptimizer
()
{
delete
momentums_
;
}
virtual
~
SGDOptimizer
()
{
delete
momentums_
;
}
void
update
(
const
Tensor
&
gradient
);
void
update
(
const
Tensor
*
gradient
);
void
set_weight
(
Tensor
*
p
);
void
set_weight
(
Tensor
*
p
);
real
*
get_weight
()
const
;
real
*
get_weight
()
const
;
...
...
paddle/optimizer/sgd_optmizer.cc
浏览文件 @
26e9c4e2
...
@@ -13,24 +13,25 @@ void SGDOptimizer::set_weight(Tensor *p) {
...
@@ -13,24 +13,25 @@ void SGDOptimizer::set_weight(Tensor *p) {
}
}
}
}
void
SGDOptimizer
::
update
(
const
Tensor
&
gradient
)
{
void
SGDOptimizer
::
update
(
const
Tensor
*
gradient
)
{
num_sample_passed
+=
1
;
num_sample_passed
+=
1
;
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
double
learning_rate
=
lr_policy
->
get_learning_rate
(
num_sample_passed
);
real
velocity
=
0.0
;
real
velocity
=
0.0
;
Tensor
&
param
=
*
parameter_
;
Tensor
&
param
=
*
parameter_
;
for
(
size_t
i
=
0
;
i
<
parameter_
->
size
();
++
i
)
{
const
Tensor
&
grad
=
*
gradient
;
Tensor
&
m
=
*
momentums_
;
for
(
size_t
i
=
0
;
i
<
param
.
size
();
++
i
)
{
if
(
momentum
==
0.0
)
{
if
(
momentum
==
0.0
)
{
velocity
=
velocity
=
-
learning_rate
*
grad
[
i
]
-
learning_rate
*
decay
*
param
[
i
];
-
learning_rate
*
gradient
[
i
]
-
learning_rate
*
decay
*
parameter_
[
i
];
}
else
{
}
else
{
m
omentums_
[
i
]
=
momentum
*
momentums_
[
i
]
-
learning_rate
*
gradient
[
i
]
-
m
[
i
]
=
momentum
*
m
[
i
]
-
learning_rate
*
grad
[
i
]
-
learning_rate
*
decay
*
parameter_
[
i
];
learning_rate
*
decay
*
param
[
i
];
velocity
=
m
omentums_
[
i
];
velocity
=
m
[
i
];
}
}
if
(
nesterov
)
{
if
(
nesterov
)
{
param
eter_
[
i
]
+=
momentum
*
velocity
-
learning_rate
*
gradient
[
i
];
param
[
i
]
+=
momentum
*
velocity
-
learning_rate
*
grad
[
i
];
}
else
{
}
else
{
param
eter_
[
i
]
+=
velocity
;
param
[
i
]
+=
velocity
;
}
}
}
}
}
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录