Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
BaiXuePrincess
Paddle
提交
efe53811
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看板
提交
efe53811
编写于
3月 04, 2017
作者:
Y
Yu Yang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
complete serialize
* Test gzip
上级
fb74ae36
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
32 addition
and
37 deletion
+32
-37
demo/mnist/.gitignore
demo/mnist/.gitignore
+1
-0
demo/mnist/api_train_v2.py
demo/mnist/api_train_v2.py
+5
-9
python/paddle/v2/parameters.py
python/paddle/v2/parameters.py
+26
-28
未找到文件。
demo/mnist/.gitignore
浏览文件 @
efe53811
...
...
@@ -7,3 +7,4 @@ train.log
.ipynb_checkpoints
params.pkl
params.tar
params.tar.gz
demo/mnist/api_train_v2.py
浏览文件 @
efe53811
import
paddle.v2
as
paddle
import
cPickle
import
gzip
def
softmax_regression
(
img
):
...
...
@@ -73,8 +73,8 @@ def main():
cost
=
paddle
.
layer
.
classification_cost
(
input
=
predict
,
label
=
label
)
try
:
with
open
(
'params.pkl
'
,
'r'
)
as
f
:
parameters
=
cPickle
.
load
(
f
)
with
gzip
.
open
(
'params.tar.gz
'
,
'r'
)
as
f
:
parameters
=
paddle
.
parameters
.
Parameters
.
from_tar
(
f
)
except
IOError
:
parameters
=
paddle
.
parameters
.
create
(
cost
)
...
...
@@ -99,12 +99,8 @@ def main():
event
.
pass_id
,
event
.
batch_id
,
event
.
cost
,
event
.
metrics
,
result
.
metrics
)
with
open
(
'params.pkl'
,
'w'
)
as
f
:
cPickle
.
dump
(
parameters
,
f
,
protocol
=
cPickle
.
HIGHEST_PROTOCOL
)
with
open
(
'params.tar'
,
'w'
)
as
f
:
parameters
.
serialize_to_tar
(
f
)
with
gzip
.
open
(
'params.tar.gz'
,
'w'
)
as
f
:
parameters
.
to_tar
(
f
)
elif
isinstance
(
event
,
paddle
.
event
.
EndPass
):
result
=
trainer
.
test
(
reader
=
paddle
.
reader
.
batched
(
...
...
python/paddle/v2/parameters.py
浏览文件 @
efe53811
...
...
@@ -224,19 +224,6 @@ class Parameters(object):
self
.
__gradient_machines__
.
append
(
gradient_machine
)
def
__getstate__
(
self
):
params
=
{}
for
name
in
self
.
names
():
params
[
name
]
=
self
.
get
(
name
)
param_conf
=
{}
for
name
in
self
.
__param_conf__
:
conf
=
self
.
__param_conf__
[
name
]
assert
isinstance
(
conf
,
ParameterConfig
)
param_conf
[
name
]
=
conf
.
SerializeToString
()
return
{
'conf'
:
param_conf
,
'params'
:
params
}
def
serialize
(
self
,
name
,
f
):
"""
...
...
@@ -260,10 +247,10 @@ class Parameters(object):
:return:
"""
f
.
read
(
16
)
# header
arr
=
np
.
from
file
(
f
,
dtype
=
np
.
float32
)
arr
=
np
.
from
buffer
(
f
.
read
()
,
dtype
=
np
.
float32
)
self
.
set
(
name
,
arr
.
reshape
(
self
.
get_shape
(
name
)))
def
serialize_
to_tar
(
self
,
f
):
def
to_tar
(
self
,
f
):
tar
=
tarfile
.
TarFile
(
fileobj
=
f
,
mode
=
'w'
)
for
nm
in
self
.
names
():
buf
=
cStringIO
.
StringIO
()
...
...
@@ -273,19 +260,30 @@ class Parameters(object):
tarinfo
.
size
=
len
(
buf
.
getvalue
())
tar
.
addfile
(
tarinfo
,
buf
)
def
__setstate__
(
self
,
obj
):
Parameters
.
__init__
(
self
)
def
__impl__
(
conf
,
params
):
for
name
in
conf
:
p
=
ParameterConfig
()
p
.
ParseFromString
(
conf
[
name
])
self
.
__append_config__
(
p
)
for
name
in
params
:
shape
=
self
.
get_shape
(
name
)
self
.
set
(
name
,
params
[
name
].
reshape
(
shape
))
__impl__
(
**
obj
)
conf
=
self
.
__param_conf__
[
nm
]
confStr
=
conf
.
SerializeToString
()
tarinfo
=
tarfile
.
TarInfo
(
name
=
"%s.protobuf"
%
nm
)
tarinfo
.
size
=
len
(
confStr
)
buf
=
cStringIO
.
StringIO
(
confStr
)
buf
.
seek
(
0
)
tar
.
addfile
(
tarinfo
,
fileobj
=
buf
)
@
staticmethod
def
from_tar
(
f
):
params
=
Parameters
()
tar
=
tarfile
.
TarFile
(
fileobj
=
f
,
mode
=
'r'
)
for
finfo
in
tar
:
assert
isinstance
(
finfo
,
tarfile
.
TarInfo
)
if
finfo
.
name
.
endswith
(
'.protobuf'
):
f
=
tar
.
extractfile
(
finfo
)
conf
=
ParameterConfig
()
conf
.
ParseFromString
(
f
.
read
())
params
.
__append_config__
(
conf
)
for
param_name
in
params
.
names
():
f
=
tar
.
extractfile
(
param_name
)
params
.
deserialize
(
param_name
,
f
)
return
params
def
__get_parameter_in_gradient_machine__
(
gradient_machine
,
name
):
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录