def test_imag(dtype, input_cur): backend = tensorflow_backend.TensorFlowBackend() cur = backend.convert_to_tensor(input_cur) expected = tf.math.imag(input_cur) np.testing.assert_allclose(backend.imag(cur), expected)
def test_norm(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(np.ones((2, 2))) assert backend.norm(a).numpy() == 2
def test_zeros(dtype): backend = tensorflow_backend.TensorFlowBackend(dtype=dtype) a = backend.zeros((4, 4)) np.testing.assert_allclose(tf.zeros((4, 4), dtype=dtype), a)
def test_eps(dtype): backend = tensorflow_backend.TensorFlowBackend() assert backend.eps(dtype) == tf.experimental.numpy.finfo(dtype).eps
def test_shape_prod(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(2 * np.ones([1, 2, 3, 4])) actual = np.array(backend.shape_prod(a)) assert actual == 2**24
def test_broadcast_right_multiplication(dtype): backend = tensorflow_backend.TensorFlowBackend() tensor1 = backend.randn((2, 4, 3), dtype=dtype, seed=10) tensor2 = backend.randn((3,), dtype=dtype, seed=10) out = backend.broadcast_right_multiplication(tensor1, tensor2) np.testing.assert_allclose(out, tensor1 * tensor2)
def test_sparse_shape(): dtype = tf.float64 backend = tensorflow_backend.TensorFlowBackend() tensor = backend.randn((2, 3, 4), dtype=dtype, seed=10) np.testing.assert_allclose(backend.sparse_shape(tensor), tensor.shape)
def test_eye_dtype(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.eye(N=4, M=4, dtype=dtype) assert a.dtype == dtype
def test_zeros_dtype(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.zeros((4, 4), dtype=dtype) assert a.dtype == dtype
def test_ones(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.ones((4, 4), dtype=dtype) np.testing.assert_allclose(tf.ones((4, 4), dtype=dtype), a)
def test_random_uniform_non_zero_imag(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.random_uniform((4, 4), dtype=dtype, seed=10) assert tf.math.greater(tf.linalg.norm(tf.math.imag(a)), 0.0)
def test_diag(): backend = tensorflow_backend.TensorFlowBackend() b = backend.convert_to_tensor(np.array([1.0, 2.0, 3.0])) actual = backend.diag(b) expected = np.array([[1.0, 0.0, 0.0], [0.0, 2.0, 0.0], [0.0, 0.0, 3.0]]) np.testing.assert_allclose(expected, actual)
def test_trace(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(np.array([[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]])) actual = backend.trace(a) np.testing.assert_allclose(actual, 6)
def test_multiply(a, b, expected): backend = tensorflow_backend.TensorFlowBackend() tensor1 = backend.convert_to_tensor(a) tensor2 = backend.convert_to_tensor(b) np.testing.assert_allclose(backend.multiply(tensor1, tensor2), expected)
def test_gmres_not_implemented(): backend = tensorflow_backend.TensorFlowBackend() with pytest.raises(NotImplementedError): backend.gmres(lambda x: x, np.ones((2)))
def test_randn_dtype(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.randn((4, 4), dtype=dtype) assert a.dtype == dtype
def test_eigsh_lanczos_not_implemented(): backend = tensorflow_backend.TensorFlowBackend() with pytest.raises(NotImplementedError): backend.eigsh_lanczos(lambda x: x, [])
def test_random_uniform_dtype(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.random_uniform((4, 4), dtype=dtype, seed=10) assert a.dtype == dtype
def test_broadcast_left_multiplication(dtype): backend = tensorflow_backend.TensorFlowBackend() tensor1 = backend.randn((3,), dtype=dtype, seed=10) tensor2 = backend.randn((3, 4, 2), dtype=dtype, seed=10) out = backend.broadcast_left_multiplication(tensor1, tensor2) np.testing.assert_allclose(out, np.reshape(tensor1, (3, 1, 1)) * tensor2)
def test_randn_seed(dtype): backend = tensorflow_backend.TensorFlowBackend() a = backend.randn((4, 4), seed=10, dtype=dtype) b = backend.randn((4, 4), seed=10, dtype=dtype) np.testing.assert_allclose(a, b)
def test_matrix_ops(dtype, method): backend = tensorflow_backend.TensorFlowBackend() matrix = backend.randn((4, 4), dtype=dtype, seed=10) matrix1 = getattr(backend, method)(matrix) matrix2 = getattr(tf.linalg, method)(matrix) np.testing.assert_almost_equal(matrix1.numpy(), matrix2.numpy())
def test_random_uniform_seed(dtype): test = tf.test.TestCase() backend = tensorflow_backend.TensorFlowBackend() a = backend.random_uniform((4, 4), seed=10, dtype=dtype) b = backend.random_uniform((4, 4), seed=10, dtype=dtype) test.assertAllCloseAccordingToType(a, b)
def test_shape_tuple(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(np.ones([2, 3, 4])) actual = backend.shape_tuple(a) assert actual == (2, 3, 4)
def test_reshape(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(np.ones((2, 3, 4))) actual = backend.shape_tuple(backend.reshape(a, np.array((6, 4, 1)))) assert actual == (6, 4, 1)
def test_sqrt(): backend = tensorflow_backend.TensorFlowBackend() a = backend.convert_to_tensor(np.array([4., 9.])) actual = backend.sqrt(a) expected = np.array([2, 3]) np.testing.assert_allclose(expected, actual)
def test_matrix_inv_raises(dtype): backend = tensorflow_backend.TensorFlowBackend() matrix = backend.randn((4, 4, 4), dtype=dtype, seed=10) with pytest.raises(ValueError): backend.inv(matrix)
def test_eye(dtype): backend = tensorflow_backend.TensorFlowBackend(dtype=dtype) a = backend.eye(N=4, M=5) np.testing.assert_allclose(tf.eye(num_rows=4, num_columns=5, dtype=dtype), a)
def test_eigs_not_implemented(): backend = tensorflow_backend.TensorFlowBackend() with pytest.raises(NotImplementedError): backend.eigs(np.ones((2, 2)))
def test_randn(dtype): backend = tensorflow_backend.TensorFlowBackend(dtype=dtype) a = backend.randn((4, 4)) assert a.shape == (4, 4)
def test_backend_dtype_exception(): backend = tensorflow_backend.TensorFlowBackend(dtype=tf.float32) tensor = np.random.rand(2, 2, 2) with pytest.raises(TypeError): _ = backend.convert_to_tensor(tensor)