博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
机器学习实战:TensorFlow模型的保存和读取
阅读量:2052 次
发布时间:2019-04-28

本文共 5759 字,大约阅读时间需要 19 分钟。

模型的保存:tf.train.Saver类中的save

在训练一个TensorFlow模型之后,可以将训练好的模型保存成文件,这样可以方便下一次对新的数据进行预测的时候直接加载训练好的模型即可获得结果,可以通过TensorFlow提供的tf.train.Saver函数,将一个模型保存成文件,一般习惯性的将TensorFlow的模型文件命名为*.ckpt文件。

模型的读取:tf.train.Saver类中的restore

import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_data#将参数固化到磁盘上mnist = input_data.read_data_sets('data/', one_hot=True)trainimg = mnist.train.imagestrainlabel=mnist.train.labelstestimg=mnist.test.images[0:1500]#针对可能出现的分配超过系统内存的问题testlabel=mnist.test.labels[0:1500]print("MNIST Ready")n_input = 784n_output = 10weights = {
'wc1': tf.Variable(tf.random_normal([3, 3, 1, 64], stddev=0.1)), 'wc2': tf.Variable(tf.random_normal([3, 3, 64, 128], stddev=0.1)), 'wd1': tf.Variable(tf.random_normal([7*7*128, 1024], stddev=0.1)), 'wd2': tf.Variable(tf.random_normal([1024, n_output], stddev=0.1))}biases = {
'bc1': tf.Variable(tf.random_normal([64], stddev=0.1)), 'bc2': tf.Variable(tf.random_normal([128], stddev=0.1)), 'bd1': tf.Variable(tf.random_normal([1024], stddev=0.1)), 'bd2': tf.Variable(tf.random_normal([n_output], stddev=0.1))}def conv_basic(_input, _w, _b, _keepratio): # 卷积神经网络的前向传播 # 对输入进行简单的预处理[n,h,w,c]-bitchsize大小,图像的高度、宽度,深度 _input_r = tf.reshape(_input, shape=[-1, 28, 28, 1]) # -1意思是让TensorFlow自己做一个推断,确定了其他所有维,可推断出第一维 ''' tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除去name参数用以指定该操作的name,与方法有关的一共五个参数: 第一个参数input:指需要做卷积的输入图像,它要求是一个Tensor,具有[batch, in_height, in_width, in_channels]这样的shape, 具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求类型为float32和float64其中之一 第二个参数filter:相当于CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape, 具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维 第三个参数strides:卷积时在图像每一维的步长,这是一个一维的向量,长度4 第四个参数padding:string类型的量,只能是"SAME","VALID"其中之一,这个值决定了不同的卷积方式 第五个参数:use_cudnn_on_gpu:bool类型,是否使用cudnn加速,默认为true ''' # "VALID" 仅舍弃最后列,或最后行的数据 # "SAME" 尝试在数据左右均匀的填充0,若填充个数为奇数时,则将多余的填充值放数据右侧. _conv1 = tf.nn.conv2d(_input_r, _w['wc1'], strides=[1, 1, 1, 1], padding='SAME') _conv1 = tf.nn.relu(tf.nn.bias_add(_conv1, _b['bc1'])) ''' tf.nn.max_pool(value, ksize, strides, padding, name=None) 参数是四个,和卷积很类似: 第一个参数value:需要池化的输入,一般池化层接在卷积层后面,所以输入通常是feature map,依然是[batch, height, width, channels]这样的shape 第二个参数ksize:池化窗口的大小,取一个四维向量,一般是[1, height, width, 1],因为我们不想在batch和channels上做池化,所以这两个维度设为了1 第三个参数strides:和卷积类似,窗口在每一个维度上滑动的步长,一般也是[1, stride, stride, 1] 第四个参数padding:和卷积类似,可以取'VALID'或者'SAME' 返回一个Tensor,类型不变,shape仍然是[batch, height, width, channels]这种形式 ''' _pool1 = tf.nn.max_pool(_conv1, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') _pool1_dr1 = tf.nn.dropout(_pool1, _keepratio) # _keepratio表示保留的比例 _conv2 = tf.nn.conv2d(_pool1_dr1, _w['wc2'], strides=[1, 1, 1, 1], padding='SAME') _conv2 = tf.nn.relu(tf.nn.bias_add(_conv2, _b['bc2'])) _pool2 = tf.nn.max_pool(_conv2, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME') _pool_dr2 = tf.nn.dropout(_pool2, _keepratio) # 全连接层——把输出转换为矩阵(向量)的形式 _densel = tf.reshape(_pool_dr2, [-1, _w['wd1'].get_shape().as_list()[0]]) _fc1 = tf.nn.relu(tf.add(tf.matmul(_densel, _w['wd1']), _b['bd1'])) _fc_dr1 = tf.nn.dropout(_fc1, _keepratio) ''' tf.nn.dropout(x, keep_prob, noise_shape=None, seed=None, name=None) 根据给出的keep_prob参数,将输入tensor x按比例输出。 使用说明:参数 keep_prob: 表示的是保留的比例,假设为0.8 则 20% 的数据变为0,然后其他的数据乘以 1/keep_prob;keep_prob 越大,保留的越多;参数 noise_shape:干扰形状。 此字段默认是None,表示第一个元素的操作都是独立,但是也不一定。比例:数据的形状是shape(x)=[k, l, m, n],而noise_shape=[k, 1, 1, n],则第1和4列是独立保留或删除,第2和3列是要么全部保留,要么全部删除。 ''' _out = tf.add(tf.matmul(_fc_dr1, _w['wd2']), _b['bd2']) out = {
'input_r': _input_r, 'conv1': _conv1, 'pool1': _pool1, 'pool1_dr1': _pool1_dr1, 'conv2': _conv2, 'pool2': _pool2, 'pool_dr2': _pool_dr2, 'densel': _densel, 'fc1': _fc1, 'fc_dr1': _fc_dr1, 'out': _out} return outprint("CNN READY")x = tf.placeholder(tf.float32, [None, n_input])y = tf.placeholder(tf.float32, [None, n_output])keepratio = tf.placeholder(tf.float32)_pred = conv_basic(x, weights, biases, keepratio)['out']cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=_pred, labels=y))optm = tf.train.AdamOptimizer(learning_rate=0.001).minimize(cost)#优化器的选择_corr = tf.equal(tf.argmax(_pred, 1), tf.argmax(y, 1))accr = tf.reduce_mean(tf.cast(_corr, tf.float32))init = tf.global_variables_initializer()save_step=1saver=tf.train.Saver(max_to_keep=3)print("FUNCTIONS READY")do_train=1sess = tf.Session()sess.run(init)training_epochs = 15batch_size = 16 # 示范,防止过慢display_step = 1#训练if do_train ==1: for epoch in range(training_epochs): avg_cost = 0 total_batch = 10 # 示范,防止过慢 for i in range(total_batch): batch_xs, batch_ys = mnist.train.next_batch(batch_size) sess.run(optm, feed_dict={
x: batch_xs, y: batch_ys, keepratio: 0.7}) avg_cost += sess.run(cost, feed_dict={
x: batch_xs, y: batch_ys, keepratio: 1.}) / total_batch if epoch % display_step == 0: print("Epoch: %03d/%03d cost: %.9f" % (epoch, training_epochs, avg_cost)) train_acc = sess.run(accr, feed_dict={
x: batch_xs, y: batch_ys, keepratio: 1.}) print("Training accuracy: %.3f" % (train_acc)) if epoch % save_step ==0: saver.save(sess,"save/nets/cnn_mnist_basic.ckpt-"+str(epoch))print("FINISHED")#测试if do_train ==0: epoch=training_epochs -1 saver.restore(sess,"save/nets/cnn_mnist_basic.ckpt-"+str(epoch)) test_acc=sess.run(accr,feed_dict= {
x:testimg ,y:testlabel ,keepratio:1.}) print("TEST ACCURACY: %.3f"% (test_acc))

