【问题标题】:How to extract the weights and bias from tensorflow neural network and how to compute the prediction on your own in python?如何从 tensorflow 神经网络中提取权重和偏差,以及如何在 python 中自己计算预测?
【发布时间】:2019-01-20 04:53:06
【问题描述】:

我在 python 中使用 tensorflow 来构建一个简单的回归神经网络,我想提取权重和偏差以便在另一个程序中使用它们。有人可以指出如何从 tf 神经网络中提取权重并根据相同的权重计算预测,一旦训练了 tensorflow NN,就可以使用简单的矩阵乘法和加法。这种计算也会提高预测时间(因为 tf.predict 做了一些额外的计算)?

【问题讨论】:

  • 这是我能找到的与您的查询相关的内容 - github.com/google/prettytensor/issues/6
  • 我的猜测是它不太可能改善预测时间,至少在本质上是这样。 Numpy 可能会让你到达那里,但可能不值得。不确定tf.predict 的内部工作原理,我只使用过 Keras。

标签: python tensorflow neural-network deep-learning


【解决方案1】:

您可以使用tf.train.Saver 类将您的模型保存为 ckpt 文件并在以后使用:

saver = tf.train.Saver()
 with tf.Session() as sess:
    #your code here
    save_path = saver.save(sess, "/model_path/model.ckpt")

要恢复变量:

saver.restore(sess, "/your_path/model.ckpt") 

完整文档:https://www.tensorflow.org/guide/saved_model

【讨论】:

  • 您能否详细说明如何使用保存的权重来计算预测,通过简单的矩阵乘法,而无需再次将它们加载到 tf Graph 中?
猜你喜欢
  • 2018-09-28
  • 2018-03-01
  • 2022-08-19
  • 1970-01-01
  • 1970-01-01
  • 2020-01-21
  • 2020-04-01
  • 1970-01-01
  • 2016-08-12
相关资源
最近更新 更多