tensorflow 模型保存重载(Rnn)

关于保存模型和加载模型主要涉及到

.ckpt.meta

.ckpt.index

.ckpt.data

checkpoint

以及

.pb

两类

首先第一类中的.ckpt.meta就是保存了我们定义的结构网络图,里面没有包括一些variables的值,说白了也就是在tensorboard看到的一张结构图,.ckpt.data中包括的就是神经网络图中定义训练后的各种权值,所以可以概括如下:

一 保存模型:

saver.save(sess,'net/my_net')

二 加载模型:

1加载图

saver = tf.train.import_meta_graph(path_to_ckpt_meta)

2加载权值

saver.restore(sess, path_to_ckpt_data)

这就完成了,关于.pb其实就是(meta+data),本文主要介绍第一种方式,关于.pb file的介绍请看:

https://blog.csdn.net/weixin_42001089/article/details/81055745

博主首先训练一个简单模型(RNN)

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

mnist=input_data.read_data_sets('D:/tensorflow/MNIST_data/mnist',one_hot=True)

batch_size=100
n_batch=mnist.train.num_examples//batch_size
max_time=28
n_inputs=28
lstm_size=100
n_class=10

def Weights_variables():
    initial=tf.truncated_normal([lstm_size,n_class],stddev=0.1)
    return tf.Variable(initial)

def biases_variables():
    initial=tf.constant(0.1,shape=[n_class])
    return tf.Variable(initial)

with tf.name_scope('RNN'):
    def RNN(X):    
        inputs=tf.reshape(X,[-1,max_time,n_inputs])
        lstm_cell=tf.nn.rnn_cell.BasicLSTMCell(lstm_size)
        output,final_output=tf.nn.dynamic_rnn(lstm_cell,inputs,dtype=tf.float32)
        return final_output[1]

with tf.name_scope('input'):
    x=tf.placeholder(tf.float32,[None,784],name="x")
    y=tf.placeholder(tf.float32,[None,10],name="y")


hiddlen=RNN(x)
with tf.name_scope('W'):
    w=Weights_variables()
with tf.name_scope('b'):
    b=biases_variables()
with tf.name_scope('prediction'):
    prediction=tf.nn.softmax(tf.matmul(hiddlen,w)+b)
    
with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train_step'):
    train_step=tf.train.AdamOptimizer(0.0001).minimize(loss)

with tf.name_scope('Accuracy'): 
    prediction_value=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    Accuracy=tf.reduce_mean(tf.cast(prediction_value,dtype=tf.float32),name="Accuracy")
    tf.summary.scalar('Accuracy',Accuracy)


init=tf.global_variables_initializer()
merged=tf.summary.merge_all()


saver=tf.train.Saver()


with tf.Session() as sess:
    sess.run(init)
    writer_train=tf.summary.FileWriter('logs/train',sess.graph)
    writer_test=tf.summary.FileWriter('logs/test',sess.graph)
    for batch in range(n_batch-50):
        batch_xs,batch_ys=mnist.train.next_batch(batch_size)
        sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_train.add_summary(summary,batch)
        
        batch_xs,batch_ys=mnist.test.next_batch(batch_size)
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_test.add_summary(summary,batch)
        
    saver.save(sess,'net/my_net')
  x=tf.placeholder(tf.float32,[None,784],name="x")
    y=tf.placeholder(tf.float32,[None,10],name="y")


hiddlen=RNN(x)
with tf.name_scope('W'):
    w=Weights_variables()
with tf.name_scope('b'):
    b=biases_variables()
with tf.name_scope('prediction'):
    prediction=tf.nn.softmax(tf.matmul(hiddlen,w)+b)
    
with tf.name_scope('loss'):
    loss=tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
    tf.summary.scalar('loss',loss)
with tf.name_scope('train_step'):
    train_step=tf.train.AdamOptimizer(0.0001).minimize(loss)

with tf.name_scope('Accuracy'): 
    prediction_value=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
    Accuracy=tf.reduce_mean(tf.cast(prediction_value,dtype=tf.float32),name="Accuracy")
    tf.summary.scalar('Accuracy',Accuracy)


