def sg_leaky_relu(x, opt): r""""See [Xu, et al. 2015](https://arxiv.org/pdf/1505.00853v2.pdf) Args: x: A tensor opt: name: A name for the operation (optional). Returns: A `Tensor` with the same type and shape as `x`. """ return tf.where(tf.greater(x, 0), x, 0.01 * x, name=opt.name)
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))