Tensorflow:如何在应用程序中使用经过训练的模型? [英] Tensorflow: How to use a trained model in a application?

查看:125
本文介绍了Tensorflow:如何在应用程序中使用经过训练的模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经训练了一个 Tensorflow 模型,现在我想导出函数"以在我的 python 程序中使用它.这是可能的,如果是,如何?任何帮助都会很好,在文档中找不到太多内容.(我不想保存会话!)

I have trained a Tensorflow Model, and now I want to export the "function" to use it in my python program. Is that possible, and if yes, how? Any help would be nice, could not find much in the documentation. (I dont want to save a session!)

我现在已经按照您的建议存储了会话.我现在像这样加载它:

I have now stored the session as you suggested. I am loading it now like this:

f = open('batches/batch_9.pkl', 'rb')
input = pickle.load(f)
f.close()
sess = tf.Session()

saver = tf.train.Saver()
saver.restore(sess, 'trained_network.ckpt')
y_pred = []

sess.run(y_pred, feed_dict={x: input})

print(y_pred)

但是,当我尝试初始化保护程序时,出现错误没有要保存的变量".

However, I get the error "no Variables to save" when I try to initialize the saver.

我想要做的是:我正在为棋盘游戏编写机器人,输入是棋盘上的情况格式化为张量.现在我想返回一个张量,它为我提供了接下来播放的最佳位置,即一个处处为 0,一个位置处为 1 的张量.

What I want to do is this: I am writing a bot for a board game, and the input is the situation on the board formatted into a tensor. Now I want to return a tensor which gives me the best position to play next, i.e. a tensor with 0 everywhere and a 1 at one position.

推荐答案

我不知道是否还有其他方法可以做到,但是您可以通过保存会话在另一个 Python 程序中使用您的模型:

I don't know if there is any other way to do it, but you can use your model in another Python program by saving your session:

您的训练代码:

# build your model

sess = tf.Session()
# train your model
saver = tf.train.Saver()
saver.save(sess, 'model/model.ckpt')

在您的应用程序中:

# build your model (same as training)
sess = tf.Session()
saver = tf.train.Saver()
saver.restore(sess, 'model/model.ckpt')

然后,您可以使用 feed_dict 评估模型中的任何张量.这显然取决于您的型号.例如:

You can then evaluate any tensor in your model using a feed_dict. This obviously depends on your model. For example:

#evaluate tensor
sess.run(y_pred, feed_dict={x: input_data})

这篇关于Tensorflow:如何在应用程序中使用经过训练的模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