机器学习之简单神经网络手写数字识别

    科技2022-08-20  107

    注意:

    该程序运行环境为:pycharm2020+python3.7+tensorflow2.2 cpu版本

    因为在学习的过程时,学习视频使用的是tensorflow2.x以下的版本,所以在运行中出现了许多错误,不过已经更正。下面代码都可正常运行。需注意的是: 当导入mnist数据集时会报出 ModuleNotFoundError: No module named ‘tensorflow.examples.tutorials’ 错误

    原因是tensorflow_core中缺tutorial文件夹

    解决办法就是下载缺失的tutorial文件夹放到 tensorflow_core 目录下

    但是tensorflow2.2可能找不到tensorflow_core,这是因为2.2版本将core合并到tensorflow,所以,将下载好的tutorial文件夹放在tensorflow的examples里即可,下载地址如下: tutorial文件【mnist数据集】

    1、神经网络流程分析

    1.1 步骤1使用的API

    1.1.1 全连接-从输入直接到输出

    特征加权:

    tf.matmul(a, b,name=None)+bias
    return:全连接结果,供交叉损失运算不需要激活函数(因为是最后的输出)

    1.2 步骤2使用的API

    1.2.1 SoftMax计算、交叉熵

    tf.nn.softmax_cross_entropy_with_logits(labels=None, logits=None,name=None)
    计算logits和labels之间的交叉损失熵
    labels:标签值(真实值)logits:样本加权之后的值,预测值return:返回损失值列表

    1.2.2 损失值列表平均值计算

    tf.reduce_mean(input_tensor)
    计算张量的尺寸的元素平均值

    1.2.3 准确性计算

    1、equal_list = tf.equal(tf.argmax(y, 1), tf.argmax(y_label, 1))
    2、accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))

    2、手写数字识别案例

    2.1 单层(全连接层)实现⼿写数字识别流程分析

    特征值[None, 784] ⽬标值[None, 10]

    1、建⽴模型
    随机初始化权重和偏置w [784, 10] <——————>b = [10]y_predict = tf.matmul(x, w) + b
    2、计算损失
    loss 平均样本损失
    3、梯度下降优化
    0.1学习率、 步数 2000 、准确率

    2.1 ⼿写数字识别代码

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data FLAGS = tf.compat.v1.app.flags.FLAGS tf.compat.v1.disable_eager_execution() tf.compat.v1.flags.DEFINE_integer("is_train", 1, "指定程序是预测还是训练") def full_connected(): # 获取真实的数据 mnist = input_data.read_data_sets("./data/mnist/input_data/", one_hot=True) # 1、建立数据的占位符 x [None, 784] y_true [None, 10] with tf.compat.v1.variable_scope("data"): x = tf.compat.v1.placeholder(tf.compat.v1.float32, [None, 784]) y_true = tf.compat.v1.placeholder(tf.compat.v1.int32, [None, 10]) # 2、建立一个全连接层的神经网络 w [784, 10] b [10] with tf.compat.v1.variable_scope("fc_model"): # 随机初始化权重和偏置 weight = tf.compat.v1.Variable(tf.compat.v1.random.normal([784, 10], mean=0.0, stddev=1.0), name="w") bias = tf.compat.v1.Variable(tf.compat.v1.constant(0.0, shape=[10])) # 预测None个样本的输出结果matrix [None, 784]* [784, 10] + [10] = [None, 10] y_predict = tf.compat.v1.matmul(x, weight) + bias # 3、求出所有样本的损失,然后求平均值 with tf.compat.v1.variable_scope("soft_cross"): # 求平均交叉熵损失 loss = tf.compat.v1.reduce_mean(tf.compat.v1.nn.softmax_cross_entropy_with_logits(labels=y_true, logits=y_predict)) # 4、梯度下降求出损失 with tf.compat.v1.variable_scope("optimizer"): train_op = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss) # 5、计算准确率 with tf.compat.v1.variable_scope("acc"): equal_list = tf.compat.v1.equal(tf.compat.v1.argmax(y_true, 1), tf.compat.v1.argmax(y_predict, 1)) # equal_list None个样本 [1, 0, 1, 0, 1, 1,..........] accuracy = tf.compat.v1.reduce_mean(tf.compat.v1.cast(equal_list, tf.float32)) # 6.1、收集变量 单个数字值收集 步骤6、为观察准确率和损失率是如何变化的。 tf.compat.v1.summary.scalar("losses", loss) tf.compat.v1.summary.scalar("acc", accuracy) # 6.2、高纬度变量收集 步骤6、为观察准确率和损失率是如何变化的。 tf.compat.v1.summary.histogram("weightes", weight) tf.compat.v1.summary.histogram("biases", bias) # 定义一个初始化变量的op init_op = tf.compat.v1.global_variables_initializer() # 6.3、定义一个合并变量de op 步骤6、为观察准确率和损失率是如何变化的。 merged = tf.compat.v1.summary.merge_all() # 创建一个saver saver = tf.compat.v1.train.Saver() # 开启会话去训练 with tf.compat.v1.Session() as sess: # 初始化变量 sess.run(init_op) # 6.4、建立events文件,然后写入 步骤6、为观察准确率和损失率是如何变化的。 filewriter = tf.compat.v1.summary.FileWriter("./tmp/summary/test/", graph=sess.graph) if FLAGS.is_train == 1: # 迭代步数去训练,更新参数预测 for i in range(2000): # 取出真实存在的特征值和目标值 mnist_x, mnist_y = mnist.train.next_batch(50) # 运行train_op训练 sess.run(train_op, feed_dict={x: mnist_x, y_true: mnist_y}) # 6.5、写入每步训练的值 步骤6、为观察准确率和损失率是如何变化的。 summary = sess.run(merged, feed_dict={x: mnist_x, y_true: mnist_y}) filewriter.add_summary(summary, i) # 6.6、 步骤6、为观察准确率和损失率是如何变化的。 print("训练第%d步,准确率为:%f" % (i, sess.run(accuracy, feed_dict={x: mnist_x, y_true: mnist_y}))) # 保存模型 saver.save(sess, "./tmp/ckpt/fc_model") else: # 加载模型 saver.restore(sess, "./tmp/ckpt/fc_model") # 如果是0,做出预测 for i in range(100): # 每次测试一张图片 [0,0,0,0,0,1,0,0,0,0] x_test, y_test = mnist.test.next_batch(1) print("第%d张图片,手写数字图片目标是:%d, 预测结果是:%d" % ( i, tf.compat.v1.argmax(y_test, 1).eval(), tf.compat.v1.argmax(sess.run(y_predict, feed_dict={x: x_test, y_true: y_test}), 1).eval() )) return None if __name__ == "__main__": full_connected() 结果输出:训练第0步,准确率为:0.160000 训练第1步,准确率为:0.060000 训练第2步,准确率为:0.140000 训练第3步, 准确率为:0.040000 训练第4步,准确率为:0.160000 训练第5步,准确率为:0.120000 ... 训练第1986步,准确率为:0.900000 训练第1987步,准确率为:0.800000 训练第1988步,准确率为:0.900000 训练第1989步,准确率为:0.940000 ... 训练第1998步,准确率为:0.800000 训练第1999步,准确率为:0.860000 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data FLAGS = tf.compat.v1.app.flags.FLAGS tf.compat.v1.disable_eager_execution() tf.compat.v1.flags.DEFINE_integer("is_train", 0, "指定程序是预测还是训练") # 当is_train=0时程序是预测 # 通过训练后保存的模型进行手写数字的预测 # 预测结果为见下: 预测结果: 第0张图片,手写数字图片目标是:8, 预测结果是:8 第1张图片,手写数字图片目标是:3, 预测结果是:3 第2张图片,手写数字图片目标是:0, 预测结果是:0 第3张图片,手写数字图片目标是:5, 预测结果是:5 .... 第95张图片,手写数字图片目标是:9, 预测结果是:9 第96张图片,手写数字图片目标是:2, 预测结果是:2 第97张图片,手写数字图片目标是:6, 预测结果是:6 第98张图片,手写数字图片目标是:8, 预测结果是:5 第99张图片,手写数字图片目标是:7, 预测结果是:7

    Column 1Column 2
    Processed: 0.010, SQL: 10