如何在Tensorflow和python中創建帶有索引和值的稀疏向量?

2024-04-25 06:54:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我有两个张量,像这样:

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

如何从中创建SparseTensor?xx\u idx是索引,xx\u 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

Tags: inhomelibpackagestftensorflowlinesite
1条回答
网友
1楼 · 发布于 2024-04-25 06:54:15

问题已解决

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

相关问题 更多 >