快捷搜索:  汽车  科技

机器学习从零到一学习tensorflow(机器学习入门6)

机器学习从零到一学习tensorflow(机器学习入门6)2.获取数据集作用:将 TensorFlow 日志信息输出到屏幕之后打开jupyter notebook,一步一步来1.导入需要使用的包import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from matplotlib import pyplot as plt %matplotlib inline tf.logging.set_verbosity(tf.logging.INFO) tf.logging.set_verbosity(tf.logging.INFO)

本文主要内容:Ubuntu下基于Tensorflow的Mnist手写数字识别的实现

训练数据和测试数据资料:http://yann.lecun.com/exdb/mnist/

前面环境都搭建好了,直接在终端输入:

source activate tf1

jupyter notebook

之后打开jupyter notebook,一步一步来

1.导入需要使用的包

import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from matplotlib import pyplot as plt %matplotlib inline tf.logging.set_verbosity(tf.logging.INFO)

tf.logging.set_verbosity(tf.logging.INFO)

作用:将 TensorFlow 日志信息输出到屏幕

2.获取数据集

mnist = input_data.read_data_sets('./') print(mnist.train.images.shape) print(mnist.train.labels.shape) print(mnist.validation.images.shape) print(mnist.validation.labels.shape) print(mnist.test.images.shape) print(mnist.test.labels.shape)

输出:

Extracting ./train-images-idx3-ubyte.gz Extracting ./train-labels-idx1-ubyte.gz Extracting ./t10k-images-idx3-ubyte.gz Extracting ./t10k-labels-idx1-ubyte.gz (55000 784) (55000 ) (5000 784) (5000 ) (10000 784) (10000 )

3.输出一些image,数据的展示

plt.figure(figsize=(8 8)) for idx in range(16): plt.subplot(4 4 idx 1) plt.axis('off')#不显示坐标轴 plt.title('[{}]'.format(mnist.train.labels[idx]))#图片标题 plt.imshow(mnist.train.images[idx].reshape((28 28)))#显示一些图片,像素是28*28

输出:

机器学习从零到一学习tensorflow(机器学习入门6)(1)

分析:

figure(num=None figsize=None dpi=None facecolor=None edgecolor=None frameon=True)

num:图像编号或名称,数字为编号 ,字符串为名称

figsize:指定figure的宽和高,单位为英寸;

dpi参数指定绘图对象的分辨率,即每英寸多少个像素,缺省值为80

1英寸等于 2.5cm A4纸是 21*30cm的纸张

facecolor:背景颜色

edgecolor:边框颜色

frameon:是否显示边框

原文链接:https://blog.csdn.net/m0_37362454/article/details/81511427

4.定义用于训练的网络

#定义两个placeholder分别用于图像和label数据 x = tf.placeholder('float' [None 784]) y = tf.placeholder('int64' [None]) learning_rate = tf.placeholder('float')#设置学习率 def initialize(shape stddev=0.1): return tf.truncated_normal(shape stddev=0.1) #1. 隐层中的神经元个数 L1_units_count = 100 W_1 = tf.Variable(initialize([784 L1_units_count])) b_1 = tf.Variable(initialize([L1_units_count])) logits_1 = tf.matmul(x W_1) b_1 #将乘积数据激活函数,激活函数为ReLU output_1 = tf.nn.relu(logits_1) #2. 神经网络输出节点 ,共10个输出点 L2_units_count = 10 W_2 = tf.Variable(initialize([L1_units_count L2_units_count])) b_2 = tf.Variable(initialize([L2_units_count])) logits_2 = tf.matmul(output_1 W_2) b_2 logits = logits_2 #定义loss和用于优化网络的优化器 loss计算使用了sparse_softmax_cross_entropy_with_logits 这样做的好处 #是labels可以不用手动 #做one_hot省了一些麻烦。这里使用sgd优化器,学习率可以根据需要设定 #拓展--可以尝试增大学习率,换个优化器再进行训练 cross_entropy_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits labels=y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy_loss)

分析:

(1)tf.truncated_normal(shape mean=0.0 stddev=1.0 dtype=tf.float32 seed=None name=None)

