用from_generator创建一个TF数据集的SparseTensors。

问题描述 投票:0回答:0

我有一个发电机,产生 tf.sparse.SparseTensors. 我想把这个变成一个Tensorflow Dataset,但是遇到了一些问题。 我使用的是TF2。 首先,与普通的Tensors不同,你不能简单地将它们传递进来(并为output_types提供正确的数据类型)。 对于一个稀疏张量的 [1,0,0,0,5,0],错误看起来像

tensorflow.python.framework.errors_impl.InvalidArgumentError: TypeError: `generator` yielded an element that could not be converted to the expected type. The expected type was int64, but the yielded element was SparseTensor(indices=tf.Tensor(
E     [[0]
E      [4]], shape=(2, 1), dtype=int64), values=tf.Tensor([1 5], shape=(2,), dtype=int64), dense_shape=tf.Tensor([6], shape=(1,), dtype=int64)).

在网上找了一下,发现了这个公开的问题,并尝试着做了类似的事情。https:/github.comtensorflowtensorflowissues16689。 - 将指数、值和形状作为单独的张量读入TF数据集,然后在数据集上映射以创建稀疏张量。 这在github问题中的一些例子中是行不通的----。tf.sparse.SparseTensor(indices, values, shape) 似乎不接受tf.Tensor形式的索引和形状--它很乐意接受一个list或numpy数组,但不接受Tensor。 因为 map 不急,我也不能叫 .numpy() Tensor上也没有。 有什么最好的方法可以让它工作? 我看到有 tf.py_functiontf.numpy_function 这可能会有帮助,但对于我的用例来说,构建输出类型可能会很棘手(尽管不是不可能)--传入的数据不是固定的,可能会有稀疏和密集型的混合。

tensorflow dataset sparse-matrix
© www.soinside.com 2019 - 2024. All rights reserved.