Skip to main content
 首页 » 编程设计

python之在 Tensorflow 中表示 3 维张量

2024年10月01日33unruledboy

如何在 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.