提交 175dc512 authored 作者: Li's avatar Li 提交者: Frederic

pickle and unpickle done.

上级 5e70f3ac
......@@ -683,6 +683,16 @@ import theano and print the config variable, as in:
optimization phase. Theano user's do not need to use this. This is
to help debug shape error in Theano optimization.
.. attribute:: config.reoptimize_unpickled_function
Bool value, default: True
Theano users can use the standard python pickle tools to save a compiled
theano function. When pickling, both graph before and after the optimization
are saved, including shared variables. When set to True, the graph is
reoptimized when being unpickled. Otherwise, skip the graph optimization and
use directly the optimized graph.
.. attribute:: config.exception_verbosity
String Value: ``'low'``, ``'high'``.
......
......@@ -547,6 +547,7 @@ AddConfigVar('check_input',
BoolParam(True))
AddConfigVar('cache_optimizations',
"WARNING: work in progress, does not work yet."
"Specify if the optimization cache should be used. This cache will"
"any optimized graph and its optimization. Actually slow downs a lot"
"the first optimization, and could possibly still contains some bugs."
......
import unittest
import theano
import theano.tensor as T
from theano.gof.graph import is_same_graph
from theano.gof import FunctionGraph
from theano.scan_module.scan_utils import equal_computations
def test_graph_equivalence():
# Test if equivalent graphs are in fact equivalent
# by using some functions in Theano
# graph g1
g1_a = T.fmatrix('inputs')
g1_b = T.fmatrix('inputs')
g1_y = T.sum(g1_a+g1_b)
g1_yy = T.sum(g1_a + g1_b)
g2_x = T.fmatrix('inputs')
g2_y = g2_x.sum()
g3_a = T.fmatrix('inputs')
g3_b = T.fmatrix('inputs')
g3_y = T.sum(g3_a+g3_b)
assert is_same_graph(g1_y, g2_y) is False
# This does not work.
assert is_same_graph(g1_y, g1_y)
assert is_same_graph(g1_y, g1_yy)
assert is_same_graph(g1_y, g3_y, givens={g1_a: g3_a, g1_b: g3_b})
l1 = theano.gof.graph.inputs([g1_y])
l2 = theano.gof.graph.inputs([g3_y])
assert len(l1) == len(l2)
#FunctionGraph([], g1_y)
#assert graphs_equal(g1_y, g3_y) == True
#assert graphs_equal(g1_y, g2_y) == False
if __name__ == '__main__':
test_graph_equivalence()
......@@ -33,19 +33,19 @@ def test_pickle_unpickle_with_reoptimization():
f = theano.function([x1,x2],y, updates=updates)
# now pickle the compiled theano fn
pkl_path = open('thean_fn.pkl','wb')
cPickle.dump(f, pkl_path, -1)
string_pkl = cPickle.dumps(f, -1)
in1 = numpy.ones((10, 10), dtype=floatX)
in2 = numpy.ones((10, 10), dtype=floatX)
print 'the desired value is ',f(in1, in2)
# test unpickle with optimization
theano.config.reoptimize_unpickled_function=True # the default is True
pkl_path = open('thean_fn.pkl','r')
f_ = cPickle.load(pkl_path)
print 'got value ', f_(in1, in2)
assert f(in1, in2) == f_(in1, in2)
default = theano.config.reoptimize_unpickled_function
try:
theano.config.reoptimize_unpickled_function=True # the default is True
f_ = cPickle.loads(string_pkl)
assert f(in1, in2) == f_(in1, in2)
finally:
theano.config.reoptimize_unpickled_function = default
def test_pickle_unpickle_without_reoptimization():
x1 = T.fmatrix('x1')
......@@ -60,20 +60,20 @@ def test_pickle_unpickle_without_reoptimization():
f = theano.function([x1,x2],y, updates=updates)
# now pickle the compiled theano fn
pkl_path = open('thean_fn.pkl','wb')
cPickle.dump(f, pkl_path, -1)
string_pkl = cPickle.dumps(f, -1)
# compute f value
in1 = numpy.ones((10, 10), dtype=floatX)
in2 = numpy.ones((10, 10), dtype=floatX)
print 'the desired value is ',f(in1, in2)
# test unpickle without optimization
theano.config.reoptimize_unpickled_function=False # the default is True
pkl_path = open('thean_fn.pkl','r')
f_ = cPickle.load(pkl_path)
print 'got value ', f_(in1, in2)
assert f(in1, in2) == f_(in1, in2)
default = theano.config.reoptimize_unpickled_function
try:
theano.config.reoptimize_unpickled_function=False # the default is True
f_ = cPickle.loads(string_pkl)
assert f(in1, in2) == f_(in1, in2)
finally:
theano.config.reoptimize_unpickled_function = default
if __name__ == '__main__':
test_pickle_unpickle_with_reoptimization()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论