提交 78bf69d4 authored 作者: Benjamin Scellier's avatar Benjamin Scellier

file theano/compile/tests/test_function_module.py

上级 e331b68f
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import copy import copy
import six.moves.cPickle as pickle import six.moves.cPickle as pickle
import numpy import numpy as np
import unittest import unittest
...@@ -18,8 +18,6 @@ from theano import tensor ...@@ -18,8 +18,6 @@ from theano import tensor
from theano import tensor as T from theano import tensor as T
import theano import theano
import numpy as N
def PatternOptimizer(p1, p2, ign=True): def PatternOptimizer(p1, p2, ign=True):
return gof.OpKeyOptimizer(gof.PatternSub(p1, p2), ignore_newtrees=ign) return gof.OpKeyOptimizer(gof.PatternSub(p1, p2), ignore_newtrees=ign)
...@@ -281,7 +279,7 @@ class T_function(unittest.TestCase): ...@@ -281,7 +279,7 @@ class T_function(unittest.TestCase):
def test_swap_SharedVariable(self): def test_swap_SharedVariable(self):
i = T.iscalar() i = T.iscalar()
x_list = theano.shared(value=numpy.random.rand(10).astype(config.floatX)) x_list = theano.shared(value=np.random.rand(10).astype(config.floatX))
x = T.scalar('x') x = T.scalar('x')
# SharedVariable for tests, one of them has update # SharedVariable for tests, one of them has update
...@@ -343,11 +341,11 @@ class T_function(unittest.TestCase): ...@@ -343,11 +341,11 @@ class T_function(unittest.TestCase):
A special testcase for logistic_sgd.py in Deep Learning Tutorial A special testcase for logistic_sgd.py in Deep Learning Tutorial
This test assert that SharedVariable in different function have same storage This test assert that SharedVariable in different function have same storage
""" """
train_x = theano.shared(value=numpy.random.rand(10, 10).astype(config.floatX)) train_x = theano.shared(value=np.random.rand(10, 10).astype(config.floatX))
test_x = theano.shared(value=numpy.random.rand(10, 10).astype(config.floatX)) test_x = theano.shared(value=np.random.rand(10, 10).astype(config.floatX))
train_y = theano.shared(value=numpy.random.rand(10, 1).astype(config.floatX)) train_y = theano.shared(value=np.random.rand(10, 1).astype(config.floatX))
test_y = theano.shared(value=numpy.random.rand(10, 1).astype(config.floatX)) test_y = theano.shared(value=np.random.rand(10, 1).astype(config.floatX))
i = T.iscalar('index') i = T.iscalar('index')
x = T.vector('x') x = T.vector('x')
...@@ -500,42 +498,42 @@ class T_function(unittest.TestCase): ...@@ -500,42 +498,42 @@ class T_function(unittest.TestCase):
when borrow=True is implemented. when borrow=True is implemented.
""" """
a = T.dmatrix() a = T.dmatrix()
aval = numpy.random.rand(3, 3) aval = np.random.rand(3, 3)
# when borrow=False, test that a destroy map cannot alias output to input # when borrow=False, test that a destroy map cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a + 1, borrow=True)) f = theano.function([In(a, borrow=False)], Out(a + 1, borrow=True))
assert numpy.all(f(aval) == aval + 1) assert np.all(f(aval) == aval + 1)
assert not numpy.may_share_memory(aval, f(aval)) assert not np.may_share_memory(aval, f(aval))
# when borrow=False, test that a viewmap cannot alias output to input # when borrow=False, test that a viewmap cannot alias output to input
f = theano.function([In(a, borrow=False)], Out(a[0, :], borrow=True)) f = theano.function([In(a, borrow=False)], Out(a[0, :], borrow=True))
assert numpy.all(f(aval) == aval[0, :]) assert np.all(f(aval) == aval[0, :])
assert not numpy.may_share_memory(aval, f(aval)) assert not np.may_share_memory(aval, f(aval))
def test_borrow_output(self): def test_borrow_output(self):
a = T.dmatrix() a = T.dmatrix()
f = function([a], Out(a, borrow=False)) f = function([a], Out(a, borrow=False))
o = N.ones((3, 3)) o = np.ones((3, 3))
assert o is not f(o) # function no longer permits aliasing outputs to inputs assert o is not f(o) # function no longer permits aliasing outputs to inputs
f = function([a], Out(a * 4, borrow=False)) f = function([a], Out(a * 4, borrow=False))
o = N.ones((3, 3)) o = np.ones((3, 3))
four = f(o) four = f(o)
assert numpy.all(four == 4) assert np.all(four == 4)
f(o + .1) # should not clobber the memory used to store four f(o + .1) # should not clobber the memory used to store four
assert numpy.all(four == 4) assert np.all(four == 4)
f = function([a], Out(a * 4, borrow=True), mode=theano.Mode('c|py_nogc', 'fast_run')) f = function([a], Out(a * 4, borrow=True), mode=theano.Mode('c|py_nogc', 'fast_run'))
o = N.ones((3, 3)) o = np.ones((3, 3))
four = f(o) four = f(o)
assert numpy.all(four == 4) assert np.all(four == 4)
f(o + .1) # should clobber the memory used to store four f(o + .1) # should clobber the memory used to store four
if theano.config.cxx: if theano.config.cxx:
assert not numpy.all(four == 4) assert not np.all(four == 4)
else: else:
# The Elemwise.perform method don't reuse memory # The Elemwise.perform method don't reuse memory
# as some numpy version don't support that correctly. # as some numpy version don't support that correctly.
assert numpy.all(four == 4) assert np.all(four == 4)
def test_disconnected_input(self): def test_disconnected_input(self):
a = T.scalar('a') a = T.scalar('a')
...@@ -753,7 +751,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -753,7 +751,7 @@ class T_picklefunction(unittest.TestCase):
assert f2.container[s].storage is f1.container[s].storage assert f2.container[s].storage is f1.container[s].storage
# now put in a function with non-scalar # now put in a function with non-scalar
v_value = numpy.asarray([2, 3, 4.], dtype=config.floatX) v_value = np.asarray([2, 3, 4.], dtype=config.floatX)
f3 = function([x, In(v, value=v_value)], x + v) f3 = function([x, In(v, value=v_value)], x + v)
list_of_things.append(f3) list_of_things.append(f3)
...@@ -800,13 +798,13 @@ class T_picklefunction(unittest.TestCase): ...@@ -800,13 +798,13 @@ class T_picklefunction(unittest.TestCase):
assert nl[5](3) == ol[5](3) assert nl[5](3) == ol[5](3)
assert nl[4].value[nl[0]] == 6 assert nl[4].value[nl[0]] == 6
assert numpy.all(nl[6][nl[2]] == numpy.asarray([2, 3., 4])) assert np.all(nl[6][nl[2]] == np.asarray([2, 3., 4]))
def test_broken_pickle_with_shared(self): def test_broken_pickle_with_shared(self):
saves = [] saves = []
def pers_save(obj): def pers_save(obj):
if isinstance(obj, numpy.ndarray): if isinstance(obj, np.ndarray):
saves.append(obj) saves.append(obj)
return len(saves) - 1 return len(saves) - 1
else: else:
...@@ -815,7 +813,7 @@ class T_picklefunction(unittest.TestCase): ...@@ -815,7 +813,7 @@ class T_picklefunction(unittest.TestCase):
def pers_load(id): def pers_load(id):
return saves[id] return saves[id]
b = numpy.random.rand(5, 4) b = np.random.rand(5, 4)
x = theano.tensor.matrix() x = theano.tensor.matrix()
y = theano.shared(b) y = theano.shared(b)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论