作用:从截断的正态分布中输出随机值。

参数:

  • shape: 张量维度
  • mean: 正态分布的均值
  • stddev: 正态分布的标准差
  • dtype: 输出的类型
  • seed: 一个整数,当设置之后,每次生成的随机数都一样
  • name: 操作的名字

(2)为什么要用placeholder?

placeholder()函数是在神经网络构建graph的时候在模型中的占位,此时并没有把要输入的数据传入模型,它只会分配必要的内存。等建立session,在会话中,运行模型的时候通过feed_dict()函数向占位符喂入数据。

(3)tf.Variable和tf.placeholder的区别

tf.Variable

tf.Variable主要用于可训练的一些变量,比如模型的权重(weight ,w) 模型的偏置值(bias b)

1.声明时必须要进行初始化

2.名称的真实含义在于变量,也就是在训练时,其值是可以改变的

tf.placeholder

tf.placeholder用于得到传递进来的真实样本

1.不必进行初始化 通过session.run中的feed_dic={}来指定

2.仅仅作为一种占位符

(4)tf.reduce_mean 函数用于计算张量tensor沿着指定的数轴(tensor的某一维度)上的的平均值,主要用作降维或者计算tensor(图像)的平均值。

第一个参数input_tensor: 输入的待降维的tensor;

第二个参数axis: 指定的轴,如果不指定,则计算所有元素的均值;

第三个参数keep_dims:是否降维度,设置为True,输出的结果保持输入tensor的形状,设置为False,输出结果会降低维度;

第四个参数name: 操作的名称;

第五个参数 reduction_indices:在以前版本中用来指定轴,已弃用;

(5)tf.train.GradientDescentOptimizer()使用随机梯度下降算法

tf.train.MomentumOptimizer()在更新参数时,利用了超参数

tf.train.AdamOptimizer()是利用自适应学习率的优化算法

5.模型训练

#softmax概率分类 pred = tf.nn.softmax(logits) correct_pred = tf.equal(tf.argmax(pred 1) y) accuracy = tf.reduce_mean(tf.cast(correct_pred tf.float32)) #saver用于保存或恢复训练的模型 batch_size = 32 trainig_step = 1000 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) validate_data = { x: mnist.validation.images y: mnist.validation.labels } test_data = {x: mnist.test.images y: mnist.test.labels} for i in range(trainig_step): xs ys = mnist.train.next_batch(batch_size) _ loss = sess.run( [optimizer cross_entropy_loss] feed_dict={ x: xs y: ys learning_rate: 0.3 } ) if i>0 and i0 == 0: validate_accuracy = sess.run(accuracy feed_dict=validate_data) print("after %d training steps the loss is %g the validation accuracy is %g" % (i loss validate_accuracy)) saver.save(sess './model.ckpt' global_step=i) print('the training is finish') acc = sess.run(accuracy feed_dict=test_data) print('the test accuracy is:' acc)

输出:

after 100 training steps the loss is 0.343405 the validation accuracy is 0.891

after 200 training steps the loss is 0.268626 the validation accuracy is 0.8902

after 300 training steps the loss is 0.23175 the validation accuracy is 0.9224

after 400 training steps the loss is 0.110849 the validation accuracy is 0.9346

after 500 training steps the loss is 0.140661 the validation accuracy is 0.9368

after 600 training steps the loss is 0.301867 the validation accuracy is 0.9402

after 700 training steps the loss is 0.143711 the validation accuracy is 0.939

after 800 training steps the loss is 0.425264 the validation accuracy is 0.9456

after 900 training steps the loss is 0.135332 the validation accuracy is 0.95

the training is finish

the test accuracy is: 0.9497

6.测试模型

with tf.Session() as sess: #函数功能:找出训练时保存的模型 ckpt = tf.train.get_checkpoint_state('./') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess ckpt.model_checkpoint_path) final_pred acc = sess.run( [pred accuracy] feed_dict={ x: mnist.test.images[:16] y: mnist.test.labels[:16] } ) orders = np.argsort(final_pred) plt.figure(figsize=(8 8)) print(acc) for idx in range(32): order = orders[idx :][-1] prob = final_pred[idx :][order] plt.subplot(4 4 idx 1) plt.axis('off') plt.title('{}: [{}]-[{:.1f}%]'.format(mnist.test.labels[idx] order prob * 100)) plt.imshow(mnist.test.images[idx].reshape((28 28))) else: pass

