Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
urhero
Paddle
提交
8b4cbcfc
P
Paddle
项目概览
urhero
/
Paddle
与 Fork 源项目一致
Fork自
PaddlePaddle / Paddle
通知
1
Star
0
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看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8b4cbcfc
编写于
12月 20, 2016
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Start doing mnist_train_api
上级
06944ee1
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
112 addition
and
21 deletion
+112
-21
demo/mnist/api_train.py
demo/mnist/api_train.py
+28
-3
paddle/api/CMakeLists.txt
paddle/api/CMakeLists.txt
+1
-0
paddle/api/Paddle.swig
paddle/api/Paddle.swig
+2
-1
paddle/api/PaddleAPI.h
paddle/api/PaddleAPI.h
+20
-0
paddle/api/PaddleAPIPrivate.h
paddle/api/PaddleAPIPrivate.h
+25
-2
paddle/api/Parameter.cpp
paddle/api/Parameter.cpp
+1
-15
paddle/api/ParameterUpdater.cpp
paddle/api/ParameterUpdater.cpp
+35
-0
未找到文件。
demo/mnist/api_train.py
浏览文件 @
8b4cbcfc
import
py_paddle.swig_paddle
as
api
from
paddle.trainer.config_parser
import
parse_config
import
paddle.trainer.config_parser
import
numpy
as
np
def
init_parameter
(
network
):
assert
isinstance
(
network
,
api
.
GradientMachine
)
for
each_param
in
network
.
getParameters
():
assert
isinstance
(
each_param
,
api
.
Parameter
)
array
=
each_param
.
getBuf
(
api
.
PARAMETER_VALUE
).
toNumpyArrayInplace
()
assert
isinstance
(
array
,
np
.
ndarray
)
for
i
in
xrange
(
len
(
array
)):
array
[
i
]
=
np
.
random
.
uniform
(
-
1.0
,
1.0
)
def
main
():
api
.
initPaddle
(
"-use_gpu=false"
,
"-trainer_count=4"
)
# use 4 cpu cores
config
=
parse_config
(
'simple_mnist_network.py'
,
''
)
m
=
api
.
GradientMachine
.
createFromConfigProto
(
config
.
model_config
)
config
=
paddle
.
trainer
.
config_parser
.
parse_config
(
'simple_mnist_network.py'
,
''
)
opt_config
=
api
.
OptimizationConfig
.
createFromProto
(
config
.
opt_config
)
_temp_optimizer_
=
api
.
ParameterOptimizer
.
create
(
opt_config
)
enable_types
=
_temp_optimizer_
.
getParameterTypes
()
m
=
api
.
GradientMachine
.
createFromConfigProto
(
config
.
model_config
,
api
.
CREATE_MODE_NORMAL
,
enable_types
)
assert
isinstance
(
m
,
api
.
GradientMachine
)
init_parameter
(
network
=
m
)
updater
=
api
.
ParameterUpdater
.
createLocalUpdater
(
opt_config
)
assert
isinstance
(
updater
,
api
.
ParameterUpdater
)
updater
.
init
(
m
)
updater
.
startPass
()
if
__name__
==
'__main__'
:
...
...
paddle/api/CMakeLists.txt
浏览文件 @
8b4cbcfc
...
...
@@ -5,6 +5,7 @@ set(API_SOURCES
Matrix.cpp
Parameter.cpp
ParameterOptimizer.cpp
ParameterUpdater.cpp
SequenceGenerator.cpp
Trainer.cpp
Util.cpp
...
...
paddle/api/Paddle.swig
浏览文件 @
8b4cbcfc
...
...
@@ -174,6 +174,7 @@ namespace std {
%newobject Parameter::getConfig;
%newobject ParameterOptimizer::create;
%newobject ParameterOptimizer::needSpecialTraversal;
%newobject ParameterUpdater::createLocalUpdater;
%feature("director") UpdateCallback;
%feature("autodoc", 1); // To generate method stub, for code hint in ide
...
...
@@ -193,4 +194,4 @@ namespace std {
%ignore OptimizationConfigPrivate;
%ignore ParameterTraverseCallbackPrivate;
%include "utils/GlobalConstants.h"
%include "api/PaddleAPI.h"
\ No newline at end of file
%include "api/PaddleAPI.h"
paddle/api/PaddleAPI.h
浏览文件 @
8b4cbcfc
...
...
@@ -519,6 +519,7 @@ private:
friend
class
TrainerConfig
;
friend
class
ParameterOptimizer
;
friend
class
ParameterUpdater
;
friend
class
Trainer
;
};
...
...
@@ -557,6 +558,7 @@ private:
ParameterPrivate
*
m
;
friend
class
UpdateCallbackWrapper
;
friend
class
GradientMachine
;
friend
class
ParameterUpdater
;
};
struct
ModelConfigPrivate
;
...
...
@@ -772,6 +774,24 @@ private:
// Not to use c++ 11 init-list, so we use static var as function default arg.
static
std
::
vector
<
int
>
defaultParamTypes
;
friend
class
Trainer
;
friend
class
ParameterUpdater
;
};
struct
ParameterUpdaterPrivate
;
class
ParameterUpdater
{
private:
ParameterUpdater
();
public:
static
ParameterUpdater
*
createLocalUpdater
(
OptimizationConfig
*
config
);
~
ParameterUpdater
();
void
init
(
const
GradientMachine
&
gm
);
void
startPass
();
private:
ParameterUpdaterPrivate
*
m
;
};
struct
TrainerPrivate
;
...
...
paddle/api/PaddleAPIPrivate.h
浏览文件 @
8b4cbcfc
...
...
@@ -11,11 +11,13 @@ distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#pragma once
#include <memory>
#include "PaddleAPI.h"
#include "paddle/gserver/gradientmachines/GradientMachine.h"
#include "paddle/trainer/TrainerConfigHelper.h"
#
pragma once
#
include "paddle/parameter/ParameterUpdaterBase.h"
struct
GradientMachinePrivate
{
std
::
shared_ptr
<
paddle
::
GradientMachine
>
machine
;
...
...
@@ -65,3 +67,24 @@ struct ArgumentsPrivate {
return
*
(
std
::
shared_ptr
<
T
>*
)(
rawPtr
);
}
};
struct
ParameterUpdaterPrivate
{
std
::
unique_ptr
<
paddle
::
ParameterUpdater
>
updater
;
};
struct
ParameterPrivate
{
std
::
shared_ptr
<
paddle
::
Parameter
>
sharedPtr
;
paddle
::
Parameter
*
rawPtr
;
// rawPtr only used in ParameterUpdater,
// in other situation sharedPtr should
// contains value.
ParameterPrivate
()
:
sharedPtr
(
nullptr
),
rawPtr
(
nullptr
)
{}
paddle
::
Parameter
*
getPtr
()
{
if
(
sharedPtr
)
{
return
sharedPtr
.
get
();
}
else
{
return
rawPtr
;
}
}
};
paddle/api/Parameter.cpp
浏览文件 @
8b4cbcfc
...
...
@@ -14,21 +14,7 @@ limitations under the License. */
#include "paddle/parameter/Parameter.h"
#include "PaddleAPI.h"
struct
ParameterPrivate
{
std
::
shared_ptr
<
paddle
::
Parameter
>
sharedPtr
;
paddle
::
Parameter
*
rawPtr
;
ParameterPrivate
()
:
sharedPtr
(
nullptr
),
rawPtr
(
nullptr
)
{}
paddle
::
Parameter
*
getPtr
()
{
if
(
sharedPtr
)
{
return
sharedPtr
.
get
();
}
else
{
return
rawPtr
;
}
}
};
#include "PaddleAPIPrivate.h"
Parameter
::
Parameter
()
:
m
(
new
ParameterPrivate
())
{}
...
...
paddle/api/ParameterUpdater.cpp
0 → 100644
浏览文件 @
8b4cbcfc
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserve.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License. */
#include "PaddleAPI.h"
#include "PaddleAPIPrivate.h"
#include "paddle/trainer/ThreadParameterUpdater.h"
ParameterUpdater
::
ParameterUpdater
()
:
m
(
new
ParameterUpdaterPrivate
())
{}
ParameterUpdater
*
ParameterUpdater
::
createLocalUpdater
(
OptimizationConfig
*
config
)
{
auto
param
=
new
ParameterUpdater
();
param
->
m
->
updater
.
reset
(
new
paddle
::
SgdThreadUpdater
(
config
->
m
->
getConfig
()));
return
param
;
}
ParameterUpdater
::~
ParameterUpdater
()
{
delete
m
;
}
void
ParameterUpdater
::
init
(
const
GradientMachine
&
gm
)
{
m
->
updater
->
init
(
gm
.
m
->
machine
->
getParameters
());
}
void
ParameterUpdater
::
startPass
()
{
m
->
updater
->
startPass
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录