tf.sparse.SparseTensor

为什么用tf.sparse.SparseTensor的原因

  • 节约内存

  • 在表达一个稀疏矩阵时候,不用一个很大的矩阵

tf.sparse.SparseTensor(
    indices, values, dense_shape
)

  • indices :非零值的元素的索引

  • values :非零值

  • dense_shape :shape

import tensorflow as tf
sp_input=tf.sparse.SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 3], dense_shape=[3, 4])


tf.sparse.to_dense(
    sp_input, default_value=None, validate_indices=True, name=None
)

<tf.Tensor: shape=(3, 4), dtype=int32, numpy=
array([[1, 0, 0, 0],
       [0, 0, 3, 0],
       [0, 0, 0, 0]], dtype=int32)>
posted @ 2022-08-19 22:51  luoganttcc  阅读(36)  评论(0)    收藏  举报