手写数字识别 MNIST

梦里梦外; 2022-05-19 06:26 822阅读 0赞

预处理

  • tensorflow库内包含mnist,直接加载mnist数据并转为一维数组形式。直接加载的是.gz格式。

    import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据
    mnist = input_data.read_data_sets(“MNIST_data/“, one_hot=True) # one_hot为是否将标签转为一维数组形式

逻辑回归

  • 加载数据
  • 图片转为一维数组
  • 建立模型:softmax回归模型
  • w为可变n*784二维矩阵,b为10数组
  • w、b变量初始化为0
  • y=w*x+b
  • 损失函数:交叉熵
  • 训练模型
  • 模型评估

    -- coding: utf-8 --

  1. # 读取数据图片,预处理
  2. import tensorflow as tf
  3. import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据
  4. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # one_hot为是否将标签转为一维数组形式
  5. # 构建softmax回归模型
  6. sess = tf.InteractiveSession() # 交互作用类(不需用在计算前构建整个图)
  7. # 占位符(x和y_)
  8. x = tf.placeholder("float", shape=[None, 784]) # 浮点数,二维数组(第一维大小不定,第二维是784)
  9. y_ = tf.placeholder("float", shape=[None, 10]) # 用于代表对应某一MNIST图片的类别
  10. # 变量(w权重和b偏置)
  11. w = tf.Variable(tf.zeros([784, 10])) # 784*10的可变参数值二维矩阵
  12. b = tf.Variable(tf.zeros([10])) # 10维的向量
  13. sess.run(tf.initialize_all_variables()) # 初始化所有变量为0
  14. # 类别预测与损失函数
  15. y = tf.nn.softmax(tf.matmul(x, w) + b) # 计算每个分类的softmax概率值
  16. cross_entropy = -tf.reduce_sum(y_ * tf.log(y)) # 损失函数(目标类别和预测类别之间的交叉熵)
  17. # 训练模型
  18. train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) # 最速下降法,步长为0.01
  19. for i in range(1000):
  20. batch = mnist.train.next_batch(50) # 每步加载50个样本
  21. train_step.run(feed_dict={
  22. x: batch[0], y_: batch[1]}) # feed_dict被每次训练的数据替代
  23. # 模型评估
  24. correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) # 检测预测值与实际值是否匹配
  25. accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) # 将布尔数组转化为正确率
  26. print(accuracy.eval(feed_dict={
  27. x: mnist.test.images, y_: mnist.test.labels})) # 输出最终正确率

神经网络

  • 加载数据集
  • 定义函数:
  1. * 初始化函数
  2. * 神经元模型:mlp、逻辑回归、s函数
  • 照片转一维数组、确定测试、训练的照片、标签
  • 占位符:定义张量
  • 调用神经元模型函数
  • 计算代价函数、构造优化器、求行最值
  • 初始化变量、迭代100次
  • 打印正确率

    -- coding: utf-8 --

  1. # 神经网络
  2. import tensorflow as tf
  3. import numpy as np
  4. from tensorflow.examples.tutorials.mnist import input_data # 加载数据
  5. def init_weight(shape): # 初始化
  6. return tf.Variable(tf.random_normal(shape, stddev=0.01)) # (stddev是标准差,random_normal是正态分布的随机输出值)张量的可变随机值
  7. def model(X, w_h, w_o): # 神经元模型
  8. h = tf.nn.sigmoid(tf.matmul(X, w_h)) # 这是一个基本的mlp,两个堆栈逻辑回归 # X和w_h矩阵相乘,s函数
  9. return tf.matmul(h, w_o) # 在最后不使用softmax,因为代价函数 # 返回h和w_o的两矩阵之积
  10. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 把数据转换为一位数组的格式
  11. trX = mnist.train.images # 训练的图片
  12. trY = mnist.train.labels # 训练的标签
  13. teX = mnist.test.images # 测试的图片
  14. teY = mnist.test.labels # 测试的标签
  15. # 占位符
  16. X = tf.placeholder("float", [None, 784]) # 第一维长度不定,第二维长度为784的二维矩阵 浮点数 784=28*28
  17. Y = tf.placeholder("float", [None, 10]) # 输出的10种情况
  18. w_h = init_weight([784, 625]) # 创建特征变量 # 调用上边自定义的函数,对矩阵进行初始化
  19. w_o = init_weight([625, 10]) # 初始化
  20. py_x = model(X, w_h, w_o) # 调用上边的自定义函数,
  21. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # 计算代价
  22. train_op = tf.train.GradientDescentOptimizer(0.05).minimize(cost) # 构造优化器 # 0.05的学习率使代价函数最小
  23. predict_op = tf.argmax(py_x, 1) # 求行中最大值
  24. # 在会话中启动图表
  25. with tf.Session() as sess:
  26. # 初始化所有变量
  27. tf.global_variables_initializer().run() # 添加用于初始化变量的节点,
  28. for i in range(100): # 迭代100次
  29. for start, end in zip(range(0, len(trX)), range(128, len(trX)+1, 128)): # zip函数是将两个列表打包为元组的列表,元素个数与最短列表一致
  30. sess.run(train_op, feed_dict={X: trX[start: end], Y: trY[start:end]}) # 跑
  31. print(i, np.mean(np.argmax(teY, axis=1) ==
  32. sess.run(predict_op, feed_dict={X: teX}))) # mean函数是求平均值,打印预测和实际相同时的概率
  33. # for i in range(100):
  34. # for start, end in zip(range(0, 1000), range(128, 1000+1, 128)):
  35. # sess.run(train_op, feed_dict={X: trX[start: end], Y: trY[start:end]})
  36. # print(i, np.mean(np.argmax(teY, axis=1) ==
  37. # sess.run(predict_op, feed_dict={X: teX})))

