简单的神经网络实现手写数字图片识别

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
print("++++++++++++++++神经学习+++++++++++++++++")

FLAGS = tf.app.flags.FLAGS
tf.app.flags.DEFINE_integer("is_train", 0, "指定程序是预测还是训练")



def full_connected():

    # 获取真实数据
    mnist = input_data.read_data_sets("./data/mnist/input_data", one_hot=True)
    print(mnist)
    with tf.compat.v1.variable_scope("data"):
        x = tf.compat.v1.placeholder(tf.float32, [None, 784])

        y_true = tf.compat.v1.placeholder(tf.int32, [None, 10])


    with tf.compat.v1.variable_scope("fc_model"):
        # 随机初始化权重与偏置
        weight = tf.Variable(tf.random.normal([784, 10], mean=0.0, stddev=1.0), name="W")

        bias = tf.Variable(tf.constant(0.0, shape=[10]))


        # 预测none个样本的输出结果
        y_predict = tf.matmul(x, weight) + bias


    # 3. 求出所有样本的损失, 然后求平均值
    with tf.compat.v1.variable_scope("soft_cross"):

        # 求平均交叉熵损失
        loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(labels=y_true, logits=y_predict))


    # 4. 梯度下降求出损失
    with tf.compat.v1.variable_scope("optimizer"):

        train_on = tf.compat.v1.train.GradientDescentOptimizer(0.1).minimize(loss)

    # 5. 计算准确率
    with tf.compat.v1.variable_scope("acc"):

        equal_list = tf.equal(tf.argmax(y_true, 1), tf.argmax(y_predict, 1))

        #
        accuracy = tf.reduce_mean(tf.cast(equal_list, tf.float32))

    # 收集变量  单个数字值收集
    tf.summary.scalar("losses", loss)
    tf.summary.scalar("acc", accuracy)

    # 高纬度变量收集
    tf.summary.histogram("weights", weight)
    tf.summary.histogram("biases", bias)


    # 定义一个初始化变量的op
    init_op = tf.compat.v1.global_variables_initializer()

    # 定义一个合并变量的op
    merged = tf.summary.merge_all()


    # 创建一个saver
    saver = tf.train.Saver()


    with tf.compat.v1.Session() as sess:

        # 初始化变量
        sess.run(init_op)

        # 建立events文件, 然后写入
        filewriter = tf.summary.FileWriter("./data/tmp/test", graph=sess.graph)

        if FLAGS.is_train == 1:

            for i in range(2000):

                # 取出真实的特征值与目标值
                mnist_x, mnist_y = mnist.train.next_batch(50)

                # 运行训练
                sess.run(train_on, feed_dict={x: mnist_x, y_true: mnist_y})

                 # 写入每步训练的值
                summery = sess.run(merged, feed_dict={x: mnist_x, y_true: mnist_y})

                filewriter.add_summary(summery, i)

                print("训练第%d步, 准确率为:%f" % (i, sess.run(accuracy, feed_dict={x: mnist_x, y_true: mnist_y})))

            # 保存模型
            saver.save(sess, "./data/model/fc_model")
        else:
            # 加载模型
            saver.restore(sess, "./data/model/fc_model")
            # 如果是0, 做出测试
            for i in range(100):

                # 每次测试一张图片
                x_test, y_test = mnist.test.next_batch(1)
                print("第%d张图片, 手写数字目标是:%d, 预测结果是:%d" % (
                    i,
                    tf.argmax(y_test, 1).eval(),
                    tf.argmax(sess.run(y_predict, feed_dict={x: x_test, y_true: y_test}), 1).eval()

                ))


    return None



if __name__ == "__main__":
    full_connected()

数据集下载地址:http://yann.lecun.com/exdb/mnist/

发布了129 篇原创文章 · 获赞 11 · 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Batac_Lee/article/details/103512324