tensorflow模型参数与结构的加载-----二

本篇将接着上篇的保存的模型,将模型进行加载,代码如下所示:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

#载入数据集
mnist = input_data.read_data_sets("MNIST_data",one_hot=True)

#定义一个placeholder
y = tf.placeholder(tf.float32,[None,10])

#载入模型
with tf.gfile.FastGFile('./models/tfmodel.pb','rb') as f:
    graph_def = tf.GraphDef()
    graph_def.ParseFromString(f.read())
    tf.import_graph_def(graph_def,name = '')
    
with tf.Session() as sess:
    output = sess.graph.get_tensor_by_name('output:0')
    #结果存放在一个布尔型列表中
    correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(output,1))#argmax返回一维张量中最大的值所在的位置
    #求准确率
    accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
    print(sess.run(accuracy,feed_dict={'x-input:0':mnist.test.images,y:mnist.test.labels}))
    

显示结果如下所示:


总结:用tensorflow提供的convert_variable_to_constants函数在保存模型与加载模型上要优于tf.train.Saver类的方法,至此,tensorflow模型的保存与加载介绍到这里了。







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值