Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
P
pytensor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
testgroup
pytensor
Commits
73f2057b
提交
73f2057b
authored
4月 21, 2015
作者:
serdyuk
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Improved loading, saving documentation
上级
f36d29fd
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
69 行增加
和
63 行删除
+69
-63
loading_and_saving.txt
doc/tutorial/loading_and_saving.txt
+13
-1
pkl_utils.py
theano/misc/pkl_utils.py
+56
-62
没有找到文件。
doc/tutorial/loading_and_saving.txt
浏览文件 @
73f2057b
...
@@ -114,6 +114,19 @@ For instance, you can define functions along the lines of:
...
@@ -114,6 +114,19 @@ For instance, you can define functions along the lines of:
self.training_set = cPickle.load(file(self.training_set_file, 'rb'))
self.training_set = cPickle.load(file(self.training_set_file, 'rb'))
Robust Serialization
====================
This type of serialization internally uses pickling but extracts values of all
shared variables and saves it as numpy arrays. So it may be very hard to
deserialize objects on a different version of Theano. It is useful when you
would like to resume an experiment on a different kind of hardware.
.. autofunction:: theano.misc.pkl_utils.dump
.. autofunction:: theano.misc.pkl_utils.load
Long-Term Serialization
Long-Term Serialization
=======================
=======================
...
@@ -154,4 +167,3 @@ functions to reflect the change in name:
...
@@ -154,4 +167,3 @@ functions to reflect the change in name:
For more information on advanced use of ``pickle`` and its internals, see Python's
For more information on advanced use of ``pickle`` and its internals, see Python's
pickle_ documentation.
pickle_ documentation.
theano/misc/pkl_utils.py
浏览文件 @
73f2057b
...
@@ -119,17 +119,15 @@ else:
...
@@ -119,17 +119,15 @@ else:
class
PersistentNdarrayID
(
object
):
class
PersistentNdarrayID
(
object
):
"""Persist ndarrays in an object by saving them to a zip file.
"""Persist ndarrays in an object by saving them to a zip file.
Parameters
:param zip_file: A zip file handle that the NumPy arrays will be saved to.
----------
:type zip_file: :class:`zipfile.ZipFile`
zip_file : :class:`zipfile.ZipFile`
A zip file handle that the NumPy arrays will be saved to.
.. note:
Notes
The convention for persistent ids given by this class and its derived
-----
classes is that the name should take the form `type.name` where `type`
The convention for persistent ids given by this class and its derived
can be used by the persistent loader to determine how to load the
classes is that the name should take the form `type.name` where `type`
object, while `name` is human-readable and as descriptive as possible.
can be used by the persistent loader to determine how to load the
object, while `name` is human-readable and as descriptive as possible.
"""
"""
def
__init__
(
self
,
zip_file
):
def
__init__
(
self
,
zip_file
):
...
@@ -166,19 +164,16 @@ class PersistentSharedVariableID(PersistentNdarrayID):
...
@@ -166,19 +164,16 @@ class PersistentSharedVariableID(PersistentNdarrayID):
shared variable are persisted as usual (i.e. `array_0`, `array_1`,
shared variable are persisted as usual (i.e. `array_0`, `array_1`,
etc.)
etc.)
Parameters
:param allow_unnamed: Allow shared variables without a name to be
----------
persisted. Defaults to ``True``.
allow_unnamed : bool, optional
:type allow_unnamed: bool, optional
Allow shared variables without a name to be persisted. Defaults to
``True``.
:param allow_duplicates: Allow multiple shared variables to have the same
allow_duplicates : bool, optional
name, in which case they will be numbered e.g. `x`, `x_2`, `x_3`, etc.
Allow multiple shared variables to have the same name, in which
Defaults to ``True``.
case they will be numbered e.g. `x`, `x_2`, `x_3`, etc. Defaults to
:type allow_duplicates: bool, optional
``True``.
:raises ValueError
Raises
------
ValueError
If an unnamed shared variable is encountered and `allow_unnamed` is
If an unnamed shared variable is encountered and `allow_unnamed` is
``False``, or if two shared variables have the same name, and
``False``, or if two shared variables have the same name, and
`allow_duplicates` is ``False``.
`allow_duplicates` is ``False``.
...
@@ -218,10 +213,8 @@ class PersistentSharedVariableID(PersistentNdarrayID):
...
@@ -218,10 +213,8 @@ class PersistentSharedVariableID(PersistentNdarrayID):
class
PersistentNdarrayLoad
(
object
):
class
PersistentNdarrayLoad
(
object
):
"""Load NumPy arrays that were persisted to a zip file when pickling.
"""Load NumPy arrays that were persisted to a zip file when pickling.
Parameters
:param zip_file: The zip file handle in which the NumPy arrays are saved.
----------
:type zip_file: :class:`zipfile.ZipFile`
zip_file : :class:`zipfile.ZipFile`
The zip file handle in which the NumPy arrays are saved.
"""
"""
def
__init__
(
self
,
zip_file
):
def
__init__
(
self
,
zip_file
):
...
@@ -251,28 +244,29 @@ def dump(obj, f, protocol=DEFAULT_PROTOCOL,
...
@@ -251,28 +244,29 @@ def dump(obj, f, protocol=DEFAULT_PROTOCOL,
persistent_id
=
PersistentSharedVariableID
):
persistent_id
=
PersistentSharedVariableID
):
"""Pickles an object to a zip file using external persistence.
"""Pickles an object to a zip file using external persistence.
Parameters
:param obj: The object to pickle.
----------
:type obj: object
obj : object
The object to pickle
.
:param f: The file handle to save the object to
.
f
: file
:type f
: file
The file handle to save the object to.
protocol : int, optional
:param protocol: The pickling protocol to use. Unlike Python's built-in
The pickling protocol to use. Unlike Python's built-in pickle, t
he
pickle, the default is set to `2` insstead of 0 for Python 2. T
he
default is set to `2` insstead of 0 for Python 2. The Python 3
Python 3 default (level 3) is maintained.
default (level 3) is maintained.
:type protocol: int, optional
persistent_id : callable
The callable that persists certain objects in the object hierarchy
:param persistent_id: The callable that persists certain objects in the
to separate files inside of the zip file. For example,
object hierarchy
to separate files inside of the zip file. For example,
:class:`PersistentNdarrayID` saves any :class:`numpy.ndarray` to a
:class:`PersistentNdarrayID` saves any :class:`numpy.ndarray` to a
separate NPY file inside of the zip file.
separate NPY file inside of the zip file.
:type persistent_id: callable
Notes
.. note::
-----
The final file is simply a zipped file containing at least one file,
The final file is simply a zipped file containing at least one file,
`pkl`, which contains the pickled object. It can contain any other
`pkl`, which contains the pickled object. It can contain any other
number of external objects. Note that the zip files are compatible with
number of external objects. Note that the zip files are compatible with
NumPy's :func:`numpy.load` function.
NumPy's :func:`numpy.load` function.
>>> import theano
>>> import theano
>>> foo_1 = theano.shared(0, name='foo')
>>> foo_1 = theano.shared(0, name='foo')
...
@@ -301,13 +295,13 @@ def dump(obj, f, protocol=DEFAULT_PROTOCOL,
...
@@ -301,13 +295,13 @@ def dump(obj, f, protocol=DEFAULT_PROTOCOL,
def
load
(
f
,
persistent_load
=
PersistentNdarrayLoad
):
def
load
(
f
,
persistent_load
=
PersistentNdarrayLoad
):
"""Load a file that was dumped to a zip file.
"""Load a file that was dumped to a zip file.
Parameters
:param f: The file handle to the zip file to load the object from.
----------
:type f: file
f : file
The file handle to the zip file to load the object from.
:param persistent_load: The persistent loading function to use for
persistent_load : callable, optional
unpickling. This must be compatible with the `persisten_id` function
The persistent loading function to use for unpickling. This must be
used when pickling.
compatible with the `persisten_id` function used when pickling.
:type persistent_load: callable, optional
"""
"""
with
closing
(
zipfile
.
ZipFile
(
f
,
'r'
))
as
zip_file
:
with
closing
(
zipfile
.
ZipFile
(
f
,
'r'
))
as
zip_file
:
...
@@ -319,15 +313,15 @@ def load(f, persistent_load=PersistentNdarrayLoad):
...
@@ -319,15 +313,15 @@ def load(f, persistent_load=PersistentNdarrayLoad):
def
zipadd
(
func
,
zip_file
,
name
):
def
zipadd
(
func
,
zip_file
,
name
):
"""Calls a function with a file object, saving it to a zip file.
"""Calls a function with a file object, saving it to a zip file.
Parameters
:param func: The function to call.
----------
:type func: callable
func : callable
The function to call.
:param zip_file: The zip file that `func` should write its data to.
zip_file : :class:`zipfile.ZipFile`
:type zip_file: :class:`zipfile.ZipFile`
The zip file that `func` should write its data to.
name : str
:param name: The name of the file inside of the zipped archive that `func`
The name of the file inside of the zipped archive that `func`
should save its data to.
should save its data to.
:type name: str
"""
"""
with
tempfile
.
NamedTemporaryFile
(
'wb'
,
delete
=
False
)
as
temp_file
:
with
tempfile
.
NamedTemporaryFile
(
'wb'
,
delete
=
False
)
as
temp_file
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论