提交 e0d06a4e authored 作者: lamblin's avatar lamblin

Merge pull request #955 from nouiz/small

Small
...@@ -11,7 +11,7 @@ before_install: ...@@ -11,7 +11,7 @@ before_install:
- sudo apt-get install -qq libatlas3gf-base libatlas-dev - sudo apt-get install -qq libatlas3gf-base libatlas-dev
install: install:
#If we don't install numpy before SciPy 0.10.1, the SciPy installations fails. #If we don't install numpy before SciPy 0.10.1, the SciPy installations fails.
- "pip install -q numpy --use-mirrors" - "pip install -q numpy==1.4.1 --use-mirrors"
#We can't install SciPy as there is no BLAS installed. #We can't install SciPy as there is no BLAS installed.
- "pip install . --no-deps --use-mirrors" - "pip install . --no-deps --use-mirrors"
# command to run tests # command to run tests
......
...@@ -42,6 +42,25 @@ space to allow to reuse them during the next call to the same Theano ...@@ -42,6 +42,25 @@ space to allow to reuse them during the next call to the same Theano
function if they are of the good shape. The shape could change if the function if they are of the good shape. The shape could change if the
shape of the inputs change. shape of the inputs change.
Faster Small Theano function
----------------------------
.. note::
For Theano 0.6 and up.
For Theano function that don't do much work like a regular logistic
regression, the overhead of checking the input can be significant. You
can disable it by setting f.trust_input to True to remove this
check. Make sure you pass argument as what you said when compiling the
Theano function.
Also for small Theano function, you can remove more python overhead by
making a Theano function that don't take any inputs. You can use shared
variable to help you. Then you can call it like this: ``f.fn()`` or
``f.fn(n_calls=N)`` to speed up. In the last case, only the last
function output is returned.
Related Projects Related Projects
---------------- ----------------
......
...@@ -51,7 +51,7 @@ except ImportError: ...@@ -51,7 +51,7 @@ except ImportError:
loc = os.path.join(config.compiledir, dirname) loc = os.path.join(config.compiledir, dirname)
if not os.path.exists(loc): if not os.path.exists(loc):
os.mkdir(loc) os.mkdir(loc)
preargs = ['-pthread', '-fwrapv', '-O2', '-fno-strict-aliasing'] preargs = ['-fwrapv', '-O2', '-fno-strict-aliasing']
preargs += cmodule.GCC_compiler.compile_args() preargs += cmodule.GCC_compiler.compile_args()
cmodule.GCC_compiler.compile_str(dirname, code, location=loc, cmodule.GCC_compiler.compile_str(dirname, code, location=loc,
preargs=preargs) preargs=preargs)
......
...@@ -91,6 +91,12 @@ class BinCountOp(theano.Op): ...@@ -91,6 +91,12 @@ class BinCountOp(theano.Op):
def __init__(self, minlength=None): def __init__(self, minlength=None):
self.minlength = minlength self.minlength = minlength
if minlength is not None:
numpy_ver = [int(n) for n in numpy.__version__.split('.')[:2]]
if not bool(numpy_ver >= [1, 6]):
raise NotImplementedError(
"BinCountOp with minlength attribute"
" need NumPy 1.6 or higher.")
def __eq__(self, other): def __eq__(self, other):
return (type(self) == type(other) and return (type(self) == type(other) and
...@@ -145,8 +151,11 @@ class BinCountOp(theano.Op): ...@@ -145,8 +151,11 @@ class BinCountOp(theano.Op):
if weights is not None and weights.shape != x.shape: if weights is not None and weights.shape != x.shape:
raise TypeError("All inputs must have the same shape.") raise TypeError("All inputs must have the same shape.")
#Needed for numpy 1.4.1 compatibility
z[0] = np.bincount(x, weights=weights, minlength=self.minlength) if self.minlength:
z[0] = np.bincount(x, weights=weights, minlength=self.minlength)
else:
z[0] = np.bincount(x, weights=weights)
def grad(self, inputs, outputs_gradients): def grad(self, inputs, outputs_gradients):
output = self(*inputs) output = self(*inputs)
......
...@@ -10,6 +10,9 @@ from theano import tensor as T ...@@ -10,6 +10,9 @@ from theano import tensor as T
from theano import config, tensor, function from theano import config, tensor, function
numpy_ver = [int(n) for n in numpy.__version__.split('.')[:2]]
numpy_16 = bool(numpy_ver >= [1, 6])
class TestBinCountOp(utt.InferShapeTester): class TestBinCountOp(utt.InferShapeTester):
def setUp(self): def setUp(self):
super(TestBinCountOp, self).setUp() super(TestBinCountOp, self).setUp()
...@@ -39,12 +42,14 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -39,12 +42,14 @@ class TestBinCountOp(utt.InferShapeTester):
f1 = theano.function([x], bincount(x)) f1 = theano.function([x], bincount(x))
f2 = theano.function([x, w], bincount(x, weights=w)) f2 = theano.function([x, w], bincount(x, weights=w))
f3 = theano.function([x], bincount(x, minlength=23))
f4 = theano.function([x], bincount(x, minlength=5))
assert (np.bincount(a) == f1(a)).all() assert (np.bincount(a) == f1(a)).all()
assert np.allclose(np.bincount(a, weights=weights), assert np.allclose(np.bincount(a, weights=weights),
f2(a, weights)) f2(a, weights))
if not numpy_16:
continue
f3 = theano.function([x], bincount(x, minlength=23))
f4 = theano.function([x], bincount(x, minlength=5))
assert (np.bincount(a, minlength=23) == f3(a)).all() assert (np.bincount(a, minlength=23) == f3(a)).all()
assert (np.bincount(a, minlength=5) == f4(a)).all() assert (np.bincount(a, minlength=5) == f4(a)).all()
...@@ -79,6 +84,8 @@ class TestBinCountOp(utt.InferShapeTester): ...@@ -79,6 +84,8 @@ class TestBinCountOp(utt.InferShapeTester):
50, size=(25,)).astype(dtype)], 50, size=(25,)).astype(dtype)],
self.op_class) self.op_class)
if not numpy_16:
continue
self._compile_and_check( self._compile_and_check(
[x], [x],
[bincount(x, minlength=60)], [bincount(x, minlength=60)],
......
import unittest import unittest
from numpy.testing import assert_allclose
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
import numpy as np import numpy as np
...@@ -22,7 +21,7 @@ class test_sort(unittest.TestCase): ...@@ -22,7 +21,7 @@ class test_sort(unittest.TestCase):
a = tensor.dmatrix() a = tensor.dmatrix()
w = sort(a) w = sort(a)
f = theano.function([a], w) f = theano.function([a], w)
assert_allclose(f(self.m_val), np.sort(self.m_val)) assert np.allclose(f(self.m_val), np.sort(self.m_val))
def test2(self): def test2(self):
a = tensor.dmatrix() a = tensor.dmatrix()
...@@ -32,7 +31,7 @@ class test_sort(unittest.TestCase): ...@@ -32,7 +31,7 @@ class test_sort(unittest.TestCase):
for axis_val in 0, 1: for axis_val in 0, 1:
gv = f(self.m_val, axis_val) gv = f(self.m_val, axis_val)
gt = np.sort(self.m_val, axis_val) gt = np.sort(self.m_val, axis_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
def test3(self): def test3(self):
a = tensor.dvector() a = tensor.dvector()
...@@ -40,7 +39,7 @@ class test_sort(unittest.TestCase): ...@@ -40,7 +39,7 @@ class test_sort(unittest.TestCase):
f = theano.function([a], w2) f = theano.function([a], w2)
gv = f(self.v_val) gv = f(self.v_val)
gt = np.sort(self.v_val) gt = np.sort(self.v_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
def test4(self): def test4(self):
a = tensor.dmatrix() a = tensor.dmatrix()
...@@ -50,7 +49,7 @@ class test_sort(unittest.TestCase): ...@@ -50,7 +49,7 @@ class test_sort(unittest.TestCase):
for axis_val in 0, 1: for axis_val in 0, 1:
gv = f(self.m_val, axis_val) gv = f(self.m_val, axis_val)
gt = np.sort(self.m_val, axis_val) gt = np.sort(self.m_val, axis_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
def test5(self): def test5(self):
a1 = SortOp("mergesort", []) a1 = SortOp("mergesort", [])
...@@ -67,7 +66,7 @@ class test_sort(unittest.TestCase): ...@@ -67,7 +66,7 @@ class test_sort(unittest.TestCase):
f = theano.function([a], l) f = theano.function([a], l)
gv = f(self.m_val) gv = f(self.m_val)
gt = np.sort(self.m_val, None) gt = np.sort(self.m_val, None)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
class TensorInferShapeTester(utt.InferShapeTester): class TensorInferShapeTester(utt.InferShapeTester):
...@@ -97,7 +96,7 @@ def test_argsort(): ...@@ -97,7 +96,7 @@ def test_argsort():
f = theano.function([a], w) f = theano.function([a], w)
gv = f(m_val) gv = f(m_val)
gt = np.argsort(m_val) gt = np.argsort(m_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
#Example 2 #Example 2
a = tensor.dmatrix() a = tensor.dmatrix()
...@@ -107,7 +106,7 @@ def test_argsort(): ...@@ -107,7 +106,7 @@ def test_argsort():
for axis_val in 0, 1: for axis_val in 0, 1:
gv = f(m_val, axis_val) gv = f(m_val, axis_val)
gt = np.argsort(m_val, axis_val) gt = np.argsort(m_val, axis_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
#Example 3 #Example 3
a = tensor.dvector() a = tensor.dvector()
...@@ -115,7 +114,7 @@ def test_argsort(): ...@@ -115,7 +114,7 @@ def test_argsort():
f = theano.function([a], w2) f = theano.function([a], w2)
gv = f(v_val) gv = f(v_val)
gt = np.argsort(v_val) gt = np.argsort(v_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
#Example 4 #Example 4
a = tensor.dmatrix() a = tensor.dmatrix()
...@@ -125,7 +124,7 @@ def test_argsort(): ...@@ -125,7 +124,7 @@ def test_argsort():
for axis_val in 0, 1: for axis_val in 0, 1:
gv = f(m_val, axis_val) gv = f(m_val, axis_val)
gt = np.argsort(m_val, axis_val) gt = np.argsort(m_val, axis_val)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
#Example 5 #Example 5
a = tensor.dmatrix() a = tensor.dmatrix()
...@@ -143,4 +142,4 @@ def test_argsort(): ...@@ -143,4 +142,4 @@ def test_argsort():
f = theano.function([a], w2) f = theano.function([a], w2)
gv = f(m_val) gv = f(m_val)
gt = np.argsort(m_val, None) gt = np.argsort(m_val, None)
assert_allclose(gv, gt) assert np.allclose(gv, gt)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论