具有混合数据类型的 TensorFlow 数据集生成器

TensorFlow Dataset Generator With Mixed Datatypes

我正在使用 TensorFlow 数据集 API (https://www.tensorflow.org/guide/datasets) and in particular, i'm using it with the TensorFlow Estimators API (https://www.tensorflow.org/guide/datasets_for_estimators),它建议使用生成器函数。

我在编写生成具有不同输出类型(例如,int、float 和 string 的混合)的特征的生成器函数时遇到了问题。我已经想出了如何指定不同于生成器...但仅当所有标签类型都相同时。

但是...假设您要发出各种特征类型(例如,在典型的 imports85 TensorFlow 演示中,您将发出汽车品牌和型号作为字符串(稍后分类 downstream) 以及 Highway-MPG 作为 float32 和 number-of-doors 作为 int。如何在数据集上指定 from_generator 调用各种特征类型?

dataset = tf.data.Dataset. from_generator(generator=self._generator, output_types=(tf.float32, tf.int32), output_shapes=(tf.TensorShape([None]),tf.TensorShape([1])))

我已经尝试过明显的使用方法 output_types=((tf.float32, tf.float32, tf.string, tf.string), tf.int32) 没有运气。任何帮助将不胜感激。

来自official documentation

tf.Tensor 不可能有超过一种数据类型。但是,可以将任意数据结构序列化为字符串并将其存储在 tf.Tensors.

因此您可能需要将它们存储为字符串,然后使用 decode_raw 等函数对其进行计算。