Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Crayon鑫
Paddle
提交
5b1f66fe
P
Paddle
项目概览
Crayon鑫
/
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看板
提交
5b1f66fe
编写于
1月 24, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Removed unused GradientMachine::create
上级
f4678331
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
0 addition
and
264 deletion
+0
-264
paddle/gserver/gradientmachines/GradientMachine.cpp
paddle/gserver/gradientmachines/GradientMachine.cpp
+0
-49
paddle/gserver/gradientmachines/GradientMachine.h
paddle/gserver/gradientmachines/GradientMachine.h
+0
-33
paddle/trainer/tests/CMakeLists.txt
paddle/trainer/tests/CMakeLists.txt
+0
-8
paddle/trainer/tests/test_Prediction.cpp
paddle/trainer/tests/test_Prediction.cpp
+0
-174
未找到文件。
paddle/gserver/gradientmachines/GradientMachine.cpp
浏览文件 @
5b1f66fe
...
@@ -60,55 +60,6 @@ GradientMachine* GradientMachine::create(
...
@@ -60,55 +60,6 @@ GradientMachine* GradientMachine::create(
return
nullptr
;
return
nullptr
;
}
}
GradientMachine
*
GradientMachine
::
create
(
const
std
::
string
&
modelFile
,
DataConfig
*
dataConfig
)
{
std
::
ifstream
is
(
modelFile
);
CHECK
(
is
)
<<
"Fail to open "
<<
modelFile
;
return
create
(
is
,
dataConfig
);
}
GradientMachine
*
GradientMachine
::
create
(
std
::
istream
&
is
,
DataConfig
*
dataConfig
)
{
TrainerConfig
trainerConfig
;
GradientMachine
*
ret
=
create
(
is
,
&
trainerConfig
);
if
(
dataConfig
&&
trainerConfig
.
has_data_config
())
{
*
dataConfig
=
trainerConfig
.
data_config
();
}
return
ret
;
}
GradientMachine
*
GradientMachine
::
create
(
const
std
::
string
&
modelFile
,
TrainerConfig
*
trainerConfig
)
{
std
::
ifstream
is
(
modelFile
);
CHECK
(
is
)
<<
"Fail to open "
<<
modelFile
;
return
create
(
is
,
trainerConfig
);
}
GradientMachine
*
GradientMachine
::
create
(
std
::
istream
&
is
,
TrainerConfig
*
trainerConfig
)
{
TrainerConfig
trainerConfigTemp
;
int64_t
size
;
CHECK
(
is
.
read
((
char
*
)
&
size
,
sizeof
(
size
)))
<<
"Fail to read "
;
std
::
string
buf
;
buf
.
resize
(
size
);
CHECK
(
is
.
read
(
&
buf
[
0
],
size
))
<<
"Fail to read "
;
CHECK
(
trainerConfigTemp
.
ParseFromString
(
buf
))
<<
"Fail to parse config"
;
std
::
unique_ptr
<
GradientMachine
>
machine
(
create
(
trainerConfigTemp
.
model_config
()));
std
::
vector
<
ParameterPtr
>&
parameters
=
machine
->
getParameters
();
for
(
auto
&
para
:
parameters
)
{
para
->
load
(
is
);
}
machine
->
onLoadParameter
();
if
(
trainerConfig
)
{
*
trainerConfig
=
trainerConfigTemp
;
}
return
machine
.
release
();
}
void
GradientMachine
::
saveParameters
(
const
std
::
string
&
dir
)
const
{
void
GradientMachine
::
saveParameters
(
const
std
::
string
&
dir
)
const
{
LOG
(
INFO
)
<<
"Saving parameters to "
<<
dir
;
LOG
(
INFO
)
<<
"Saving parameters to "
<<
dir
;
...
...
paddle/gserver/gradientmachines/GradientMachine.h
浏览文件 @
5b1f66fe
...
@@ -89,39 +89,6 @@ public:
...
@@ -89,39 +89,6 @@ public:
std
::
vector
<
ParameterType
>
{
std
::
vector
<
ParameterType
>
{
PARAMETER_VALUE
,
PARAMETER_GRADIENT
,
PARAMETER_MOMENTUM
});
PARAMETER_VALUE
,
PARAMETER_GRADIENT
,
PARAMETER_MOMENTUM
});
/**
* Create a gradient machine from the merged model file.
* The merged model file can be generated using tools/merge_model
* If dataConfig is not null, it will be filled with the DataConfig
* from the TrainerConfig
*/
static
GradientMachine
*
create
(
const
std
::
string
&
modelFile
,
DataConfig
*
dataConfig
);
/**
* Create a gradient machine from a stream which contains the merged
* model file. The merged model file can be generated using tools/merge_model
* If dataConfig is not null, it will be filled with the DataConfig
* from the TrainerConfig
*/
static
GradientMachine
*
create
(
std
::
istream
&
is
,
DataConfig
*
dataConfig
);
/**
* Create a gradient machine from the merged model file.
* The merged model file can be generated using tools/merge_model
* If trainerConfig is not null, it will be filled with the TrainerConfig
*/
static
GradientMachine
*
create
(
const
std
::
string
&
modelFile
,
TrainerConfig
*
trainerConfig
);
/**
* Create a gradient machine from a stream which contains the merged
* model file. The merged model file can be generated using tools/merge_model
* If trainerConfig is not null, it will be filled with the TrainerConfig
*/
static
GradientMachine
*
create
(
std
::
istream
&
is
,
TrainerConfig
*
trainerConfig
);
virtual
~
GradientMachine
()
{}
virtual
~
GradientMachine
()
{}
/**
/**
...
...
paddle/trainer/tests/CMakeLists.txt
浏览文件 @
5b1f66fe
################# test_Prediction ######################
add_unittest_without_exec
(
test_Prediction
test_Prediction.cpp
)
add_test
(
NAME test_Prediction
COMMAND
${
PROJ_ROOT
}
/paddle/.set_python_path.sh -d
${
PROJ_ROOT
}
/python
${
CMAKE_CURRENT_BINARY_DIR
}
/test_Prediction --merger=
${
CMAKE_CURRENT_BINARY_DIR
}
/../paddle_merge_model
WORKING_DIRECTORY
${
PROJ_ROOT
}
/paddle/
)
################# test_Compare ############################
################# test_Compare ############################
add_unittest_without_exec
(
test_Compare
add_unittest_without_exec
(
test_Compare
test_Compare.cpp
)
test_Compare.cpp
)
...
...
paddle/trainer/tests/test_Prediction.cpp
已删除
100644 → 0
浏览文件 @
f4678331
/* 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 <paddle/utils/PythonUtil.h>
#include "paddle/trainer/Trainer.h"
#include <gtest/gtest.h>
DECLARE_string
(
config
);
DECLARE_string
(
config_args
);
DEFINE_string
(
merger
,
"./paddle_merge_model"
,
"path to paddle_merge_model binary"
);
using
namespace
paddle
;
// NOLINT
using
namespace
std
;
// NOLINT
static
const
string
&
configFile
=
"trainer/tests/sample_trainer_config.conf"
;
static
const
string
&
mergedModelFile
=
"./test_model_file"
;
static
const
string
&
modelDir
=
"./test_model_dir"
;
void
checkBuffer
(
real
*
vec1
,
real
*
vec2
,
size_t
len
)
{
for
(
size_t
i
=
0
;
i
<
len
;
i
++
)
{
EXPECT_EQ
(
vec1
[
i
],
vec2
[
i
])
<<
"vec1:"
<<
vec1
[
i
]
<<
" vec2:"
<<
vec2
[
i
];
}
}
void
checkParameters
(
vector
<
ParameterPtr
>
A
,
vector
<
ParameterPtr
>
B
)
{
CHECK_EQ
(
B
.
size
(),
A
.
size
())
<<
"parameter size not equal"
;
for
(
size_t
i
=
0
;
i
<
A
.
size
();
i
++
)
{
auto
vec1
=
A
[
i
]
->
getBuf
(
PARAMETER_VALUE
);
auto
vec2
=
B
[
i
]
->
getBuf
(
PARAMETER_VALUE
);
CHECK_EQ
(
vec1
->
useGpu_
,
vec2
->
useGpu_
)
<<
"use gpu not equal"
;
CHECK_EQ
(
vec1
->
getSize
(),
vec2
->
getSize
())
<<
"size not equal"
;
if
(
vec1
->
useGpu_
==
false
)
{
checkBuffer
(
vec1
->
getData
(),
vec2
->
getData
(),
vec1
->
getSize
());
}
else
{
VectorPtr
cpuVec1
=
Vector
::
create
(
vec1
->
getSize
(),
false
);
VectorPtr
cpuVec2
=
Vector
::
create
(
vec2
->
getSize
(),
false
);
cpuVec1
->
copyFrom
(
*
vec1
,
HPPL_STREAM_DEFAULT
);
cpuVec2
->
copyFrom
(
*
vec2
,
HPPL_STREAM_DEFAULT
);
hl_stream_synchronize
(
HPPL_STREAM_DEFAULT
);
checkBuffer
(
cpuVec1
->
getData
(),
cpuVec2
->
getData
(),
cpuVec1
->
getSize
());
}
}
}
TEST
(
GradientMachine
,
create
)
{
#ifdef PADDLE_ONLY_CPU
FLAGS_use_gpu
=
false
;
#endif
mkDir
(
modelDir
.
c_str
());
FLAGS_config
=
configFile
;
FLAGS_config_args
=
"with_cost=False"
;
auto
config
=
TrainerConfigHelper
::
createFromFlagConfig
();
// save model to directory
unique_ptr
<
GradientMachine
>
gradientMachine1
(
GradientMachine
::
create
(
*
config
));
gradientMachine1
->
saveParameters
(
modelDir
);
Trainer
trainer
;
trainer
.
init
(
config
);
ParameterUtil
*
paramUtil
=
trainer
.
getParameterUtilPtr
();
if
(
paramUtil
!=
NULL
)
{
paramUtil
->
saveConfigWithPath
(
modelDir
);
}
// create a different GradientMachine
unique_ptr
<
GradientMachine
>
gradientMachine2
(
GradientMachine
::
create
(
*
config
));
gradientMachine2
->
randParameters
();
// merge config and model to one file
string
cmd
=
FLAGS_merger
+
" --model_dir="
+
modelDir
+
" --config_args=with_cost=False"
+
" --model_file="
+
mergedModelFile
;
LOG
(
INFO
)
<<
cmd
;
int
ret
=
system
(
cmd
.
c_str
());
EXPECT_EQ
(
0
,
ret
);
if
(
ret
)
{
return
;
}
// create GradientMachine from the merged model
DataConfig
dataConfig
;
unique_ptr
<
GradientMachine
>
gradientMachine3
(
GradientMachine
::
create
(
mergedModelFile
,
&
dataConfig
));
CHECK
(
gradientMachine3
);
EXPECT_EQ
(
dataConfig
.
type
(),
"simple"
);
EXPECT_EQ
(
dataConfig
.
feat_dim
(),
3
);
// compare the parameters of GradientMachine and GradientMachine3
std
::
vector
<
ParameterPtr
>
paraMachine1
=
gradientMachine1
->
getParameters
();
std
::
vector
<
ParameterPtr
>
paraMachine3
=
gradientMachine3
->
getParameters
();
checkParameters
(
paraMachine1
,
paraMachine3
);
// Test that the GradientMachine created from the merged model
// is same as the orginnal one.
vector
<
Argument
>
inArgs
(
1
);
vector
<
Argument
>
outArgs
;
int
inputDim
=
3
;
int
numSamples
=
2
;
CpuMatrix
cpuInput
(
numSamples
,
inputDim
);
for
(
int
i
=
0
;
i
<
numSamples
;
++
i
)
{
for
(
int
j
=
0
;
j
<
inputDim
;
++
j
)
{
cpuInput
.
getData
()[
i
*
inputDim
+
j
]
=
rand
()
/
(
real
)
RAND_MAX
;
// NOLINT TODO(yuyang): use rand_r
}
}
MatrixPtr
input
=
Matrix
::
create
(
numSamples
,
inputDim
,
/* trans */
false
,
FLAGS_use_gpu
);
input
->
copyFrom
(
cpuInput
);
inArgs
[
0
].
value
=
input
;
gradientMachine1
->
forward
(
inArgs
,
&
outArgs
,
PASS_TEST
);
EXPECT_EQ
((
size_t
)
1
,
outArgs
.
size
());
vector
<
Argument
>
outArgs2
;
gradientMachine2
->
forward
(
inArgs
,
&
outArgs2
,
PASS_TEST
);
CpuMatrix
out1
(
outArgs
[
0
].
value
->
getHeight
(),
outArgs
[
0
].
value
->
getWidth
());
CpuMatrix
out2
(
outArgs2
[
0
].
value
->
getHeight
(),
outArgs2
[
0
].
value
->
getWidth
());
out1
.
copyFrom
(
*
outArgs
[
0
].
value
);
out2
.
copyFrom
(
*
outArgs2
[
0
].
value
);
for
(
size_t
i
=
0
;
i
<
out1
.
getHeight
()
*
out1
.
getWidth
();
i
++
)
{
EXPECT_NE
(
out1
.
getData
()[
i
],
out2
.
getData
()[
i
]);
}
gradientMachine3
->
forward
(
inArgs
,
&
outArgs2
,
PASS_TEST
);
out2
.
copyFrom
(
*
outArgs2
[
0
].
value
);
checkBuffer
(
out1
.
getData
(),
out2
.
getData
(),
out2
.
getHeight
()
*
out2
.
getWidth
());
cmd
=
" rm -rf "
+
modelDir
+
"/*"
;
LOG
(
INFO
)
<<
"cmd "
<<
cmd
;
ret
=
system
(
cmd
.
c_str
());
EXPECT_EQ
(
0
,
ret
);
if
(
ret
)
{
return
;
}
cmd
=
" rm -rf "
+
mergedModelFile
;
LOG
(
INFO
)
<<
"cmd "
<<
cmd
;
ret
=
system
(
cmd
.
c_str
());
EXPECT_EQ
(
0
,
ret
);
if
(
ret
)
{
return
;
}
// clean up
rmDir
(
modelDir
.
c_str
());
remove
(
mergedModelFile
.
c_str
());
}
int
main
(
int
argc
,
char
**
argv
)
{
initMain
(
argc
,
argv
);
initPython
(
argc
,
argv
);
testing
::
InitGoogleTest
(
&
argc
,
argv
);
return
RUN_ALL_TESTS
();
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录