tensorflow 1.13, keras + placeholder + saved_model的混用【主要是验证一下】

共分为两步:

1、训练模型并保存

2、加载模型进行预测

 

1、训练模型并保存

代码如下:


import tensorflow as tf  # tf.version==1.13.1
import numpy as np


x = tf.placeholder(tf.int32, shape=(None, 100), name="x")
y = tf.placeholder(tf.int32, shape=(None, ))

# embeddings = tf.keras.layers.Embedding(3000, 100)
embeddings = tf.Variable(tf.truncated_normal(shape=(3000, 100), stddev=0.02))
x_embedding = tf.nn.embedding_lookup(embeddings, x)

lstm = tf.keras.layers.LSTM(128)
output = lstm(x_embedding)

logits = tf.layers.dense(output, 2)
probs = tf.nn.softmax(logits)

loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=y))
optimizer = tf.train.AdamOptimizer()
train_op = optimizer.minimize(loss)

train_x = np.random.randint(0, 1000, size=(100, 100))
train_y = np.random.randint(0, 2, size=(100,))

test_x = [[0, 1, 2, 4] * 25]
print(test_x)

def save_model(sess:tf.Session, inputs, outputs):

    import shutil
    import os
    path = "model_dir/1"

    if os.path.exists(path):
        shutil.rmtree(path)

    builder = tf.saved_model.builder.SavedModelBuilder(path)

    input_tensor = tf.saved_model.build_tensor_info(inputs)
    output_tensor = tf.saved_model.build_tensor_info(outputs)

    predict_signature_def = tf.saved_model.build_signature_def(inputs={"inputs": input_tensor}, outputs={"outputs": output_tensor},method_name=tf.saved_model.signature_constants.PREDICT_METHOD_NAME)
    # builder.add_meta_graph_and_variables
    builder.add_meta_graph_and_variables(sess, [tf.saved_model.tag_constants.SERVING], signature_def_map={tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY: predict_signature_def})
    builder.save()


with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    batch_size = 10
    for epoch_index in range(30):
        for i in range(len(train_y) // batch_size):
            batch_x, batch_y = train_x[i * batch_size: (i + 1) * batch_size], train_y[
                                                                              i * batch_size: (i + 1) * batch_size]
            _, train_loss = sess.run([train_op, loss], {x: batch_x, y: batch_y})
            print("epoch: {}, batch: {}, loss: {}:".format(epoch_index, i, train_loss))
    print(sess.run(probs, feed_dict={x: test_x}))  # 我本地预测的结果为:[[0.00125402 0.9987459 ]],可以在加载模型后再次预测进行验证
    save_model(sess, x, probs)

2、加载模型进行预测
 


import tensorflow as tf


def load_model():
    path = "model_dir/1"
    sess = tf.Session()

    meta_graph = tf.saved_model.load(sess, tags=[tf.saved_model.tag_constants.SERVING], export_dir=path)
    signature_def = meta_graph.signature_def
    input_x = signature_def[tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY].inputs["inputs"].name
    input_y = signature_def[tf.saved_model.signature_constants.DEFAULT_SERVING_SIGNATURE_DEF_KEY].outputs["outputs"].name

    return sess, input_x, input_y


sess, input_x, input_y = load_model()

test_x = [[0, 1, 2, 4] * 25]

print(sess.run(input_y, {input_x: test_x}))

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值