python - 在 Tensorflow 中表示 3 维张量

标签 python tensorflow

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

相关文章:

python - 使用 matlab 运行 python 脚本

python - 如何检查包含不同数据类型的 pandas 数据框中的负值?

c++ - do{}while(0) 有什么用?

python - 在 pandas DataFrame 中快速应用字符串操作

machine-learning - 用于行为分类的 CNN

python - 如何使 TensorFlow RNN 训练更加稳健?

python - 未实现错误: Cast string to float is not supported

python - 模型编译部分的 RMSprop 优化器,在 keras 中不起作用

python - keras 中 softmax 输出的一个热输入

python - 修改训练好的模型架构并继续训练 Keras