tensorflow - tf.estimator 服务功能失败
问题描述
我正在使用 tf.estimator 来训练和服务我的 tensorflow 模型。培训按预期完成,但未能服务。我将我的数据作为 TFRecordDataset 读取。我的解析函数将转换应用于特征“x2”。“x2”是一个被拆分的字符串。转换后的特征是“x3”。
def parse_function(example_proto):
features={"x1":tf.FixedLenFeature((), tf.string), "x2":tf.FixedLenFeature((),
tf.string),
"label":tf.FixedLenFeature((), tf.int64)}
parsed_features = tf.parse_example(example_proto, features)
x3=tf.string_split(parsed_features["string"],',')
parsed_features["x3"]=x3
return parsed_features, parsed_features["label"]
我的服务功能是
def serving_input_fn():
receiver_tensor = {}
for feature_name in record_columns:
if feature_name in {"x1", "x2","x3"}:
dtype = tf.string
else:
dtype=tf.int32
receiver_tensor[feature_name] = tf.placeholder(dtype, shape=[None])
features = {
key: tf.expand_dims(tensor, -1)
for key, tensor in receiver_tensor.items()
}
return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)
过去,当我在解析函数中没有任何转换时,它总是有效,但现在它因错误而失败。
cloud.ml.prediction.prediction_utils.PredictionError: Failed to run the provided model: Exception during running the graph: Cannot feed value of shape (2, 1) for Tensor u'Placeholder_2:0', which has shape '(?,)' (Error code: 2)
我想我必须在我的服务功能中将转换应用于“x2”,但我不知道如何。任何帮助将不胜感激
解决方案
按照这个链接
创建receiver_tensor 后,我处理了特征“x3”。在服务函数中拆分字符串需要在拆分之前挤压张量
def serving_input_fn():
receiver_tensor = {}
receiver_tensor["x1"] = tf.placeholder(tf.string, shape=[None], name="x1")
receiver_tensor["label"] = tf.placeholder(tf.int32, shape=[None], name="x2")
receiver_tensor["x2"] = tf.placeholder(tf.string, shape=[None],
name="string")
features = {
key: tf.expand_dims(tensor, -1)
for key, tensor in receiver_tensor.items()
}
features["x3"]=tf.string_split(tf.squeeze(features["x2"]),',')
return tf.estimator.export.ServingInputReceiver(features, receiver_tensor)
推荐阅读
- python - 通过 Pandas DataReader 获取比特币数据信息
- python - 循环变量的协方差矩阵?
- python-3.x - 如何将方法更改为 .csv 文件
- java - 在媒体播放期间更新 RemoteView 的视图
- gcc - 外部变量未定义但编译器没有错误
- java - java IoC框架如何保证线程安全?
- php - 除 BACS 付款外,如何自动处理 WooCommerce 订单?
- java - 我无法在回调函数之外记录变量,该函数在 Android 中分配有其内部的值
- .net - 由于可用或不可用查询参数而导致不同的返回类型
- typescript - 返回类型不包括“未定义”但 switch 是详尽的