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

Merge pull request #955 from nouiz/small

Small
......@@ -11,7 +11,7 @@ before_install:
- sudo apt-get install -qq libatlas3gf-base libatlas-dev
install:
#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.
- "pip install . --no-deps --use-mirrors"
# command to run tests
......
......@@ -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
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
----------------
......
......@@ -51,7 +51,7 @@ except ImportError:
loc = os.path.join(config.compiledir, dirname)
if not os.path.exists(loc):
os.mkdir(loc)
preargs = ['-pthread', '-fwrapv', '-O2', '-fno-strict-aliasing']
preargs = ['-fwrapv', '-O2', '-fno-strict-aliasing']
preargs += cmodule.GCC_compiler.compile_args()
cmodule.GCC_compiler.compile_str(dirname, code, location=loc,
preargs=preargs)
......
......@@ -91,6 +91,12 @@ class BinCountOp(theano.Op):
def __init__(self, minlength=None):
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):
return (type(self) == type(other) and
......@@ -145,8 +151,11 @@ class BinCountOp(theano.Op):
if weights is not None and weights.shape != x.shape:
raise TypeError("All inputs must have the same shape.")
z[0] = np.bincount(x, weights=weights, minlength=self.minlength)
#Needed for numpy 1.4.1 compatibility
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):
output = self(*inputs)
......
......@@ -10,6 +10,9 @@ from theano import tensor as T
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):
def setUp(self):
super(TestBinCountOp, self).setUp()
......@@ -39,12 +42,14 @@ class TestBinCountOp(utt.InferShapeTester):
f1 = theano.function([x], bincount(x))
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.allclose(np.bincount(a, weights=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=5) == f4(a)).all()
......@@ -79,6 +84,8 @@ class TestBinCountOp(utt.InferShapeTester):
50, size=(25,)).astype(dtype)],
self.op_class)
if not numpy_16:
continue
self._compile_and_check(
[x],
[bincount(x, minlength=60)],
......
import unittest
from numpy.testing import assert_allclose
from theano.tests import unittest_tools as utt
import numpy as np
......@@ -22,7 +21,7 @@ class test_sort(unittest.TestCase):
a = tensor.dmatrix()
w = sort(a)
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):
a = tensor.dmatrix()
......@@ -32,7 +31,7 @@ class test_sort(unittest.TestCase):
for axis_val in 0, 1:
gv = f(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):
a = tensor.dvector()
......@@ -40,7 +39,7 @@ class test_sort(unittest.TestCase):
f = theano.function([a], w2)
gv = f(self.v_val)
gt = np.sort(self.v_val)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
def test4(self):
a = tensor.dmatrix()
......@@ -50,7 +49,7 @@ class test_sort(unittest.TestCase):
for axis_val in 0, 1:
gv = f(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):
a1 = SortOp("mergesort", [])
......@@ -67,7 +66,7 @@ class test_sort(unittest.TestCase):
f = theano.function([a], l)
gv = f(self.m_val)
gt = np.sort(self.m_val, None)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
class TensorInferShapeTester(utt.InferShapeTester):
......@@ -97,7 +96,7 @@ def test_argsort():
f = theano.function([a], w)
gv = f(m_val)
gt = np.argsort(m_val)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
#Example 2
a = tensor.dmatrix()
......@@ -107,7 +106,7 @@ def test_argsort():
for axis_val in 0, 1:
gv = f(m_val, axis_val)
gt = np.argsort(m_val, axis_val)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
#Example 3
a = tensor.dvector()
......@@ -115,7 +114,7 @@ def test_argsort():
f = theano.function([a], w2)
gv = f(v_val)
gt = np.argsort(v_val)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
#Example 4
a = tensor.dmatrix()
......@@ -125,7 +124,7 @@ def test_argsort():
for axis_val in 0, 1:
gv = f(m_val, axis_val)
gt = np.argsort(m_val, axis_val)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
#Example 5
a = tensor.dmatrix()
......@@ -143,4 +142,4 @@ def test_argsort():
f = theano.function([a], w2)
gv = f(m_val)
gt = np.argsort(m_val, None)
assert_allclose(gv, gt)
assert np.allclose(gv, gt)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论