[英]Apply TensorFlow Transform to transform/scale features in production
我按照以下指南编写了TF Records,我使用tf.Transform
来预处理我的功能。 现在,我想部署我的模型,我需要在真实的实时数据上应用这个预处理功能。
首先,假设我有2个功能:
features = ['amount', 'age']
我有来自Apache Beam的transform_fn
,驻留在working_dir=gs://path-to-transform-fn/
然后我使用以下方法加载转换函数:
tf_transform_output = tft.TFTransformOutput(working_dir)
我认为在生产中服务的最简单方法是获取一个处理数据的numpy数组,并调用model.predict()
(我使用的是model.predict()
模型)。
为此,我认为transform_raw_features()
方法正是我所需要的。
但是,似乎在构建架构之后:
raw_features = {}
for k in features:
raw_features.update({k: tf.constant(1)})
print(tf_transform_output.transform_raw_features(raw_features))
我明白了:
AttributeError: 'Tensor' object has no attribute 'indices'
现在,我假设发生了这种情况,因为我在preprocessing_fn
定义了schema时使用了tf.VarLenFeature()
。
def preprocessing_fn(inputs):
outputs = inputs.copy()
for _ in features:
outputs[_] = tft.scale_to_z_score(outputs[_])
我使用以下方法构建元数据:
RAW_DATA_FEATURE_SPEC = {}
for _ in features:
RAW_DATA_FEATURE_SPEC[_] = tf.VarLenFeature(dtype=tf.float32)
RAW_DATA_METADATA = dataset_metadata.DatasetMetadata(
dataset_schema.from_feature_spec(RAW_DATA_FEATURE_SPEC))
所以简而言之,给一本字典:
d = {'amount': [50], 'age': [32]}
,我想应用此transform_fn
,并适当缩放这些值以输入到我的模型中进行预测。 在pre_processing()
函数处理数据之前,这个字典正是我的PCollection
的格式。
class BeamProccess():
def __init__(self):
# init
self.run()
def run(self):
def preprocessing_fn(inputs):
# outputs = { 'id' : [list], 'amount': [list], 'age': [list] }
return outputs
with beam.Pipeline(options=self.pipe_opt) as p:
with beam_impl.Context(temp_dir=self.google_cloud_options.temp_location):
data = p | "read_table" >> beam.io.Read(table_bq) \
| "create_data" >> beam.ParDo(ProcessFn())
transformed_dataset, transform_fn = (
(train, RAW_DATA_METADATA) | beam_impl.AnalyzeAndTransformDataset(
preprocessing_fn))
transformed_data, transformed_metadata = transformed_dataset
transformed_data | "WriteTrainTFRecords" >> tfrecordio.WriteToTFRecord(
file_path_prefix=self.JOB_DIR + '/train/data',
file_name_suffix='.tfrecord',
coder=example_proto_coder.ExampleProtoCoder(transformed_metadata.schema))
_ = (
transform_fn
| 'WriteTransformFn' >>
transform_fn_io.WriteTransformFn(path=self.JOB_DIR + '/transform/'))
最后ParDo()
是:
class ProcessFn(beam.DoFn):
def process(self, element):
yield { 'id' : [list], 'amount': [list], 'age': [list] }
问题在于代码段
raw_features = {}
for k in features:
raw_features.update({k: tf.constant(1)})
print(tf_transform_output.transform_raw_features(raw_features))
在此代码中,您构造了一个字典,其中值是张量。 就像你说的,这不适用于VarLenFeature
。 而不是使用tf.constant
尝试使用tf.placeholder
为AA FixedLenFeature
和tf.sparse_placeholder
的VarLenFeature
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.