分析:

(1)tf.train.get_checkpoint_state 函数功能:找出训练时保存的模型

(2)ckpt.model_checkpoint_path 可以找出所有模型中最新的模型

模型识别的输出结果:

机器学习从零到一学习tensorflow(机器学习入门6)(2)


全部代码:

import numpy as np import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data from matplotlib import pyplot as plt %matplotlib inline tf.logging.set_verbosity(tf.logging.INFO) mnist = input_data.read_data_sets('./') print(mnist.train.images.shape) print(mnist.train.labels.shape) print(mnist.validation.images.shape) print(mnist.validation.labels.shape) print(mnist.test.images.shape) print(mnist.test.labels.shape) plt.figure(figsize=(8 8)) for idx in range(16): plt.subplot(4 4 idx 1) plt.axis('off') plt.title('[{}]'.format(mnist.train.labels[idx])) plt.imshow(mnist.train.images[idx].reshape((28 28))) x = tf.placeholder('float' [None 784]) y = tf.placeholder('int64' [None]) learning_rate = tf.placeholder('float') def initialize(shape stddev=0.1): return tf.truncated_normal(shape stddev=0.1) #1. 隐层中的神经元个数 L1_units_count = 100 W_1 = tf.Variable(initialize([784 L1_units_count])) b_1 = tf.Variable(initialize([L1_units_count])) logits_1 = tf.matmul(x W_1) b_1 #将乘积数据激活函数,激活函数为ReLU output_1 = tf.nn.relu(logits_1) #2. 神经网络输出节点 ,共10个输出点 L2_units_count = 10 W_2 = tf.Variable(initialize([L1_units_count L2_units_count])) b_2 = tf.Variable(initialize([L2_units_count])) logits_2 = tf.matmul(output_1 W_2) b_2 logits = logits_2 cross_entropy_loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits labels=y)) optimizer = tf.train.GradientDescentOptimizer(learning_rate=learning_rate).minimize(cross_entropy_loss) #softmax概率分类 pred = tf.nn.softmax(logits) correct_pred = tf.equal(tf.argmax(pred 1) y) accuracy = tf.reduce_mean(tf.cast(correct_pred tf.float32)) #saver用于保存或恢复训练的模型 batch_size = 32 trainig_step = 1000 saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) validate_data = { x: mnist.validation.images y: mnist.validation.labels } test_data = {x: mnist.test.images y: mnist.test.labels} for i in range(trainig_step): xs ys = mnist.train.next_batch(batch_size) _ loss = sess.run( [optimizer cross_entropy_loss] feed_dict={ x: xs y: ys learning_rate: 0.3 } ) if i>0 and i0 == 0: validate_accuracy = sess.run(accuracy feed_dict=validate_data) print("after %d training steps the loss is %g the validation accuracy is %g" % (i loss validate_accuracy)) saver.save(sess './model.ckpt' global_step=i) print('the training is finish') acc = sess.run(accuracy feed_dict=test_data) print('the test accuracy is:' acc) with tf.Session() as sess: ckpt = tf.train.get_checkpoint_state('./') if ckpt and ckpt.model_checkpoint_path: saver.restore(sess ckpt.model_checkpoint_path) final_pred acc = sess.run( [pred accuracy] feed_dict={ x: mnist.test.images[:16] y: mnist.test.labels[:16] } ) orders = np.argsort(final_pred) plt.figure(figsize=(8 8)) print(acc) for idx in range(32): order = orders[idx :][-1] prob = final_pred[idx :][order] plt.subplot(4 4 idx 1) plt.axis('off') plt.title('{}: [{}]-[{:.1f}%]'.format(mnist.test.labels[idx] order prob * 100)) plt.imshow(mnist.test.images[idx].reshape((28 28))) else: pass

猜您喜欢: