Python 无效RGUMERROR TensorFlow稀疏到稠密

Python 无效RGUMERROR TensorFlow稀疏到稠密,python,machine-learning,tensorflow,deep-learning,Python,Machine Learning,Tensorflow,Deep Learning,我有一个稀疏张量,我正在从一组索引和值中建立它。我试图实现一些代码来获取完整的行切片。尽管TensorFlow中似乎不直接支持此功能,但似乎有一些变通方法可以返回指定行的索引和值,如下所示: def sparse_slice(self, indices, values, needed_row_ids): needed_row_ids = tf.reshape(needed_row_ids, [1, -1]) num_rows = tf.shape(indices)[0]

我有一个稀疏张量,我正在从一组索引和值中建立它。我试图实现一些代码来获取完整的行切片。尽管TensorFlow中似乎不直接支持此功能,但似乎有一些变通方法可以返回指定行的索引和值,如下所示:

def sparse_slice(self, indices, values, needed_row_ids):
    needed_row_ids = tf.reshape(needed_row_ids, [1, -1])
    num_rows = tf.shape(indices)[0]
    partitions = tf.cast(tf.reduce_any(tf.equal(tf.reshape(indices[:, 0], [-1, 1]), needed_row_ids), 1),
                         tf.int32)
    rows_to_gather = tf.dynamic_partition(tf.range(num_rows), partitions, 2)[1]
    slice_indices = tf.gather(indices, rows_to_gather)
    slice_values = tf.gather(values, rows_to_gather)
    return slice_indices, slice_values
然后像这样在稀疏4x4矩阵上直接调用,我感兴趣的是访问第3行中的所有元素:

      with tf.Session().as_default():
                indices = tf.constant([[0, 0], [1, 0], [2, 0], [2, 1], [3, 0], [3, 3]])
                values = tf.constant([10, 19, 1, 1, 6, 5], dtype=tf.int64)

                needed_row_ids = tf.constant([3])
                slice_indices, slice_values = self.sparse_slice(indices, values, needed_row_ids)

                  print('indicies: {} and rows: {}'.format(slice_indices.eval(), slice_values.eval()))
其输出如下:

indicies: [[3 0]
 [3 3]] and rows: [6 5]
到目前为止还不错,我想我可以利用这些信息构造一个1x4密集张量,索引处的值和缺失列的0

  dense_representation = tf.sparse_to_dense(sparse_values=slice_values, sparse_indices=slice_indices,
                                                   output_shape=(1,4))
然而,当我在一个会话中运行张量时

sess = tf.Session()
sess.run(dense_representation)
我收到以下例外情况:

InvalidArgumentError (see above for traceback): indices[0] = [3,0] is out of bounds: need 0 <= index < [1,4]
     [[Node: SparseToDense = SparseToDense[T=DT_INT64, Tindices=DT_INT32, validate_indices=true, _device="/job:localhost/replica:0/task:0/cpu:0"](Gather_2, SparseToDense/output_shape, Gather_3, SparseToDense/default_value)]]

InvalidArgumentError(回溯见上文):索引[0]=[3,0]超出范围:需要0您是否设法解决了此问题?