def test_multidimensional_product(self, t1, t2): """Test that the multi-dimensional dot product reduces across the last dimension of the first tensor, and the second-to-last dimension of the second tensor.""" res = fn.dot(t1, t2) expected = np.array([[[[7, 7], [9, 5]], [[15, 15], [21, 11]], [[2, 2], [0, 1]]], [[[23, 23], [33, 17]], [[-3, -3], [-3, -2]], [[5, 5], [9, 4]]]]) assert fn.allequal(res, expected)
def test_allequal(t1, t2): """Test that the allequal function works for a variety of inputs.""" res = fn.allequal(t1, t2) if isinstance(t1, tf.Variable): t1 = tf.convert_to_tensor(t1) if isinstance(t2, tf.Variable): t2 = tf.convert_to_tensor(t2) expected = all(float(x) == float(y) for x, y in zip(t1, t2)) assert res == expected
def test_cast_like(t1, t2): """Test that casting t1 like t2 results in t1 being cast to the same datatype as t2""" res = fn.cast_like(t1, t2) # if tensorflow or pytorch, extract view of underlying data if hasattr(res, "numpy"): res = res.numpy() if hasattr(t2, "numpy"): t2 = t2.numpy() assert fn.allequal(res, t1) assert onp.asarray(res).dtype.type is onp.asarray(t2).dtype.type
def test_convert_tensor_like(self, t1, t2): """Test that converting t1 like t2 results in t1 being cast to the same tensor type as t2""" res = fn.convert_like(t1, t2) # if tensorflow or pytorch, extract view of underlying data if hasattr(res, "numpy"): res = res.numpy() if hasattr(t2, "numpy"): t2 = t2.numpy() assert fn.allequal(res, t1) assert isinstance(res, np.ndarray if isinstance(t2, (list, tuple)) else t2.__class__)
def test_toarray(t): """Test that the toarray method correctly converts the input tensor into a NumPy array.""" res = fn.toarray(t) assert fn.allequal(res, t) assert isinstance(res, onp.ndarray)
def test_matrix_matrix_product(self, t1, t2): """Test that the matrix-matrix dot product of two vectors results in a matrix""" res = fn.dot(t1, t1) assert fn.allequal(res, np.array([[7, 10], [15, 22]]))
def test_vector_matrix_product(self, t1, t2): """Test that the vector-matrix dot product of two vectors results in a vector""" res = fn.dot(t2, t1) assert fn.allequal(res, [27, 40])
def test_matrix_vector_product(self, t1, t2): """Test that the matrix-vector dot product of two vectors results in a vector""" res = fn.dot(t1, t2) assert fn.allequal(res, [20, 46])
def test_vector_product(self, t1, t2): """Test that the dot product of two vectors results in a scalar""" res = fn.dot(t1, t2) assert fn.allequal(res, 14)
def test_convert_scalar(self, t_like): """Test that a python scalar is converted to a scalar tensor""" res = fn.convert_like(5, t_like) assert isinstance(res, t_like.__class__) assert res.ndim == 0 assert fn.allequal(res, [5])
def test_arcsin(t): """Test that the arcsin function works for a variety of input""" res = fn.arcsin(t) assert fn.allequal(res, np.arcsin([1, 0.2, -0.5]))
def test_angle(t): """Test that the angle function works for a variety of input""" res = fn.angle(t) assert fn.allequal(res, [0, np.pi / 2, np.pi / 4])
def test_abs(t): """Test that the absolute function works for a variety of input""" res = fn.abs_(t) assert fn.allequal(res, [1, 2, 5])
def test_conj(t): res = fn.conj(t) assert fn.allequal(res, np.conj(t))