问题
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