gpt4 book ai didi

tensorflow - 在 keras.sequential 模型中使用 keras.layers.Add()

转载 作者:行者123 更新时间:2023-11-30 08:48:25 25 4
gpt4 key购买 nike

使用 TF 2.0 和 tfp 概率层,我构建了一个 keras.sequential模型。我想将其导出以使用 TensorFlow Serving 进行服务,并且我想在可服务中包含预处理和后处理步骤

我的预处理步骤相当简单——用显式值填充 NA,将一些字符串编码为 float ,规范化输入,以及反规范化输出。对于培训,我一直在使用 pandas 和 numpy 进行前/后处理。

我知道我可以导出 Keras 模型的权重,将 keras.sequential 包装起来在更大的 TensorFlow 图中模型的架构,使用像 tf.math.subtract(inputs, vector_of_feature_means) 这样的低级操作要进行前/后处理操作,请定义 tf.placeholders用于我的输入和输出,并提供服务,但我觉得必须有一种更简洁的方法来做到这一点

是否可以使用keras.layers.Add()keras.layers.Multiply()keras.sequence显式预处理步骤的模型,或者是否有一些更标准的方法来做这些事情?

最佳答案

根据我的理解,执行这些操作的标准且有效的方法是使用 Tensorflow Transform。这并不意味着如果我们必须使用 TF Transform,我们就应该使用整个 TFX Pipeline。 TF Transform 也可以独立使用。

Tensorflow Transform 创建一个 Beam Transormation Graph,它将这些变换作为 Tensorflow Graph 中的常量注入(inject)。由于这些转换在图中表示为常量,因此它们在训练和服务中将保持一致。培训和服务一致性的优点是

  1. 消除培训与服务之间的偏差
  2. 无需在服务系统中添加代码,从而缩短了延迟时间。

TF 变换的示例代码如下:

导入所有依赖项的代码:

try:
import tensorflow_transform as tft
import apache_beam as beam
except ImportError:
print('Installing TensorFlow Transform. This will take a minute, ignore the warnings')
!pip install -q tensorflow_transform
print('Installing Apache Beam. This will take a minute, ignore the warnings')
!pip install -q apache_beam
import tensorflow_transform as tft
import apache_beam as beam

import tensorflow as tf
import tensorflow_transform.beam as tft_beam
from tensorflow_transform.tf_metadata import dataset_metadata
from tensorflow_transform.tf_metadata import dataset_schema

下面提到的是预处理功能,其中我们提到了所有转换。截至目前,TF Transform 尚未提供用于缺失值插补的直接 API。因此,为此,我们必须使用低级 API 编写自己的代码。

def preprocessing_fn(inputs):
"""Preprocess input columns into transformed columns."""
# Since we are modifying some features and leaving others unchanged, we
# start by setting `outputs` to a copy of `inputs.
outputs = inputs.copy()

# Scale numeric columns to have range [0, 1].
for key in NUMERIC_FEATURE_KEYS:
outputs[key] = tft.scale_to_0_1(outputs[key])

for key in OPTIONAL_NUMERIC_FEATURE_KEYS:
# This is a SparseTensor because it is optional. Here we fill in a default
# value when it is missing.
dense = tf.sparse_to_dense(outputs[key].indices,
[outputs[key].dense_shape[0], 1],
outputs[key].values, default_value=0.)
# Reshaping from a batch of vectors of size 1 to a batch to scalars.
dense = tf.squeeze(dense, axis=1)
outputs[key] = tft.scale_to_0_1(dense)

# For all categorical columns except the label column, we generate a
# vocabulary but do not modify the feature. This vocabulary is instead
# used in the trainer, by means of a feature column, to convert the feature
# from a string to an integer id.
for key in CATEGORICAL_FEATURE_KEYS:
tft.vocabulary(inputs[key], vocab_filename=key)

# For the label column we provide the mapping from string to index.
table = tf.contrib.lookup.index_table_from_tensor(['>50K', '<=50K'])
outputs[LABEL_KEY] = table.lookup(outputs[LABEL_KEY])

return outputs

您可以引用下面提到的链接以获取详细信息和 TF 变换教程。

https://www.tensorflow.org/tfx/transform/get_started

https://www.tensorflow.org/tfx/tutorials/transform/census

关于tensorflow - 在 keras.sequential 模型中使用 keras.layers.Add(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55284828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com