转载地址:http://tfulf.baihongyu.com/

你可能感兴趣的文章
Istio 1.4 部署指南
查看>>
贫苦家庭用户的 Envoy xDS 控制平面
查看>>
Kubernetes Pod 网络精髓:pause 容器详解
查看>>
Docker 技术鼻祖 Linux Namespace 入门系列:Namespace API
查看>>
使用 ebpf 深入分析容器网络 dup 包问题
查看>>
Kubelet 中的 “PLEG is not healthy” 到底是个什么鬼?
查看>>
超详细的网络抓包神器 Tcpdump 使用指南
查看>>
从 Kubernetes 资源控制到开放应用模型,控制器的进化之旅
查看>>
从此以后运维与开发过上了没羞没臊的性福生活
查看>>
教你如何优雅地魔改 Grafana 主题,太实用了!
查看>>
让我们来看看回到单体的 Istio 到底该怎么部署
查看>>
超详细的网络抓包神器 tcpdump 使用指南
查看>>
iTerm2 都不会用,还敢自称老司机?(上)
查看>>
两个奇技淫巧,将 Docker 镜像体积减小 99%
查看>>
Istio 1.5 部署指南修正版
查看>>
不要轻易使用 Alpine 镜像来构建 Docker 镜像,有坑!
查看>>
Kubectl exec 背后到底发生了什么?
查看>>
程序员涨薪宝典
查看>>
什么?终止一个容器竟然用了 10 秒钟,这不能忍!
查看>>
Openshift 4.4 静态 IP 离线安装系列(一):准备离线资源
查看>>