TensorFlow /. , , , , .
def dense_to_sparse(dense_tensor):
where_dense_non_zero = tf.where(tf.not_equal(dense_tensor, 0))
indices = where_dense_non_zero
values = tf.gather_nd(dense_tensor, where_dense_non_zero)
shape = dense_tensor.get_shape()
return tf.SparseTensor(
indices=indices,
values=values,
shape=shape
)
, . , .
tf.sparse_to_dense, . , [2, 1, 0], . , 0:
indices = tf.where(tf.not_equal(dense_tensor, 0))
, /:
output = indices[:, 1]
, 1 - 1. , , - :
output = indices[:, len(dense_tensor.get_shape()) - 1]
, ( , ). , !
EDIT : Yaroslav's answer is better if you are looking for indexes / locations, where is the input tensor if 1; it will not expand for tensors with non-1/0 values, if necessary.
source
share