目录
8-1 将训练的模型参数保存
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#每个批次100张照片
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#定义权重和偏置值
W= tf.Variable(tf.zeros([784,10]))
b= tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W) + b)
#softmax代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#最小化损失
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
#结果存放在一个布尔型变量列表
accuracy_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#准确率预测
accuracy = tf.reduce_mean(tf.cast(accuracy_prediction,tf.float32))
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
for epoch in range(10):
for bacth in range(n_batch):
batch_xs,batch_ys = mnist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
acc = sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})
print('Iter' + str(epoch) + 'Testing accuracy' + str(acc))
saver.save(sess,'model_save/my_net.ckpt')#模型参数保存在model_save文件夹下的my_net.ckpt
Extracting MNIST_data\train-images-idx3-ubyte.gz Extracting MNIST_data\train-labels-idx1-ubyte.gz Extracting MNIST_data\t10k-images-idx3-ubyte.gz Extracting MNIST_data\t10k-labels-idx1-ubyte.gz Iter0Testing accuracy0.8998 Iter1Testing accuracy0.9114 Iter2Testing accuracy0.9168 Iter3Testing accuracy0.9205 Iter4Testing accuracy0.9226 Iter5Testing accuracy0.9242 Iter6Testing accuracy0.9246 Iter7Testing accuracy0.9291 Iter8Testing accuracy0.9281 Iter9Testing accuracy0.9278
8-2 加载数据模型
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
#每个批次100张照片
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size
#定义两个placeholder
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#定义权重和偏置值
W= tf.Variable(tf.zeros([784,10]))
b= tf.Variable(tf.zeros([10]))
prediction = tf.nn.softmax(tf.matmul(x,W) + b)
#softmax代价函数
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
#最小化损失
train_step = tf.train.AdamOptimizer(0.001).minimize(loss)
#结果存放在一个布尔型变量列表
accuracy_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#准确率预测
accuracy = tf.reduce_mean(tf.cast(accuracy_prediction,tf.float32))
init = tf.global_variables_initializer()
saver = tf.train.Saver()
with tf.Session() as sess:
sess.run(init)
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))#没有训练模型的准确率预测
saver.restore(sess,'model_save/my_net.ckpt')
print(sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels}))#加载模型后的准确率预测
Extracting MNIST_data\train-images-idx3-ubyte.gz Extracting MNIST_data\train-labels-idx1-ubyte.gz Extracting MNIST_data\t10k-images-idx3-ubyte.gz Extracting MNIST_data\t10k-labels-idx1-ubyte.gz 0.098 INFO:tensorflow:Restoring parameters from model_save/my_net.ckpt 0.9278
说明:1.关于tensorflow的代码是参考了b站练数成金的代码,链接地址:https://www.bilibili.com/video/av20542427/?p=1
2.部分代码还参考了tensorflow中文社区的网站,以及tensorflow的官网(需要梯子)。
tensorflow中文社区的网站:http://www.tensorfly.cn/
MNIST_data手写数据集下载:链接:https://pan.baidu.com/s/1_PxLxxZ4YP7KfDzZh8vPFA 密码:nyrs
更多Tensorflow资源下载,去github搜索Tensorflow下载更多demo