如何以SavedModel格式导出TensorFlow模型

时间:2018-09-09 05:21:26

标签: tensorflow tensorflow-serving

我创建了以下模型 我需要以SavedModel格式导出。 这是我的代码。有人可以让我知道如何导出吗? 我了解我需要将saved_model_builder连同signature_constants和signature_def_utils一起使用。我不确定如何在此示例中做到这一点。

训练数据集

size_data = numpy.asarray([2104, 1600, 2400, 1416, 3000, 1985, 1534, 1427,
                           1664, 2238, 2567, 1200, 852, 1852, 1203])
price_data = numpy.asarray([399900, 329900, 369000, 232000, 539900, 299900, 
                           329900, 314000, 299000, 179900, 299900, 239500])

测试数据集

size_data_test = numpy.asarray([1600, 1494, 1236, 1100, 3137, 2238])
price_data_test = numpy.asarray([329900, 242500, 199900, 249900, 579900, 
329900])

标准化数据集

def normalize(array):
    return (array - array.mean()) / array.std()

size_data_n = normalize(size_data)
price_data_n = normalize(price_data)

size_data_test_n = normalize(size_data_test)
price_data_test_n = normalize(price_data_test)

显示情节

plt.plot(size_data, price_data, 'ro', label='Samples data')
plt.legend()
plt.draw()

samples_number = price_data_n.size

TF图形输入

X = tf.placeholder("float")
Y = tf.placeholder("float")

创建模型

# Set model weights
W = tf.Variable(numpy.random.randn(), name="weight")
b = tf.Variable(numpy.random.randn(), name="bias")

设置参数

learning_rate = 0.1
training_iteration = 200

构建线性模型

model = tf.add(tf.multiply(X, W), b)

# Minimize squared errors
cost_function = tf.reduce_sum(tf.pow(model - Y, 2)) / (2 * samples_number)  
# L2 loss
optimizer = 
tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)  
#Gradient descent

# Initialize variables
init = tf.initialize_all_variables()

启动图表

with tf.Session() as sess:
    sess.run(init)

    display_step = 20
    # Fit all training data
    for iteration in range(training_iteration):
        for (x, y) in zip(size_data_n, price_data_n):
            sess.run(optimizer, feed_dict={X: x, Y: y})

        # Display logs per iteration step
        if iteration % display_step == 0:
            print("Iteration:", '%04d' % (iteration + 1), "cost=", " 
  {:.9f}".format(sess.run(cost_function, feed_dict={X: size_data_n, Y: 
  price_data_n})), "W=", sess.run(W), "b=", sess.run(b))

    tuning_cost = sess.run(cost_function, feed_dict={X: 
    normalize(size_data_n), Y: normalize(price_data_n)})

    print("Tuning completed:", "cost=", "{:.9f}".format(tuning_cost), "W=", 
    sess.run(W), "b=", sess.run(b))

验证调整模型

    testing_cost = sess.run(cost_function, feed_dict={X: size_data_test_n, 
    Y: price_data_test_n})

    print("Testing data cost:", testing_cost)
    # Display a plot
    plt.figure()
    plt.plot(size_data_n, price_data_n, 'ro', label='Normalized samples')
    plt.plot(size_data_test_n, price_data_test_n, 'go', label='Normalized 
    enter code heretesting samples')
    plt.plot(size_data_n, sess.run(W) * size_data_n + sess.run(b), 
    label='Fitted line')
    plt.legend()
    plt.show()

1 个答案:

答案 0 :(得分:0)

您可以使用tf.saved_model.simple_save()函数:

tf.saved_model.simple_save(sess, './',
                           inputs={'X': X},
                           outputs={'Y': Y})