Initializing Tensors

时光总嘲笑我的痴心妄想 提交于 2019-12-11 03:38:30

问题


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])

I get the error message

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)]] 

Isn't it possible to jus construct two lists of indexes and values to them? I have used coo_matrix before and it solves this very good. Any help?

EDIT: I solved it by creating a csr_matrix that I used the function sort_indices() then I converted it to coo_matrix. From there I just create a SparseTensor

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

回答1:


Just remove the duplicate [1,9] in 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()))


来源:https://stackoverflow.com/questions/42577773/initializing-tensors

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!