提交 2c1f7d80 authored 作者: hantek's avatar hantek

python 3 compatibility

上级 f51b38b9
......@@ -41,14 +41,14 @@ Conditions
n_times = 10
tic = time.clock()
for i in xrange(n_times):
for i in range(n_times):
f_switch(val1, val2, big_mat1, big_mat2)
print 'time spent evaluating both values %f sec'%(time.clock()-tic)
print('time spent evaluating both values %f sec' % (time.clock()-tic))
tic = time.clock()
for i in xrange(n_times):
for i in range(n_times):
f_lazyifelse(val1, val2, big_mat1, big_mat2)
print 'time spent evaluating one value %f sec'%(time.clock()-tic)
print('time spent evaluating one value %f sec' % (time.clock()-tic))
.. testoutput::
:hide:
......@@ -142,7 +142,7 @@ Loops
outputs=polynomial)
test_coeff = numpy.asarray([1, 0, 2], dtype=numpy.float32)
print calculate_polynomial(test_coeff, 3)
print(calculate_polynomial(test_coeff, 3))
.. testoutput::
......
......@@ -420,9 +420,9 @@ Create a test file containing:
t_start = time.time()
tAB = mf(A,B)
t_end = time.time()
print "NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
np_end-np_start, t_end-t_start)
print "Result difference: %f" % (np.abs(AB-tAB).max(), )
print("NP time: %f[s], theano time: %f[s] (times should be close when run on CPU!)" %(
np_end-np_start, t_end-t_start))
print("Result difference: %f" % (np.abs(AB-tAB).max(), ))
.. testoutput::
:hide:
......
......@@ -187,7 +187,7 @@ downcast** of the latter.
# test
some_num = 15
print(triangular_sequence(some_num))
print([n * (n + 1) // 2 for n in xrange(some_num)])
print([n * (n + 1) // 2 for n in range(some_num)])
.. testoutput::
......
......@@ -206,15 +206,15 @@ Let's try it out!
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_examples.test_examples_8
>>> state.get_value()
>>> print(state.get_value())
0
>>> accumulator(1)
array(0)
>>> state.get_value()
>>> print(state.get_value())
1
>>> accumulator(300)
array(1)
>>> state.get_value()
>>> print(state.get_value())
301
It is possible to reset the state. Just use the ``.set_value()`` method:
......@@ -222,7 +222,7 @@ It is possible to reset the state. Just use the ``.set_value()`` method:
>>> state.set_value(-1)
>>> accumulator(3)
array(-1)
>>> state.get_value()
>>> print(state.get_value())
2
As we mentioned above, you can define more than one function to use the same
......@@ -234,7 +234,7 @@ shared variable. These functions can all update the value.
>>> decrementor = function([inc], state, updates=[(state, state-inc)])
>>> decrementor(2)
array(2)
>>> state.get_value()
>>> print(state.get_value())
0
You might be wondering why the updates mechanism exists. You can always
......@@ -261,7 +261,7 @@ for the purpose of one particular function.
>>> skip_shared = function([inc, foo], fn_of_state, givens=[(state, foo)])
>>> skip_shared(1, 3) # we're using 3 for the state, not state.value
array(7)
>>> state.get_value() # old state still there, but we didn't use it
>>> print(state.get_value()) # old state still there, but we didn't use it
0
The ``givens`` parameter can be used to replace any symbolic variable, not just a
......
......@@ -30,10 +30,7 @@ The Basics of Pickling
The two modules ``pickle`` and ``cPickle`` have the same functionalities, but
``cPickle``, coded in C, is much faster.
.. If you modify this code, also change :
.. theano/tests/test_tutorial.py:T_loading_and_saving.test_loading_and_saving_3
>>> import cPickle
>>> from six.moves import cPickle
You can serialize (or *save*, or *pickle*) objects to a file with
``cPickle.dump``:
......@@ -42,7 +39,7 @@ You can serialize (or *save*, or *pickle*) objects to a file with
my_obj = object()
>>> f = file('obj.save', 'wb')
>>> f = open('obj.save', 'wb')
>>> cPickle.dump(my_obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
>>> f.close()
......@@ -60,7 +57,7 @@ You can serialize (or *save*, or *pickle*) objects to a file with
To de-serialize (or *load*, or *unpickle*) a pickled file, use
``cPickle.load``:
>>> f = file('obj.save', 'rb')
>>> f = open('obj.save', 'rb')
>>> loaded_obj = cPickle.load(f)
>>> f.close()
......@@ -74,14 +71,14 @@ same order):
obj2 = object()
obj3 = object()
>>> f = file('objects.save', 'wb')
>>> f = open('objects.save', 'wb')
>>> for obj in [obj1, obj2, obj3]:
... cPickle.dump(obj, f, protocol=cPickle.HIGHEST_PROTOCOL)
>>> f.close()
Then:
>>> f = file('objects.save', 'rb')
>>> f = open('objects.save', 'rb')
>>> loaded_objects = []
>>> for i in range(3):
... loaded_objects.append(cPickle.load(f))
......@@ -121,7 +118,7 @@ For instance, you can define functions along the lines of:
def __setstate__(self, d):
self.__dict__.update(d)
self.training_set = cPickle.load(file(self.training_set_file, 'rb'))
self.training_set = cPickle.load(open(self.training_set_file, 'rb'))
Robust Serialization
......
......@@ -66,7 +66,7 @@ Debug Print
The pre-compilation graph:
>>> theano.printing.debugprint(prediction) # doctest: +NORMALIZE_WHITESPACE
>>> theano.printing.debugprint(prediction) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Elemwise{gt,no_inplace} [id A] ''
|Elemwise{true_div,no_inplace} [id B] ''
| |DimShuffle{x} [id C] ''
......@@ -87,9 +87,9 @@ Elemwise{gt,no_inplace} [id A] ''
The post-compilation graph:
>>> theano.printing.debugprint(predict) # doctest: +NORMALIZE_WHITESPACE
>>> theano.printing.debugprint(predict) # doctest: +NORMALIZE_WHITESPACE, +ELLIPSIS
Elemwise{Composite{GT(scalar_sigmoid((-((-i0) - i1))), i2)}} [id A] '' 4
|Gemv{inplace} [id B] '' 3
|...Gemv{inplace} [id B] '' 3
| |AllocEmpty{dtype='float64'} [id C] '' 2
| | |Shape_i{0} [id D] '' 1
| | |x [id E]
......
......@@ -45,7 +45,8 @@ def function_dump(filename, inputs, outputs=None, mode=None, updates=None,
To load such a dump and do the compilation:
>>> import cPickle, theano
>>> from six.moves import cPickle
>>> import theano
>>> d = cPickle.load(open("func_dump.bin", "rb")) # doctest: +SKIP
>>> f = theano.function(**d) # doctest: +SKIP
......
......@@ -327,13 +327,13 @@ def dump(obj, file_handler, protocol=DEFAULT_PROTOCOL,
>>> import theano
>>> foo_1 = theano.shared(0, name='foo')
>>> foo_2 = theano.shared(1, name='foo')
>>> with open('model.zip', 'w') as f:
>>> with open('model.zip', 'wb') as f:
... dump((foo_1, foo_2, numpy.array(2)), f)
>>> numpy.load('model.zip').keys()
['foo', 'foo_2', 'array_0', 'pkl']
>>> numpy.load('model.zip')['foo']
array(0)
>>> with open('model.zip') as f:
>>> with open('model.zip', 'rb') as f:
... foo_1, foo_2, array = load(f)
>>> array
array(2)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论