初始化张量

时间:2017-03-03 11:31:42

标签: tensorflow sparse-matrix

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,它解决了这个问题。有什么帮助吗?

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

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

1 个答案:

答案 0 :(得分:0)

只需移除[1,9]中的重复indices

即可
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()))