您的位置:首页 > 其它

6.TensorFlow模型的保存和读取

2017-10-03 14:26 573 查看

1.简单保存和读取操作

首先在当前路径下新建一个save文件夹,因为模型路径为save/model.ckpt。

import tensorflow as tf

v1 = tf.Variable(tf.random_normal([1,2]), name="v1")
v2 = tf.Variable(tf.random_normal([2,3]), name="v2")
init_op = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init_op)
print ("V1:",sess.run(v1))
print ("V2:",sess.run(v2))
saver_path = saver.save(sess, "save/model.ckpt")
print ("Model saved in file: ", saver_path)


打印信息为:

('V1:', array([[ 1.49957299, -1.31671512]], dtype=float32))
('V2:', array([[-0.33024931, -0.06910656, -1.33667743],
[ 0.78832048,  0.28635943,  1.57955635]], dtype=float32))
('Model saved in file: ', 'save/model.ckpt')


后面是读取模型代码:

import tensorflow as tf
v1 = tf.Variable(tf.random_normal([1,2]), name="v1")
v2 = tf.Variable(tf.random_normal([2,3]), name="v2")
saver = tf.train.Saver()

with tf.Session() as sess:
saver.restore(sess, "save/model.ckpt") #读取模型
print ("V1:",sess.run(v1))
print ("V2:",sess.run(v2))
print ("Model restored")


打印信息为:

('V1:', array([[ 1.49957299, -1.31671512]], dtype=float32))
(
4000
'V2:', array([[-0.33024931, -0.06910656, -1.33667743],
[ 0.78832048,  0.28635943,  1.57955635]], dtype=float32))
Model restored


可以看到读取到的V1和V2是之前保存的值。

2.CNN模型的保存和读取

首先在当前路径下新建文件夹nets,训练的模型会保存在这个路径中。

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
sess = tf.InteractiveSession()

def weight_variable(shape):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)

def bias_variable(shape):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)

def conv2d(x, W):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(x):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],
strides=[1, 2, 2, 1], padding='SAME')

x = tf.placeholder(tf.float32, [None, 784])
y_ = tf.placeholder(tf.float32, [None, 10])
x_image = tf.reshape(x, [-1,28,28,1])

W_conv1 = weight_variable([5, 5, 1, 32])
b_conv1 = bias_variable([32])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([5, 5, 32, 64])
b_conv2 = bias_variable([64])
h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)
h_pool2 = max_pool_2x2(h_conv2)

W_fc1 = weight_variable([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

keep_prob = tf.placeholder(tf.float32)
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

W_fc2 = weight_variable([1024, 10])
b_fc2 = bias_variable([10])
y_conv=tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2)

cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y_conv), reduction_indices=[1]))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

# SAVER
save_step = 500 #每500次保存一下模型
saver = tf.train.Saver(max_to_keep=3) #保存最新的3个模型
do_train = 1    #训练测试选择

tf.initialize_all_variables().run()  #tf.global_variables_initializer

if do_train == 1:   #进行训练
for i in range(20000):
batch = mnist.train.next_batch(50)
if i%100 == 0:
train_accuracy = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print("step %d, training accuracy %g"%(i, train_accuracy))
train_step.run(feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})
#save net
if i % save_step == 0:
saver.save(sess,"save/nets/cnn_mnist.ckpt-" + str(i))
print("Train finished")

if do_train == 0:   #进行测试
epoch = 3500
saver.restore(sess,"save/nets/cnn_mnist.ckpt-" + str(epoch))
#不用下面这种方式的原因好像是太耗内存,会出错,给4G运存都不够
'''
test_acc = sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0})
print (" TEST ACCURACY: %.3f" % (test_acc))
'''
#对每个batch做测试
for i in range(20):
batch = mnist.test.next_batch(50)
test_acc = accuracy.eval(feed_dict={x:batch[0], y_: batch[1], keep_prob: 1.0})
print (" TEST ACCURACY: %.3f" % (test_acc))


令do_train=1,模型会进行训练,每500次在nets文件夹中保存一个snap,最多保存3个。

令do_train=0,模型会从nets文件夹中读取特定的模型,然后进行测试,我们对batch进行测试,一次读入太多内存貌似吃不消。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: