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

标签 tensorflow tensorflow-datasets tensorflow-estimator

我正在使用 TensorFlow Datasets API ( https://www.tensorflow.org/guide/datasets ),特别是,我将它与建议使用生成器函数的 TensorFlow Estimators API ( https://www.tensorflow.org/guide/datasets_for_estimators ) 一起使用。

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

但是...假设您有多种特征类型要发出(例如,在典型的imports85 TensorFlow 演示中,您将发出汽车品牌和型号作为字符串(稍后将在下游分类)以及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])))



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

最佳答案

来自 official documentation :

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

因此,您可能需要将它们存储为字符串,然后使用诸如 decode_raw 之类的函数来评估它们。例如。

关于具有混合数据类型的 TensorFlow 数据集生成器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52450788/

相关文章:

python - Tensorflow 图像分类脚本

python - TensorFlow:tf.add_n 的乘积等价物

tensorflow - 将迭代器(来自 tf.data.Dataset)中的元素提供给 TensorFlow 模型的有效方法是什么?

python - tf.data.Dataset : how to get the dataset size (number of elements in a epoch)?

python - 读入 tf.dataset 时的一种热编码

tensorflow - 寻求有关 "running"Tensorflow 模型的说明

python - Tensorflow 2.0 Keras 的训练速度比 2.0 Estimator 慢 4 倍

tensorflow : serving model return always the same prediction

python - 如何使用tensorflow读取Excel文件?

python - Tensorflow 估计器 : how to use tf. graph_util.convert_variables_to_constants