Как создать sparseVector с индексом и значением в Tensorflow, Python? - PullRequest
0 голосов
/ 12 ноября 2018

У меня есть два тензора, вот так:

>>> 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 голосов
/ 13 ноября 2018

проблема решена

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])
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...