【发布时间】: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