使用张量流tf变换的数据标准化

时间:2017-09-28 17:02:59

标签: python tensorflow google-cloud-platform google-cloud-ml tensorflow-transform

我正在使用Tensorflow使用我自己的数据集进行神经网络预测。我做的第一个是与我的计算机中的小数据集一起使用的模型。在此之后,我更改了代码,以便使用具有更大数据集的Google Cloud ML-Engine在ML-Engine中实现列车和预测。

我正在规范熊猫数据框中的功能,但这会引入偏差,我的预测结果很差。

我真正想要的是使用库preprocessing_fn来规范化图表中的数据。为此,我想创建一个函数tft.scale_to_0_1并使用“Feature: Sample Feature File Scenario: An international coffee shop must handle currencies Given the price list for an international coffee shop | product | currency | price | | coffee | EUR | 1 | | donut | SEK | 18 | When I buy 1 coffee and 1 donut Then should I pay 1 EUR and 18 SEK Scenario Outline: eating Given there are <start> cucumbers When I eat <eat> cucumbers Then I should have <left> cucumbers Examples: | start | eat | left | | 12 | 5 | 7 | | 20 | 5 | 15 | ”。 https://github.com/tensorflow/transform/blob/master/getting_started.md

我发现的主要问题是当我试图做预测时。我正在寻找互联网,但我没有找到任何导出模型的例子,其中数据在训练中被标准化。在我发现的所有示例中,数据未在任何地方进行标准化。

我想知道的是如果我对训练中的数据进行规范化并发送带有新数据的新实例来进行预测,那么如何对这些数据进行归一化?

¿可能在Tensorflow数据管道中?用于标准化的变量是否保存在某个地方?

总结:我正在寻找一种方法来规范化模型的输入,然后新实例也变得标准化。

1 个答案:

答案 0 :(得分:11)

首先,你真的不需要tf.transform。您需要做的就是编写一个函数,您可以从training / eval input_fn和服务input_fn中调用它。

例如,假设您已在整个数据集中使用Pandas来计算最小值和最大值

```
def add_engineered(features):
  min_x = 22
  max_x = 43
  features['x'] = (features['x'] - min_x) / (max_x - min_x)
  return features
```

然后,在你的input_fn中,通过调用add_engineered来包装你返回的功能:

```
def input_fn():
  features = ...
  label = ...
  return add_engineered(features), label
```

并在您的serving_input fn中,确保通过调用add_engineered类似地包装返回的要素(而不是feature_placeholders):

```
def serving_input_fn():
    feature_placeholders = ...
    features = feature_placeholders.copy()
    return tf.estimator.export.ServingInputReceiver(
         add_engineered(features), feature_placeholders)
```

现在,您在预测时的JSON输入只需要包含原始的,未缩放的值。

这是这种方法的完整工作示例。

https://github.com/GoogleCloudPlatform/training-data-analyst/blob/master/courses/machine_learning/feateng/taxifare/trainer/model.py#L130

tf.transform提供了一个两阶段过程:计算最小值,最大值和图形修改步骤的分析步骤,将您的缩放比例插入到TensorFlow图形中。因此,要使用tf.transform,首先需要编写一个Dataflow管道进行分析,然后在TensorFlow代码中插入对tf.scale_0_to_1的调用。这是一个这样做的例子:

https://github.com/GoogleCloudPlatform/cloudml-samples/tree/master/criteo_tft

add_engineered()方法更简单,是我的建议。如果您的数据分布随时间变化,则需要采用tf.transform方法,因此您希望自动化整个管道(例如,用于连续培训)。

相关问题