我是ML和TF的新手,我正在尝试使用TensorFlow服务在GCP上托管原始的TensorFlow模型。为此,我需要将DNNClassifier
模型转换为TensorFlow服务模型。根据{{3}}指南,我需要使用
SavedModelBuilder
方法,但在Get Started的情况下,我无法弄清楚如何定义输入/输出。
是否有人可以为此案例发布示例代码?
完整代码:
(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))
答案 0 :(得分:1)
在训练和评估模型后,您就可以保存模型。
(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()
根据您的应用程序,您还可以将signature_def_map
添加到builder.add_meta_graph_and_variables()函数。
请注意,对于分类器,输入是feature_columns,输出是三个类之一。对于构建器,输入是会话,
tag_constants.SERVING and
signature_def_map`,输出是' Desired_Directory / saved_model.pb'
答案 1 :(得分:0)
只需将arythmic图案更改为张量样式可能必须交叉合并样式,然后使用格式均衡器进行调整。