如何在 Tensorflow 中表示 3 维稀疏张量? 对于 2 维,我使用
SparseTensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
结果
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
很难展示 3 维的视觉效果,但我会尝试
SparseTensor(indices=[[0, 0, 0], [0, 1, 2]], [1,1,2], values=[1, 2,3], dense_shape=[3, 4,2])
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0],
[0, 0, 0, 0]
[0, 0, 3, 0]
[0, 0, 0, 0]]
这就是我想要的。有人可以帮我解决这个问题吗?我无法理解 SparseTensor 希望我如何编写代码
最佳答案
您只是将 ]
放在了错误的位置。
SparseTensor(indices=[[0, 0, 0], [1, 2, 0], [1,2,1]], values=[1,2,3], dense_shape=[3,4,2])
为您提供所需的矩阵。
根据 docs ,
dense_shape: A 1-D int64 tensor of dense_shape [ndims], which specifies the dense_shape of the sparse tensor. Takes a list indicating the number of elements in each dimension. For example, dense_shape=[3,6] specifies a two-dimensional 3x6 tensor, dense_shape=[2,3,4] specifies a three-dimensional 2x3x4 tensor, and dense_shape=[9] specifies a one-dimensional tensor with 9 elements.
关于python - 在 Tensorflow 中表示 3 维张量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42953282/