def test_BlockMatrix_Inverse_execution(): k, n = 2, 4 dtype = 'float32' A = diofant.MatrixSymbol('A', n, k) B = diofant.MatrixSymbol('B', n, n) inputs = A, B output = B.I * A cutsizes = { A: [(n // 2, n // 2), (k // 2, k // 2)], B: [(n // 2, n // 2), (n // 2, n // 2)] } cutinputs = [diofant.blockcut(i, *cutsizes[i]) for i in inputs] cutoutput = output.subs(dict(zip(inputs, cutinputs))) dtypes = dict(zip(inputs, [dtype] * len(inputs))) f = theano_function(inputs, [output], dtypes=dtypes, cache={}) fblocked = theano_function(inputs, [diofant.block_collapse(cutoutput)], dtypes=dtypes, cache={}) ninputs = [np.random.rand(*x.shape).astype(dtype) for x in inputs] ninputs = [ np.arange(n * k).reshape(A.shape).astype(dtype), np.eye(n).astype(dtype) ] ninputs[1] += np.ones(B.shape) * 1e-5 assert np.allclose(f(*ninputs), fblocked(*ninputs), rtol=1e-5)
def test_theano_function_kwargs(): import numpy as np f = theano_function([x, y, z], [x + y], dim=1, on_unused_input='ignore', dtypes={ x: 'float64', y: 'float64', z: 'float64' }) assert np.linalg.norm(f([1, 2], [3, 4], [0, 0]) - np.asarray([4, 6])) < 1e-9 f = theano_function([x, y, z], [x + y], dtypes={ x: 'float64', y: 'float64', z: 'float64' }, dim=1, on_unused_input='ignore') xx = np.arange(3).astype('float64') yy = 2 * np.arange(3).astype('float64') zz = 2 * np.arange(3).astype('float64') assert np.linalg.norm(f(xx, yy, zz) - 3 * np.arange(3)) < 1e-9
def test_theano_function_numpy(): f = theano_function([x, y], [x + y], dim=1, dtypes={ x: 'float64', y: 'float64' }) assert np.linalg.norm(f([1, 2], [3, 4]) - np.asarray([4, 6])) < 1e-9 f = theano_function([x, y], [x + y], dtypes={ x: 'float64', y: 'float64' }, dim=1) xx = np.arange(3).astype('float64') yy = 2 * np.arange(3).astype('float64') assert np.linalg.norm(f(xx, yy) - 3 * np.arange(3)) < 1e-9
def test_bad_keyword_args_raise_error(): pytest.raises(Exception, lambda: theano_function([x], [x + 1], foobar=3))
def test_theano_function_simple(): f = theano_function([x, y], [x + y]) assert f(2, 3) == 5