Tensorflow 如何从稀疏张量中仅获取非零值
利用TensorFlow的哈希表查找实现,我使用提供的默认值返回SparseTensor。我想清除它,得到一个没有默认值的SparseTensorTensorflow 如何从稀疏张量中仅获取非零值,tensorflow,Tensorflow,利用TensorFlow的哈希表查找实现,我使用提供的默认值返回SparseTensor。我想清除它,得到一个没有默认值的SparseTensor 如何清除该默认值?为了实现这一点,我不介意默认值是多少。0很好,-1也很好。tf.sparse\u retain应该可以: def sparse_remove(sparse_tensor, remove_value=0.): return tf.sparse_retain(sparse_tensor, tf.not_equal(a.values,
如何清除该默认值?为了实现这一点,我不介意默认值是多少。0很好,-1也很好。
tf.sparse\u retain
应该可以:
def sparse_remove(sparse_tensor, remove_value=0.):
return tf.sparse_retain(sparse_tensor, tf.not_equal(a.values, remove_value))
例如:
import tensorflow as tf
a = tf.SparseTensor(indices=[[1, 2], [2, 2]], values=[0., 1.], shape=[3, 3])
with tf.Session() as session:
print(session.run([a, sparse_remove(a)]))
打印(我已稍微重新格式化):
[SparseTensorValue(indices=array([[1, 2], [2, 2]]), values=array([ 0., 1.], dtype=float32), shape=array([3, 3])),
SparseTensorValue(indices=array([[2, 2]]), values=array([ 1.], dtype=float32), shape=array([3, 3]))]