def test_tf_sparse(idx_shape): import tensorflow as tf from tensorflow import SparseTensor from jina.types.ndarray.sparse.tensorflow import SparseNdArray a = SparseTensor(indices=idx_shape[0], values=[1, 2, 3], dense_shape=idx_shape[1]) b = SparseNdArray() b.value = a np.testing.assert_equal(tf.sparse.to_dense(b.value).numpy(), tf.sparse.to_dense(a).numpy())
def test_dummy_numpy_sparse(shape): a = np.random.random(shape) a[a > 0.5] = 1 from jina.types.ndarray.sparse.numpy import SparseNdArray b = SparseNdArray() b.value = a np.testing.assert_almost_equal(a, b.value)
def test_torch_sparse(idx_shape, transpose=False): from jina.types.ndarray.sparse.pytorch import SparseNdArray import torch i = torch.LongTensor(idx_shape[0]) v = torch.FloatTensor([3, 4, 5]) a = torch.sparse.FloatTensor(i, v, torch.Size(idx_shape[1])) b = SparseNdArray(transpose_indices=transpose) b.value = a np.testing.assert_equal(b.value.to_dense().numpy(), a.to_dense().numpy())
def test_scipy_sparse(sp_format): from scipy.sparse import coo_matrix from jina.types.ndarray.sparse.scipy import SparseNdArray row = np.array([0, 3, 1, 0]) col = np.array([0, 3, 1, 2]) data = np.array([4, 5, 7, 9]) a = coo_matrix((data, (row, col)), shape=(4, 4)) dense_a = a.toarray() b = SparseNdArray(sp_format=sp_format) # write to proto b.value = a # read from proto dense_b = b.value.toarray() np.testing.assert_equal(dense_b, dense_a)