【问题标题】:Feeding nullable data from BigQuery into Tensorflow Transform将 BigQuery 中的可为空数据馈送到 TensorFlow 转换
【发布时间】:2020-05-08 21:36:47
【问题描述】:

我们正在尝试构建一个管道,该管道从 BigQuery 获取数据,通过 TensorFlow Transform 运行,然后在 TensorFlow 中进行训练。

管道已启动并正在运行,但我们在 BigQuery 中处理空值时遇到了困难。

我们正在使用 Beam 从 BigQuery 加载:

    raw_data = (pipeline
                | '{}_read_from_bq'.format(step) >> beam.io.Read(
                    beam.io.BigQuerySource(query=source_query,
                                           use_standard_sql=True,
                                           )))

我正在使用数据集元数据,尝试FixedLenFeatureVarLenFeature 用于各种列:

    # Categorical feature schema
    categorical_features = {
        column_name: tf.io.FixedLenFeature([], tf.string) for column_name in categorical_columns
    }
    raw_data_schema.update(categorical_features)

    # Numerical feature schema
    numerical_features = {
        column_name: tf.io.VarLenFeature(tf.float32) for column_name in numerical_columns
    }
    raw_data_schema.update(numerical_features)

    # Create dataset_metadata given raw_data_schema
    raw_metadata = dataset_metadata.DatasetMetadata(
        schema_utils.schema_from_feature_spec(raw_data_schema))

正如预期的那样,如果您尝试将 BigQuery NULL 输入到 FixedLenFeature,它会中断。

但是,当我尝试将字符串或整数输入 VarLenFeature 时,它也会中断。这似乎是因为 VarLenFeature 需要一个列表,但 BigQuerySource 提供了一个 Python 原语。它中断的确切点在这里(错误来自我尝试使用整数时):

File "/usr/local/lib/python3.7/site-packages/tensorflow_transform/impl_helper.py", line 157, in <listcomp>
indices = [range(len(value)) for value in values]
TypeError: object of type 'int' has no len()
[while running 'train_transform/AnalyzeDataset/ApplySavedModel[Phase0]/ApplySavedModel/ApplySavedModel']

当我使用我的字符串输入尝试 VarLenFeature 时,例如"UK",输出是这样的 SparseTensor:

SparseTensorValue(indices=[(0, 0), (0, 1)], values=['U', 'K'], dense_shape=(1, 2))

所以看来我需要将一个列表传递给 VarLenFeature 才能使其工作,但 BigQuerySource 默认情况下不这样做。

有没有一种简单的方法来实现这一点?还是我完全错过了从 BigQuery 读取可空列的标记?

非常感谢您!

【问题讨论】:

    标签: python tensorflow google-bigquery apache-beam tensorflow-transform


    【解决方案1】:

    您可能需要自己处理 NULL(缺失)值。对于数值列,您可以将 NULL 替换为平均值或中位数。对于分类列 (STRING),您可以使用一些默认值,如空 STRING 或新值作为缺失值指示符。

    我对 VarLenFeature 不是很熟悉,但您可能可以替换 source_query 中的 NULL(NULL 插补)。比如:

    IFNULL(col, col_mean) AS col_imputed

    缺点是您必须先使用 sql 计算 col_mean 并在此处将其填充为常数。另一件事是您需要记住这个平均值并在预测中应用相同的平均值,因为它不是 tf.transform(您的图表)的一部分。

    Bigquery 本身将 BQML 作为 ML 平台。他们确实支持TRANSFORM and automatic imputation。也许你也可以看看:)

    【讨论】:

    • 非常感谢您的回复!我们正在将此作为后备选项来实施。我们的愿望是在 Tensorflow Transform 中实现插补,因为它似乎是一个预期的用例,至少从“人口普查”示例来看:tensorflow.org/tfx/tutorials/transform/… 看看他们如何对待他们的OPTIONAL_NUMERIC_FEATURE_KEYS - 它似乎对我们不太适用不过这种情况......我们将在 BQ 中实施插补,直到我们有时间找出 VarLenFeature 问题:)
    • 我们已经根据一些实验和一些进一步的反馈达成了妥协。 (1) 如果您提到的注意事项可以在 BQ 中执行插补,我们使用此方法:SQL 中的IFNULL(...)FixedLenFeature。 (2)需要TFT插补的地方,我们对BQ中的列进行操作转换为数组:CASE WHEN col IS NULL THEN NULL ELSE ARRAY(SELECT col) END AS col,并使用VarLenFeature
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-15
    • 2019-09-21
    • 1970-01-01
    相关资源
    最近更新 更多