繁体   English   中英

将可空数据从 BigQuery 馈送到 Tensorflow Transform

[英]Feeding nullable data from BigQuery into Tensorflow Transform

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

管道已启动并正在运行,但我们在 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 读取可为空列的标记?

非常感谢您!

您可能需要自己处理 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 和自动插补 也许你也可以看看:)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM