【问题标题】:Using SparseTensor with reinitializable iterator doesn't work将 SparseTensor 与可重新初始化的迭代器一起使用不起作用
【发布时间】:2018-09-13 23:02:37
【问题描述】:

在 Python 3.5.2 中使用 Tensorflow 1.10.1

我有一个 tf.SparseTensor 对象,它是从一组索引元组创建的,所有值都为 1,并且我已经创建了一个这样的数据集

data = SparseTensor(indices = tuples, values= np.ones(len(tuples)), 
    dense_shape=[n_users, n_items])

然后我从它创建了一个迭代器

dataset = tf.data.Dataset.from_tensor_slices(data)

我已经初始化了迭代器

iterator = tf.data.Iterator.from_structure(dataset.output_types, 
    dataset.output_shapes, None, dataset.output_classes)
training_init_op = iterator.make_initializer(dataset)
next_element = iterator.get_next()

我已经非常简单地将网络定义为

input_data = tf.sparse_tensor_to_dense(next_element)
h = tf.layers.dense(input_data, 50)

当我尝试通过调用通过网络传递数据集时

with tf.Session() as sess:
    init_op = tf.group(tf.global_variables_initializer(),
        tf.local_variables_initializer())
    sess.run(init_op)
    sess.run(training_init_op)
    sess.run([h])

我收到以下错误

Traceback (most recent call last):
h = tf.layers.dense(input_data, 50)
File "python3.5/site-packages/tensorflow/python/layers/core.py", line 189, in dense
    return layer.apply(inputs)
File "python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 805, in apply
    return self.__call__(inputs, *args, **kwargs)
File "python3.5/site-packages/tensorflow/python/layers/base.py", line 362, in __call__
    outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
File "python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 720, in __call__
    self._assert_input_compatibility(inputs)
File "python3.5/site-packages/tensorflow/python/keras/engine/base_layer.py", line 1440, in _assert_input_compatibility
    str(x.shape.as_list()))
ValueError: Input 0 of layer dense_1 is incompatible with the layer: : expected min_ndim=2, found ndim=1. Full shape received: [None]

我还注意到数据集的下一个元素缺少列数的维度。在通过迭代器调用之前:

    print(data)
    print(data.get_shape())
    print(data.dense_shape)

了解我

SparseTensor(indices=Tensor("SparseTensor/indices:0", shape=(2451491,2), dtype=int64), 
    values=Tensor("SparseTensor/values:0", shape=(2451491,), dtype=float64), 
    dense_shape=Tensor("SparseTensor/dense_shape:0", shape=(2,), dtype=int64))
(50213, 32392)
Tensor("SparseTensor/dense_shape:0", shape=(2,), dtype=int64)

如果我通过调用在下一个元素上调用相同的:

    print(next_element)
    print(next_element.get_shape())
    print(next_element.dense_shape)

我回来了

SparseTensor(indices=Tensor("DeserializeSparse:0", shape=(?, 1), dtype=int64), 
    values=Tensor("DeserializeSparse:1", shape=(?,), dtype=float64),
    dense_shape=Tensor("DeserializeSparse:2", shape=(1,), dtype=int64))
(?,)
Tensor("DeserializeSparse:2", shape=(1,), dtype=int64)

关于我做错了什么有什么想法吗?

【问题讨论】:

    标签: tensorflow tensorflow-datasets


    【解决方案1】:

    在数据集上调用 get_next() 可以获得稀疏张量中一行的张量。您可以将dataset = dataset.batch(1) 添加到您的代码中,这样您将获得正确维度的行,因为然后数据集将生成一个包含一行的列表(例如返回形状 [1,X] 而不是 [X,])

    【讨论】:

      猜你喜欢
      • 2012-02-26
      • 2018-04-20
      • 2017-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-24
      • 1970-01-01
      • 2021-10-01
      相关资源
      最近更新 更多