卷积神经网络

  • CNN
  1. * 输入层
  2. * 卷积层
  3. * 激活函数
  4. * 池化层
  5. * 全连接层

卷积就是为了降维

池化就是数据压缩,特征压缩(提取主要特征)

  • 加载数据
  • 定义初始化函数、定义模型函数(relu、max_pool、dropout)
  • 图片转一维数组
  • 张量、初始化
  • 调用模型函数
  • 训练的下降率
  • argmax
  • 迭代10次
  • 测试集是打乱的(np.random.shuffle)
  • 打印准确率

    -- coding: utf-8 --

    #
    #

    # # 读取数据图片,预处理

    # import tensorflow as tf

    # import tensorflow.examples.tutorials.mnist.input_data as input_data # 加载mnist数据

    # mnist = input_data.read_data_sets(“MNIST_data/“, one_hot=True) # one_hot为是否将标签转为一维数组形式

    #
    #

    # 构建一个多层卷积神经网络

    import tensorflow as tf

    #
    #

    # 权重初始化

    def weight_variable(shape): # 权重

    initial = tf.truncated_normal(shape, stddev=0.01)

    return tf.Variable(initial)

    #
    #

    def bias_variable(shape): # 偏置

    initial = tf.constant(0.1, shape=shape)

    return tf.Variable(initial)

    #
    #

    # 卷积和池化

    def conv2d(x, W):

    return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding=’SAME’) # 1步长的卷积,0边距的模板(用0填充边界)

    #
    #

    def max_pool_2x2(x): # 池化使用2*2的模板

    return tf.nn.max_pool(x, ksize=[1, 2, 2, 1],

    strides=[1, 2, 2, 1], padding=’SAME’)

    #
    #

    # 第一层卷积

    W_conv1 = weight_variable([5, 5, 1, 32]) # 卷积在每个5*5的patch中算出32个特征

    b_conv1 = bias_variable([32]) # 每个输出的通道都有一个的对应的偏置量

    #

    x_image = tf.reshape(x, [-1, 28, 28, 1]) # x变为一个4d向量,第2、3维是图片的宽、高,第4维是图片的颜色通道数

    #

    h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1) # 把x_image和权值向量进行卷积,加上偏置项,然后使用RELU函数

    h_pool1 = max_pool_2x2(h_conv1) # 最后进行max_pooling(四个像素点中选取最大的)

    #
    #

    # 第二层卷积

    W_conv2 = weight_variable([5, 5, 32, 64]) # 每个5*5的patch中算出64个特征

    b_conv2 = bias_variable([64])

    #

    h_conv2 = tf.nn.relu(conv2d(h_pool1, W_conv2) + b_conv2)

    h_pool2 = max_pool_2x2(h_conv2)

    #
    #

    # 密集层网络

    W_fc1 = weight_variable([7764, 1024]) # 图片尺寸减少到了7*7,加入一个有1024个神经元的全连接层

    b_fc1 = bias_variable([1024])

    #

    h_pool2_flat = tf.reshape(h_pool2, [-1, 7764]) # 把池化层输出的张量reshape为一些向量

    h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1) # 乘上权重矩阵,加上偏置,然后使用RELU

    #
    #

    # Droput(减少过拟合)

    keep_prob = tf.placeholder(“float”) # 训练中启用dropout,测试中关闭的dropout

    h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

    #
    #

    # 输出层(添加一个softmax层)

    W_fc2 = weight_variable([1024, 10])

    b_fc2 = bias_variable([10])

    #

    y_conv = tf.nn.softmax(tf.matmul(h_fc1_drop, W_fc2) + b_fc2) # softmax函数

    #
    #

    # 训练和模型评估(ADAM优化器做梯度下降,每100次迭代输出一次日志)

    crossentropy = -tf.reduce_sum(y * tf.log(y_conv))

    train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)

    correctprediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y, 1))

    accuracy = tf.reduce_mean(tf.cast(correct_prediction, “float”))

    sess.run(tf.initialize_all_variables())

    for i in range(20000):

    batch = mnist.train.next_batch(50)

    if i % 100 == 0:

    train_accuracy = accuracy.eval(feed_dict={

    x: batch[0], y_: batch[1], keep_prob: 1.0

    })

    print(‘step %d, training accuracy %g’ % i, train_accuracy)

    trainstep.run(feed_dict={x: batch[0], y: batch[1], keep_prob: 0.5})

    #

    print(‘test accuracy %g’ % accuracy.eval(feed_dict={

    x: mnist.test.images, y_: mnist.test.labels, keep_prob: 1.0

    }))

    #
    #

    ———————————————————————————————————————————————————————————

    #

    # # -- coding: utf-8 --

    #
    #

    import tensorflow as tf

    import numpy as np

    from tensorflow.examples.tutorials.mnist import input_data

    #
    #

    batch_size = 128

    test_size = 256

    #
    #

    def init_weights(shape):

    return tf.Variable(tf.random_normal(shape, stddev=0.01))

    #
    #

    def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden):

    l1a = tf.nn.relu(tf.nn.conv2d(X, w,

    strides=[1, 1, 1, 1], padding=’SAME’))

    l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1],

    strides=[1, 2, 2, 1], padding=’SAME’)

    l1 = tf.nn.dropout(l1, p_keep_conv)

    #

    l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,

    strides=[1, 1, 1, 1], padding=’SAME’))

    l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1],

    strides=[1, 2, 2, 1], padding=’SAME’)

    l2 = tf.nn.dropout(l2, p_keep_conv)

    #

    l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,

    strides=[1, 1, 1, 1], padding=’SAME’))

    l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1],

    strides=[1, 2, 2, 1], padding=’SAME’)

    l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]])

    l3 = tf.nn.dropout(l3, p_keep_conv)

    #

    l4 = tf.nn.relu(tf.matmul(l3, w4))

    l4 = tf.nn.dropout(l4, p_keep_conv)

    #

    pyx = tf.matmul(l4, w_o)

    return pyx

    #
    #

    mnist = input_data.read_data_sets(“MNIST_data/“, one_hot=True)

    trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels

    trX = trX.reshape(-1, 28, 28, 1)

    teX = teX.reshape(-1, 28, 28, 1)

    #
    #

    X = tf.placeholder(“float”, [None, 28, 28, 1])

    Y = tf.placeholder(“float”, [None, 10])

    #
    #

    w = init_weights([3, 3, 1, 32])

    w2 = init_weights([3, 3, 32, 64])

    w3 = init_weights([3, 3, 64, 128])

    w4 = init_weights([128 4 4, 625])

    w_o = init_weights([625, 10])

    #
    #

    p_keep_conv = tf.placeholder(“float”)

    p_keep_hidden = tf.placeholder(“float”)

    py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden)

    #
    #

    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y))

    train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost)

    predict_op = tf.argmax(py_x, 1)

    #
    #

    #

    with tf.Session() as sess:

    #

    tf.global_variables_initializer().run()

    #

    for i in range(10):

    train_batch = zip(range(0, len(trX), batch_size),

    range(batch_size, len(trX) + 1, batch_size))

    for start, end in train_batch:

    sess.run(train_op, feed_dict={X: trX[start: end], Y: trY[start: end],

    p_keep_conv: 0.8, p_keep_hidden: 0.5})

    #

    test_indices = np.arange(len(teX))

    np.random.shuffle(test_indices)

    test_indices = test_indices[0:test_size]

    #

    print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==

    sess.run(predict_op, feed_dict={X: teY[test_indices],

    Y: teY[test_indices],

    p_keep_conv: 1.0,

    p_keep_hidden: 1.0})))

  1. # ----------------------------------------------------------------------------------------------------------------------
  2. import tensorflow as tf
  3. import numpy as np
  4. from tensorflow.examples.tutorials.mnist import input_data # 加载数据
  5. batch_size = 128
  6. test_size = 256
  7. def init_weights(shape): # 初始化
  8. return tf.Variable(tf.random_normal(shape, stddev=0.01))
  9. def model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden): # 定义的模型函数
  10. l1a = tf.nn.relu(tf.nn.conv2d(X, w,
  11. strides=[1, 1, 1, 1], padding='SAME')) # l1a shape=(?, 28, 28, 32)
  12. l1 = tf.nn.max_pool(l1a, ksize=[1, 2, 2, 1], # l1 shape=(?, 14, 14, 32)
  13. strides=[1, 2, 2, 1], padding='SAME')
  14. l1 = tf.nn.dropout(l1, p_keep_conv)
  15. l2a = tf.nn.relu(tf.nn.conv2d(l1, w2,
  16. strides=[1, 1, 1, 1], padding='SAME')) # l2a shape=(?, 14, 14, 64)
  17. l2 = tf.nn.max_pool(l2a, ksize=[1, 2, 2, 1], # l2 shape=(?, 7, 7, 64)
  18. strides=[1, 2, 2, 1], padding='SAME')
  19. l2 = tf.nn.dropout(l2, p_keep_conv)
  20. l3a = tf.nn.relu(tf.nn.conv2d(l2, w3,
  21. strides=[1, 1, 1, 1], padding='SAME')) # l3a shape=(?, 7, 7, 128)
  22. l3 = tf.nn.max_pool(l3a, ksize=[1, 2, 2, 1], # l3 shape=(?, 4, 4, 128)
  23. strides=[1, 2, 2, 1], padding='SAME')
  24. l3 = tf.reshape(l3, [-1, w4.get_shape().as_list()[0]]) # reshape to (?, 2048)
  25. l3 = tf.nn.dropout(l3, p_keep_conv)
  26. l4 = tf.nn.relu(tf.matmul(l3, w4))
  27. l4 = tf.nn.dropout(l4, p_keep_hidden)
  28. pyx = tf.matmul(l4, w_o)
  29. return pyx
  30. mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) # 将照片转一维数组
  31. trX, trY, teX, teY = mnist.train.images, mnist.train.labels, mnist.test.images, mnist.test.labels # 定义训练、测试的图片、属性
  32. trX = trX.reshape(-1, 28, 28, 1) # 28x28x1 input img
  33. teX = teX.reshape(-1, 28, 28, 1) # 28x28x1 input img
  34. X = tf.placeholder("float", [None, 28, 28, 1]) # 张量 浮点数 4维矩阵
  35. Y = tf.placeholder("float", [None, 10])
  36. w = init_weights([3, 3, 1, 32]) # 3x3x1 conv, 32 outputs
  37. w2 = init_weights([3, 3, 32, 64]) # 3x3x32 conv, 64 outputs
  38. w3 = init_weights([3, 3, 64, 128]) # 3x3x32 conv, 128 outputs
  39. w4 = init_weights([128 * 4 * 4, 625]) # FC 128 * 4 * 4 inputs, 625 outputs
  40. w_o = init_weights([625, 10]) # FC 625 inputs, 10 outputs (labels)
  41. p_keep_conv = tf.placeholder("float") # 卷积核多项式乘法
  42. p_keep_hidden = tf.placeholder("float") # 隐藏的
  43. py_x = model(X, w, w2, w3, w4, w_o, p_keep_conv, p_keep_hidden) # 调用模型函数
  44. cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=py_x, labels=Y)) # 准确率
  45. train_op = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cost) # 训练的下降率
  46. predict_op = tf.argmax(py_x, 1) # 求行中最大
  47. #
  48. with tf.Session() as sess:
  49. # 初始化所有可变值
  50. tf.global_variables_initializer().run() # 跑
  51. for i in range(10): # 迭代10次
  52. training_batch = zip(range(0, len(trX), batch_size),
  53. range(batch_size, len(trX)+1, batch_size)) # 训练批次
  54. for start, end in training_batch: # 训练
  55. sess.run(train_op, feed_dict={X: trX[start:end], Y: trY[start:end],
  56. p_keep_conv: 0.8, p_keep_hidden: 0.5})
  57. test_indices = np.arange(len(teX)) # 得到一个测试批
  58. np.random.shuffle(test_indices) # 打乱测试集
  59. test_indices = test_indices[0:test_size]
  60. print(i, np.mean(np.argmax(teY[test_indices], axis=1) ==
  61. sess.run(predict_op, feed_dict={X: teX[test_indices],
  62. Y: teY[test_indices],
  63. p_keep_conv: 1.0,
  64. p_keep_hidden: 1.0}))) # 打印预准率

发表评论

表情:
评论列表 (有 0 条评论,822人围观)

还没有评论,来说两句吧...

相关阅读