Python DNN分类器模型到TensorFlow服务模型

Python DNN分类器模型到TensorFlow服务模型,python,tensorflow,google-cloud-platform,tensorflow-serving,Python,Tensorflow,Google Cloud Platform,Tensorflow Serving,我是ML和TF的新手,我正在尝试使用TensorFlow服务在GCP上托管原始TensorFlow模型。为此,我需要将DNNClassifier模型转换为TensorFlow服务模型。根据指南,我需要使用 SavedModelBuilder方法,但我不知道如何在使用的情况下定义输入/输出 有人能为这个案例发布一个示例代码吗 完整代码: (train_x, train_y), (test_x, test_y) = iris_data.load_data() # Feature columns d

我是ML和TF的新手,我正在尝试使用TensorFlow服务在GCP上托管原始TensorFlow模型。为此,我需要将
DNNClassifier
模型转换为TensorFlow服务模型。根据指南,我需要使用
SavedModelBuilder
方法,但我不知道如何在使用的情况下定义输入/输出

有人能为这个案例发布一个示例代码吗

完整代码:

(train_x, train_y), (test_x, test_y) = iris_data.load_data()

# Feature columns describe how to use the input.
my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

# Build 2 hidden layer DNN with 10, 10 units respectively.
classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns,
    # Two hidden layers of 10 nodes each.
    hidden_units=[10, 10],
    # The model must choose between 3 classes.
    n_classes=3)

# Train the Model.
classifier.train(
    input_fn=lambda:iris_data.train_input_fn(train_x, train_y,
                                             args.batch_size),
    steps=args.train_steps)

# Evaluate the model.
eval_result = classifier.evaluate(
    input_fn=lambda:iris_data.eval_input_fn(test_x, test_y,
                                            args.batch_size))

print('\nTest set accuracy: {accuracy:0.3f}\n'.format(**eval_result))

# Generate predictions from the model
expected = ['Setosa', 'Versicolor', 'Virginica']
predict_x = {
    'SepalLength': [5.1, 5.9, 6.9],
    'SepalWidth': [3.3, 3.0, 3.1],
    'PetalLength': [1.7, 4.2, 5.4],
    'PetalWidth': [0.5, 1.5, 2.1],
}

predictions = classifier.predict(
    input_fn=lambda:iris_data.eval_input_fn(predict_x,
                                            labels=None,
                                            batch_size=args.batch_size))

for pred_dict, expec in zip(predictions, expected):
    template = ('\nPrediction is "{}" ({:.1f}%), expected "{}"')

    class_id = pred_dict['class_ids'][0]
    probability = pred_dict['probabilities'][class_id]

    print(template.format(iris_data.SPECIES[class_id],
                          100 * probability, expec))

只需将arythmic模式更改为张量样式,可能需要交叉合并样式,然后使用格式均衡器进行调整。

在训练和评估模型之后,您就可以保存模型了

(train_x, train_y), (test_x, test_y) = iris_data.load_data()

# Feature columns describe how to use the input.
my_feature_columns = []
for key in train_x.keys():
    my_feature_columns.append(tf.feature_column.numeric_column(key=key))

# Build 2 hidden layer DNN with 10, 10 units respectively.
classifier = tf.estimator.DNNClassifier(
    feature_columns=my_feature_columns,
    # Two hidden layers of 10 nodes each.
    hidden_units=[10, 10],
    # The model must choose between 3 classes.
    n_classes=3)

# Train the Model.
classifier.train(
    input_fn=lambda:iris_data.train_input_fn(train_x, train_y,
                                             args.batch_size),
    steps=args.train_steps)

# Evaluate the model.
eval_result = classifier.evaluate(
    input_fn=lambda:iris_data.eval_input_fn(test_x, test_y,
                                            args.batch_size))

export_path = 'Your Desired new Path '
builder = tf.saved_model.builder.SavedModelBuilder(export_path)
sess = tf.InteractiveSession()
builder.add_meta_graph_and_variables(
  sess, [tf.saved_model.tag_constants.SERVING]
builder.save()
根据应用程序的不同,您还可以将
签名\u def\u映射
添加到生成器中。添加\u meta\u graph\u和\u variables()函数


请注意,对于分类器,输入是feature_列,输出是三个类之一。对于生成器,输入是“tf session
tag\u constants.service
signature\u def\u map”,输出是“Desired\u Directory/saved\u model.pb”

谢谢您的回复,您有任何与示例相关的链接吗?谢谢您的回答,但在这种情况下,我无法为该模型定义输入/输出。我的意思是,在导出
分类器
生成器
的输入/输出之前,我无法理解如何调用此模型?对于分类器,输入是特征列,输出是三个类之一。对于生成器,输入为“tf session”,
标记常量。提供
签名定义映射
,输出为“所需目录/saved_model.pb”