def sg_to_sparse(tensor, opt): r"""Converts a dense tensor into a sparse tensor. See `tf.SparseTensor()` in tensorflow. Args: tensor: A `Tensor` with zero-padding (automatically given by chain). opt: name: If provided, replace current tensor's name. Returns: A `SparseTensor`. """ indices = tf.where(tf.not_equal(tensor.sg_float(), 0.)) return tf.SparseTensor(indices=indices, values=tf.gather_nd(tensor, indices) - 1, # for zero-based index dense_shape=tf.shape(tensor).sg_cast(dtype=tf.int64))
def sg_to_sparse(tensor, opt): indices = tf.where(tf.not_equal(tensor.sg_float(), 0.)) return tf.SparseTensor( indices=indices, values=tf.gather_nd(tensor, indices) - 1, # for zero-based index shape=tf.shape(tensor).sg_cast(dtype=tf.int64))