【问题标题】:Initializing Tensors初始化张量
【发布时间】:2017-03-07 18:24:21
【问题描述】:
tf_coo = tf.SparseTensor(indices=np.array([[0, 0, 0, 1, 1, 2, 3, 9],
                                            [1, 4, 9, 9, 9, 9, 9, 9]]).T,
                        values=[1, 2, 3, 5,1,1,1,1],
                        shape=[10, 10])

我收到错误消息

InvalidArgumentError (see above for traceback): indices[4] = [1,9] is repeated
     [[Node: SparseToDense = SparseToDense[T=DT_INT32, Tindices=DT_INT64, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](SparseTensor/indices, SparseToDense/output_shape, SparseTensor/values, SparseToDense/default_value)]] 

难道不能只为它们构造两个索引和值列表吗?我之前使用过 coo_matrix ,它很好地解决了这个问题。有什么帮助吗?

编辑: 我通过创建一个使用函数 sort_indices() 的 csr_matrix 来解决它,然后将其转换为 coo_matrix。从那里我只创建一个 SparseTensor

 tf.SparseTensor(indices= (coo_martix.row, coo_martix.col), values= coo_matrix.data, dense_shape=coo_martix.shape)

【问题讨论】:

  • 你应该把你的解决方案作为答案。

标签: tensorflow sparse-matrix


【解决方案1】:

只需删除indices中重复的[1,9]即可:

from __future__ import print_function
import tensorflow as tf
import numpy as np
tf_coo = tf.SparseTensor(indices=np.array([[0, 0, 0, 1, 2, 3, 9],
                                            [1, 4, 9, 9, 9, 9, 9]]).T,
                        values=[1, 2, 3, 1,1,1,1],
                        dense_shape=[10, 10])

print('tf_coo: {0}'.format(tf_coo))
print('tf_coo.get_shape(): {0}'.format(tf_coo.get_shape()))

【讨论】:

  • 是的,但我得到的数据包含双精度数据,并且使用 coo_matrix 它只是将新索引添加到现有元素。因此,如果第一个 [1,9] = 4 和第二个 [1,9] = 1,矩阵将显示 5。
  • @MarcusLagerstedt 我明白了。我猜 SparseTensor 的行为不同。
  • 我收到类似“AttributeError: type object 'coo_matrix' has no attribute 'rows'”的错误消息
猜你喜欢
  • 2016-02-16
  • 2019-07-30
  • 2016-09-15
  • 1970-01-01
  • 1970-01-01
  • 2017-12-10
  • 1970-01-01
  • 2020-06-08
  • 1970-01-01
相关资源
最近更新 更多