2017-03-03 97 views
2
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) 
+0

你應該把你的解決方案作爲一個答案。 – daknowles

回答

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())) 
+0

是的,但我得到的數據包含雙打和coo_matrix它只是將新的索引添加到現有的元素。所以如果第一[1,9] = 4和第二[1,9] = 1矩陣將顯示5. –

+0

@MarcusLagerstedt我看到。我猜SparseTensor的行爲有所不同。 –

+0

我收到了錯誤消息,如「AttributeError:type object'coo_matrix'沒有屬性'rows'」 – mgokhanbakal