def test_to_sparse_zero(): shape = [2, 3] data_zero = tf.zeros(shape) sparse_tensor = tx.to_sparse(data_zero) dense = tf.sparse.to_dense(sparse_tensor) num_indices = tf.shape(sparse_tensor.indices)[0] assert num_indices == 0 assert tx.tensor_equal(dense, data_zero)
def test_gather_sparse(): v = tf.constant([[1, 0, 1], [0, 0, 2], [3, 0, 3]], tf.float32) sp = tx.to_sparse(v) indices = np.array([[0, 1], [2, 0]], dtype=np.int64) gather_sp = tx.gather_sparse(sp, indices) gather = tf.sparse.to_dense(gather_sp) expected = tf.constant([[1., 0., 1.], [0., 0., 2.], [3., 0., 3.], [1., 0, 1.]]) assert tx.tensor_equal(gather, expected)
def test_sparse_dense_mul(): x = tf.random.uniform([2, 4]) s = tx.to_sparse(x) v = tf.random.uniform([4, 3]) # matmul mul0 = tf.matmul(x, v) mul1 = tf.sparse.sparse_dense_matmul(s, v) assert tx.tensor_all_close(mul0, mul1) # element-wise mul2 = tx.sparse_dense_multiply(s, x) mul3 = tf.multiply(x, x) assert tx.tensor_all_close(mul2, mul3)
def test_to_sparse(): c = [[1, 0], [2, 3]] sparse_tensor = tx.to_sparse(c) dense_shape = tf.shape(c, out_type=tf.int64) indices = tf.where(tf.not_equal(c, 0)) flat_values = tf.reshape(c, [-1]) flat_indices = tf.where(tf.not_equal(flat_values, 0)) flat_indices = tf.squeeze(flat_indices) flat_indices = tf.math.mod(flat_indices, dense_shape[1]) values = tf.gather_nd(c, indices) sp_indices = tx.sparse_indices(sparse_tensor) assert tx.tensor_equal(sparse_tensor.indices, indices) assert tx.tensor_equal(sp_indices.values, flat_indices) assert tx.tensor_equal(sparse_tensor.values, values)
import tensorflow as tf import tensorx as tx import numpy as np from tensorflow.python.framework import tensor_util v = np.array([[1, 0, 1], [0, 0, 2], [3, 0, 3], [7, 0, 0]], dtype=np.float32) ids = np.array([[1, 0], [1, 3]], dtype=np.int64) print(" dense representation \n ", v) sp = tx.to_sparse(v) tf.InteractiveSession() print(sp.eval()) flat_ids = tf.reshape(ids, [-1]) num_elems = tf.shape(flat_ids)[0] print("gather_ids: ", flat_ids.eval()) # COUNT COLUMNS sp_cols = tx.sparse_ones(sp.indices, sp.dense_shape, dtype=tf.int64) col_count = tf.sparse_reduce_sum(sp_cols, axis=-1) col_count_cs = tf.cumsum(col_count) start_coord = col_count_cs - col_count print("#cols ", col_count.eval()) print("cumsum #cols ", col_count_cs.eval()) print("start coord ", start_coord.eval()) current_coors = None
import tensorflow as tf import tensorx as tx import numpy as np from tensorflow.python.ops.sparse_ops import sparse_dense_cwise_mul import time ss = tf.InteractiveSession() v = np.random.uniform(-1, 1, [3, 6]) v = tf.constant(v, dtype=tf.float32) print("dense tensor \n ", v.eval()) # create a dummy sparse tensor sp_v = tx.to_sparse(v) print(tf.shape(sp_v).eval()) # suppose se are reshaping from a concatenated tensor that represents # a sequence to a batch of sequences of feature vectors # print(tf.shape(sp_v_reshaped).eval()) # suppose now that we want to use broadcasting with the sparse_tensor weights = tf.constant([[1., 0.5], [1., -1.], [1., -1.]], dtype=tf.float32) weights_r = tx.repeat(weights, 3) print(weights_r.eval()) # weights = tf.expand_dims(weights, -1) out = tx.sparse_multiply(sp_v, weights_r) dense_out = tf.sparse_tensor_to_dense(out)