【发布时间】: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