如何在Tensorflow中创建带有索引和值的sparseVector,python?

时间:2018-11-12 13:01:40

标签: python tensorflow vector

我有两个张量,像这样:

>>> xx_idx
<tf.Tensor 'Placeholder:0' shape=(100, ?) dtype=int64>
>>> xx_val
<tf.Tensor 'Placeholder_1:0' shape=(100, ?) dtype=float64>

如何用它们创建一个SparseTensor? xx_idx是索引,xx_val是值。 有100个样本。 向量的维数未知,可能是22000。

我尝试过:

xx_vec = tf.SparseTensor(xx_idx, xx_val, 25000)

但是出现错误:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'user_idx' is not defined
>>> xx_vec = tf.SparseTensor(xx_idx, xx_val, 25000)
Traceback (most recent call last):
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 671, in merge_with
    self.assert_same_rank(other)
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 716, in assert_same_rank
    other))
ValueError: Shapes (100, ?) and (?,) must have the same rank

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 746, in with_rank
    return self.merge_with(unknown_shape(ndims=rank))
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 677, in merge_with
    raise ValueError("Shapes %s and %s are not compatible" % (self, other))
ValueError: Shapes (100, ?) and (?,) are not compatible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/sparse_tensor.py", line 133, in __init__
    values_shape = values.get_shape().with_rank(1)
  File "/home/work/tf/lib/python3.5/site-packages/tensorflow/python/framework/tensor_shape.py", line 748, in with_rank
    raise ValueError("Shape %s must have rank %d" % (self, rank))
ValueError: Shape (100, ?) must have rank 1

1 个答案:

答案 0 :(得分:0)

问题解决了

import tensorflow as tf
from tensorflow import TensorShape, Dimension


class GetVector:

    @classmethod
    def get_sparse_vector(cls, idx_all_0, val_all, dim_num):
        batch_size = idx_all_0.shape[0].value
        '''
        cur_idx = tf.placeholder(tf.int64, [batch_size, None, 1])
        cur_val = tf.placeholder(tf.float64, [batch_size, None])
        cur_vec = tf.placeholder(tf.float64, [batch_size, dim_num])
        '''
        idx_all = cls.idx_reform(idx_all_0)

        ans = []
        for i in range(batch_size):
            cur_idx = idx_all[i]
            cur_val = val_all[i]
            cur_vec = tf.SparseTensor(cur_idx, cur_val, TensorShape(Dimension(dim_num)))
            cur_vec_tensor = tf.sparse_tensor_to_dense(cur_vec)
            ans = tf.concat([ans, cur_vec_tensor], 0)
        ans = tf.reshape(ans, [batch_size, dim_num])
        return ans

    @classmethod
    def idx_reform(cls, idx_all):
        batch_size = idx_all.shape[0].value
        return tf.reshape(idx_all, [batch_size, -1, 1])