TensorFlow学习笔记(六) tensorflow 中的保存训练的参数以及载入参数检测test集
2018-04-01 10:06
375 查看
如何保存训练好的参数以前面练习的一个小例子,来保存训练好的参数:
请参考点击打开链接
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow import float32
#载入数据,会自动通过一个脚本下载好数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
#每个批次大小以及多少批次
batch_size = 100
n_batch = mnist.train.num_examples // batch_size
#设置两个占位符
x = tf.placeholder(dtype=tf.float32, shape=[None, 784])
y = tf.placeholder(dtype=tf.float32, shape=[None, 10])
#创建一个简单的神经网络
W = tf.Variable(tf.zeros([784, 10]),float32)
b = tf.Variable(tf.zeros([10]),float32)
prediction = tf.nn.softmax(tf.matmul(x, W)+b)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降方法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
#结果放在布尔型列表中,其中argmax返回数列中最大值所在的位置
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction, 1))
#求准确性
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()#为下面保存做准备
with tf.Session() as sess:
sess.run(init)
for epoch in range (21):
for batch 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,'net/my_net.ckpt')你将会在你创建的/net文件夹中看到保存的数据文件
请参考点击打开链接
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow import float32
#载入数据,会自动通过一个脚本下载好数据集
mnist = input_data.read_data_sets("MNIST_data", one_hot=True)
#每个批次大小以及多少批次
batch_size = 100
n_batch = mnist.train.num_examples // batch_size
#设置两个占位符
x = tf.placeholder(dtype=tf.float32, shape=[None, 784])
y = tf.placeholder(dtype=tf.float32, shape=[None, 10])
#创建一个简单的神经网络
W = tf.Variable(tf.zeros([784, 10]),float32)
b = tf.Variable(tf.zeros([10]),float32)
prediction = tf.nn.softmax(tf.matmul(x, W)+b)
#二次代价函数
loss = tf.reduce_mean(tf.square(y-prediction))
#使用梯度下降方法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)
#初始化变量
init = tf.global_variables_initializer()
#结果放在布尔型列表中,其中argmax返回数列中最大值所在的位置
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction, 1))
#求准确性
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
saver = tf.train.Saver()#为下面保存做准备
with tf.Session() as sess:
sess.run(init)
for epoch in range (21):
for batch 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,'net/my_net.ckpt')你将会在你创建的/net文件夹中看到保存的数据文件
使用载入保存好的参数检验准确性(和没训练过的参数比较)
import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from tensorflow import float32 #载入数据,会自动通过一个脚本下载好数据集 mnist = input_data.read_data_sets("MNIST_data", one_hot=True) #每个批次大小以及多少批次 batch_size = 100 n_batch = mnist.train.num_examples // batch_size #设置两个占位符 x = tf.placeholder(dtype=tf.float32, shape=[None, 784]) y = tf.placeholder(dtype=tf.float32, shape=[None, 10]) #创建一个简单的神经网络 W = tf.Variable(tf.zeros([784, 10]),float32) b = tf.Variable(tf.zeros([10]),float32) prediction = tf.nn.softmax(tf.matmul(x, W)+b) #二次代价函数 loss = tf.reduce_mean(tf.square(y-prediction)) #使用梯度下降方法 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) #初始化变量 init = tf.global_variables_initializer() #结果放在布尔型列表中,其中argmax返回数列中最大值所在的位置 correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(prediction, 1)) #求准确性 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) 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, 'net/my_net.ckpt') print(sess.run(accuracy,feed_dict={x:mnist.test.images, y:mnist.test.labels}))其中没训练的参数的检测准确率很低, 而通过导入的参数检测的却很高
相关文章推荐
- tensorflow: 保存和加载模型, 参数;以及使用预训练参数方法
- tensorflow保存网络参数 使用训练好的网络参数进行数据的预测
- TensorFlow 训练好模型参数的保存和恢复代码
- TensorFlow训练的模型参数(W和B)利用np.savez()保存和用cnpy库C++读取
- Tensorflow使用笔记(1) 怎么保存和使用训练好的参数和模型
- tensorflow学习(4):保存模型Saver.save()的参数命名机制以及restore并创建手写字体识别引擎
- 4000 TensorFlow学习笔记(2)——保存和加载训练模型参数
- tensorflow1.1/保存和提取训练好的参数
- Tensorflow学习教程------模型参数和网络结构保存且载入,输入一张手写数字图片判断是几
- Tensorflow学习笔记(1)---TensorFlow安装以及调试
- tensorflow:保存与读取网络结构,参数等
- TensorFlow模型保存/载入的两种方法
- tensorflow表情识别:训练、参数固化、应用预测
- Tensorflow学习笔记:模型训练数据的保存和恢复的简单实例
- tensorflow笔记:模型的保存与训练过程可视化
- TensorFlow保存和载入模型
- TensorFlow 深度学习框架(7)-- 变量管理及训练模型的保存与加载
- (二) Ubuntu安装新版tensorflow以及目标检测API
- Tensorflow的安装以及当前Ubuntu的版本检测
- tensorflow2caffe(1) : 如何将tensorflow框架下训练得到的权重转化为caffe框架下的权重参数