def test_tensorflow_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 tensorflow with tf.Session(config=_TF_CONFIG).as_default(): res_got = expr(var, backend='tensorflow') assert all(array is None or infer_backend(array) == 'tensorflow' for array in expr._evaluated_constants['tensorflow']) 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 tensorflow call returns tensorflow still res_got3 = expr(backends.to_tensorflow(var)) assert isinstance(res_got3, tf.Tensor)
def test_tensorflow_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 tensorflow sess = tf.Session(config=_TF_CONFIG) with sess.as_default(): res_got = expr(var, backend='tensorflow') sess.close() assert 'tensorflow' 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 tensorflow call returns tensorflow still res_got3 = expr(backends.to_tensorflow(var), backend='tensorflow') assert isinstance(res_got3, tf.Tensor)
def test_tensorflow(string): views = helpers.build_views(string) ein = contract(string, *views, optimize=False, use_blas=False) opt = np.empty_like(ein) shps = [v.shape for v in views] expr = contract_expression(string, *shps, optimize=True) sess = tf.Session(config=_TF_CONFIG) with sess.as_default(): expr(*views, backend='tensorflow', out=opt) sess.close() assert np.allclose(ein, opt) # test non-conversion mode tensorflow_views = [backends.to_tensorflow(view) for view in views] expr(*tensorflow_views)