我正在将使用估算器构建的当前Tensorflow 1.x模型迁移到Tensorflow 2.0 Keras。在涉及序列化服务模型之前,迁移一直比较顺利。
模型指定如下
model = tf.keras.Sequential()
model.add(tf.keras.layers.DenseFeatures(feature_columns))
for units in hidden_layers:
model.add(tf.keras.layers.Dense(units,activation='relu'))
model.add(tf.keras.layers.Dense(2,activation=None))
我正在使用Tensorflow特征列api,该API期望将特征列的字典作为输入,并在这些特征传递到模型之前对其应用转换。
例如训练时
def dataset_transformation_function(feature_dict: Dict[str,tf.Tensor]):
output_dict = feature_dict.copy()
output_dict['logx1'] = tf.math.log(feature_dict['x1'])
return output_dict
train_dataset = (
tf.data.Dataset.from_tensor_slices(
(train_feature_dict,train_label_vector)
)
.shuffle(n_train)
.batch(batch_size)
.map(dataset_transformation_function)
.repeat()
.prefetch(tf.data.experimental.AUTOTUNE)
)
看来在服务时间我需要执行相同的转换:
input_tensors = [tf.Tensorspec(name=...),...]
@tf.function(input_signature=input_tensors)
def dataset_transformation_function(args) -> Dict[str,tf.Tensor]:
...
和
tf.saved_model.save(
model,MODEL_DIR,signatures=feature_transform,)
但是我无法确定输入张量或函数的正确签名。
我要从中迁移的方法是:
def serving_input_fn():
receiver_tensors = {
'x1': tf.placeholder(dtype=tf.float32,shape=[None,],name='x1')
'x2': tf.placeholder(dtype=tf.string,name='x2')
}
features = dataset_transformation_function(
receiver_tensors
)
return tf.estimator.export.ServingInputReceiver(features,receiver_tensors)
estimator.export_savedmodel(
MODEL_DIR,serving_input_fn,as_text=False,checkpoint_path=estimator.best_checkpoint,)