def test_theano_with_constants(constants): eq = 'ij,jk,kl->li' shapes = (2, 3), (3, 4), (4, 5) non_const, = {0, 1, 2} - constants ops = [ np.random.rand(*shp) if i in constants else shp for i, shp in enumerate(shapes) ] var = np.random.rand(*shapes[non_const]) res_exp = contract(eq, *(ops[i] if i in constants else var for i in range(3))) expr = contract_expression(eq, *ops, constants=constants) # check theano res_got = expr(var, backend='theano') assert all(array is None or infer_backend(array) == 'theano' for array in expr._evaluated_constants['theano']) assert np.allclose(res_exp, res_got) # check can call with numpy still res_got2 = expr(var, backend='numpy') assert np.allclose(res_exp, res_got2) # check theano call returns theano still res_got3 = expr(backends.to_theano(var)) assert isinstance(res_got3, theano.tensor.TensorVariable)
def test_theano_with_constants(): eq = 'ij,jk,kl->li' shapes = (2, 3), (3, 4), (4, 5) constants = {0, 2} ops = [ np.random.rand(*shp) if i in constants else shp for i, shp in enumerate(shapes) ] var = np.random.rand(*shapes[1]) res_exp = contract(eq, ops[0], var, ops[2]) expr = contract_expression(eq, *ops, constants=constants) # check theano res_got = expr(var, backend='theano') assert 'theano' in expr._evaluated_constants assert np.allclose(res_exp, res_got) # check can call with numpy still res_got2 = expr(var, backend='numpy') assert np.allclose(res_exp, res_got2) # check theano call returns theano still res_got3 = expr(backends.to_theano(var), backend='theano') assert isinstance(res_got3, theano.tensor.TensorVariable)
def test_theano(string): views = helpers.build_views(string) ein = contract(string, *views, optimize=False, use_blas=False) shps = [v.shape for v in views] expr = contract_expression(string, *shps, optimize=True) opt = expr(*views, backend='theano') assert np.allclose(ein, opt) # test non-conversion mode theano_views = [backends.to_theano(view) for view in views] theano_opt = expr(*theano_views) assert isinstance(theano_opt, theano.tensor.TensorVariable)