init=tf.global_variables_initializer()
merged=tf.summary.merge_all()


saver=tf.train.Saver()


with tf.Session() as sess:
    sess.run(init)
    writer_train=tf.summary.FileWriter('logs/train',sess.graph)
    writer_test=tf.summary.FileWriter('logs/test',sess.graph)
    for batch in range(n_batch-50):
        batch_xs,batch_ys=mnist.train.next_batch(batch_size)
        sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_train.add_summary(summary,batch)
        
        batch_xs,batch_ys=mnist.test.next_batch(batch_size)
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_test.add_summary(summary,batch)
        
    saver.save(sess,'net/my_net')

其中红色部分就是保存模型的关键语句

天蓝色语句中

name="XXXX"

对于我们加载有重要作用,保存后的模型就在net文件夹下:

tensorflow 模型保存重载(Rnn)_第1张图片

其中.meta就是我们训练的神经网络图,.data就是训练好的的权值,checkpoint就是最后一次的结果

接着我们加载我们的模型

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

mnist=input_data.read_data_sets('D:/tensorflow/MNIST_data/mnist',one_hot=True)

batch_size=100
n_batch=mnist.test.num_examples//batch_size

with tf.Session() as sess:
    my_saver = tf.train.import_meta_graph('net/my_net.meta')
    my_saver.restore(sess,tf.train.latest_checkpoint('net/'))
    graph = tf.get_default_graph()
    writer_test=tf.summary.FileWriter('logs/load',sess.graph)
    x = graph.get_tensor_by_name("input/x:0")              
    y = graph.get_tensor_by_name("input/y:0")
    Accuracy = graph.get_tensor_by_name("Accuracy/Accuracy:0")
    tf.summary.scalar('Accuracy',Accuracy)

    merged=tf.summary.merge_all()    
    for batch in range(n_batch):      
        batch_xs,batch_ys=mnist.test.next_batch(batch_size)
        print(sess.run(Accuracy,feed_dict={x:batch_xs,y:batch_ys}))
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_test.add_summary(summary,batch)
                
        
   
  my_saver = tf.train.import_meta_graph('net/my_net.meta')
    my_saver.restore(sess,tf.train.latest_checkpoint('net/'))
    graph = tf.get_default_graph()
    writer_test=tf.summary.FileWriter('logs/load',sess.graph)
    x = graph.get_tensor_by_name("input/x:0")              
    y = graph.get_tensor_by_name("input/y:0")
    Accuracy = graph.get_tensor_by_name("Accuracy/Accuracy:0")
    tf.summary.scalar('Accuracy',Accuracy)

    merged=tf.summary.merge_all()    
    for batch in range(n_batch):      
        batch_xs,batch_ys=mnist.test.next_batch(batch_size)
        print(sess.run(Accuracy,feed_dict={x:batch_xs,y:batch_ys}))
        summary=sess.run(merged,feed_dict={x:batch_xs,y:batch_ys})
        writer_test.add_summary(summary,batch)
                
        
   

其中最主要的就是红色,

my_saver = tf.train.import_meta_graph('net/my_net.meta')

加载了神经网络图

 

my_saver.restore(sess,tf.train.latest_checkpoint('net/'))

加载了训练好后的权值

注意:

1 因为我们原demo中使用了tensorboard的命名空间,其中x,y都有一个共同的命名空间所以,在重载x时前面要加上您自己命名的命名空间,否则会报x不存在的错误

 x = graph.get_tensor_by_name("input/x:0")

2 这里之所以能通过x索引前面原demo中tensor的name,关键就是之前天蓝色部分的作用

重载的神经网络后的网络图和原demo一样:

tensorflow 模型保存重载(Rnn)_第2张图片

tensorflow 模型保存重载(Rnn)_第3张图片

可以看到准确率都是在0.60左右,因为我们的原demo训练小,也没进行各种优化,所以训练处的模型不是很好,但是我们重在说明的是tensorflow保存重载机制

 

你可能感兴趣的:(神经网络)