【问题标题】:How to export a Tensorflow model with exogenous features如何导出具有外生特征的 TensorFlow 模型
【发布时间】:2018-03-16 18:56:18
【问题描述】:

我尝试导出 Tensorflow 模型,但找不到将外生特征添加到 tf.contrib.timeseries.StructuralEnsembleRegressor.build_raw_serving_input_receiver_fn 的最佳方法。

我使用来自 Tensorflow 贡献者的示例:https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/timeseries/examples/known_anomaly.py,我只是尝试保存模型。

# this is the exogenous column 
string_feature = tf.contrib.layers.sparse_column_with_keys(
      column_name="is_changepoint", keys=["no", "yes"])

one_hot_feature = tf.contrib.layers.one_hot_column(
      sparse_id_column=string_feature)

estimator = tf.contrib.timeseries.StructuralEnsembleRegressor(
      periodicities=12,    
      cycle_num_latent_values=3,
      num_features=1,
      exogenous_feature_columns=[one_hot_feature],
      exogenous_update_condition=
      lambda times, features: tf.equal(features["is_changepoint"], "yes"))

reader = tf.contrib.timeseries.CSVReader(
      csv_file_name,

      column_names=(tf.contrib.timeseries.TrainEvalFeatures.TIMES,
                    tf.contrib.timeseries.TrainEvalFeatures.VALUES,
                    "is_changepoint"),

      column_dtypes=(tf.int64, tf.float32, tf.string),

      skip_header_lines=1)

train_input_fn = tf.contrib.timeseries.RandomWindowInputFn(reader, batch_size=4, window_size=64)
estimator.train(input_fn=train_input_fn, steps=train_steps)
evaluation_input_fn = tf.contrib.timeseries.WholeDatasetInputFn(reader)
evaluation = estimator.evaluate(input_fn=evaluation_input_fn, steps=1)

export_directory = tempfile.mkdtemp()

###################################################### 
# the exogenous column must be provided to the build_raw_serving_input_receiver_fn. 
# But How ?
######################################################

input_receiver_fn = estimator.build_raw_serving_input_receiver_fn()
# -> error missing 'is_changepoint' key    

#input_receiver_fn = estimator.build_raw_serving_input_receiver_fn({'is_changepoint' : string_feature}) 
# -> cast exception

export_location = estimator.export_savedmodel(export_directory, input_receiver_fn)

根据documentation,build_raw_serving_input_receiver_fn exogenous_features 参数:将特征键映射到外生特征(Numpy 数组或张量)的字典。用于确定这些特征的占位符的形状。

那么将 one_hot_column 或 sparse_column_with_keys 转换为 Tensor 对象的最佳方法是什么?

【问题讨论】:

    标签: python tensorflow export time-series


    【解决方案1】:

    由于外生特征可以是 Numpy 数组或张量,我将这些特征添加为 NumpyArrays,如下所示:

    input_receiver_fn = estimator.build_raw_serving_input_receiver_fn(
          exogenous_features={"is_changepoint": [["yes", "no"]]})
    

    至少模型被保存了,但我仍然不确定这是制作它的正确和最佳方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-11-15
      • 2020-09-13
      • 2018-11-11
      • 2019-01-15
      • 2016-05-11
      • 2019-10-27
      • 1970-01-01
      • 2018-12-09
      相关资源
      最近更新 更多