zoukankan      html  css  js  c++  java
  • tensorflow mnist数据集 普通神经网络

      今天写一个mnist数据集的神经网络识别 

      mnist数据集是机器学习的Hello World ,这篇博客用最简单的神经网络classfy 数据集中的数字

    1.import tensorflow 然后载入数据集,在tensorflow中google已经帮我们封装好了这个数据集,直接  从tensorflow.example.tutorial.mnist中 import就行。然后input_data中读入数据,在hot_hot参数中设置True。他会帮我们自动编码成‘one-hot’

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    # number 1 to 10 data
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)

    2.这里我们写一个add_layer的函数,增加一个隐藏层,参数有inputs:输入数据, in_size:输入数据的大小, out_size:输出数据的大小, activate_function:激活函数,初始值设为None,当传入激活函数时,add_layer会输出经过激活函数之后的值

    def add_layer(inputs, in_size, out_size, activate_function=None):
        Weight = tf.Variable(tf.random_normal([in_size, out_size]))
        biases = tf.Variable(tf.zeros([1, out_size])+ 0.1,)
        Wx_plus_b = tf.matmul(inputs, Weight) + biases
        if activate_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activate_function(Wx_plus_b)
        return outputs

     3.新建两个占位符 ys(labels) 它的大小是10,也就是数字0到9的one-hot编码  xs(features) 是每幅图像的像素点(28*28)

    ys = tf.placeholder(tf.int64, [None, 10])
    xs = tf.placeholder(tf.float32, [None, 784])

    4.logits: logits就是神经网络的输出值。没有经过softmax函数缩放的值

    logits= add_layer(xs, 784, 10, activate_function=None)

    5.计算损失函数。 这里我用tf.nn.softmax_cross_entropy_with_logtis,这是一个tensorflow封装好了的函数,这边的可以直接把logtis和labels放进去,就可以直接得到经过soft_max后的logits和labels的交叉熵loss

    关于几个不同的损失函数,可以看我另一篇博客整理的内容https://www.cnblogs.com/francischeng/p/9836341.html

    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=ys))

    6.生成优化器,这里我选择了GrandientDescent方法进行优化 ,学习率(learning rate)设置成了0.5

    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)

    7.def计算准确度函数 :

     这边传入的参数v_xs和v_ys将会是整个数据集的数据,会计算模型对于整个数据集的准确度

    def compute_accuracy(v_xs, v_ys):
        y_pre = sess.run(logits, feed_dict={xs: v_xs})
        correct_prediction = tf.equal(tf.arg_max(y_pre, 1), tf.arg_max(v_ys, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})
        return result

    8. 跑起来之前的准备 

    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)

    9.跑起来

    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={xs:batch_xs, ys:batch_ys})
        if i%50 == 0:
            print(compute_accuracy(mnist.test.images, mnist.test.labels))

    完整代码1

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    
    
    def add_layer(inputs, in_size, out_size, activate_function=None):
        Weight = tf.Variable(tf.random_normal([in_size, out_size]))
        biases = tf.Variable(tf.zeros([1, out_size])+ 0.1,)
        Wx_plus_b = tf.matmul(inputs, Weight) + biases
        if activate_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activate_function(Wx_plus_b)
        return outputs
        
    def compute_accuracy(v_xs, v_ys):
        y_pre = sess.run(logits, feed_dict={xs: v_xs})
        correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        result = sess.run(accuracy, feed_dict={xs:v_xs, ys:v_ys})
        return result
    
    ys = tf.placeholder(tf.int64, [None, 10])
    xs = tf.placeholder(tf.float32, [None, 784])
    
    
    logits= add_layer(xs, 784, 10, activate_function=None)
    
    loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=logits, labels=ys))
    
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
    init = tf.global_variables_initializer()
    
    
    
    sess = tf.Session()
    init = tf.global_variables_initializer()
    sess.run(init)
    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={xs:batch_xs, ys:batch_ys})
        if i%50 == 0:
            print(compute_accuracy(mnist.test.images, mnist.test.labels))

    完整代码2:

    这个是没有用tf.nn.softmax_cross_entropy_with_logits, 手动添加了soft_max并计算了交叉熵

    import tensorflow as tf
    from tensorflow.examples.tutorials.mnist import input_data
    
    mnist = input_data.read_data_sets('MNIST_data', one_hot=True)
    
    
    def add_layer(inputs, in_size, out_size, activation_function=None, ):
        Weights = tf.Variable(tf.random_normal([in_size, out_size]))
        biases = tf.Variable(tf.zeros([1, out_size]) + 0.1, )
        Wx_plus_b = tf.matmul(inputs, Weights) + biases
        if activation_function is None:
            outputs = Wx_plus_b
        else:
            outputs = activation_function(Wx_plus_b, )
        return outputs
    
    
    def compute_accuracy(v_xs, v_ys):
        global prediction
        y_pre = sess.run(prediction, feed_dict={xs: v_xs})
        correct_prediction = tf.equal(tf.argmax(y_pre, 1), tf.argmax(v_ys, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys})
        return result
    
    
    xs = tf.placeholder(tf.float32, [None, 784])  # 28x28
    ys = tf.placeholder(tf.float32, [None, 10])
    
    prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)
    
    
    cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
                                                  reduction_indices=[1]))
    train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
    
    sess = tf.Session()
    
    
    init = tf.global_variables_initializer()
    sess.run(init)
    
    for i in range(1000):
        batch_xs, batch_ys = mnist.train.next_batch(100)
        sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
        if i % 50 == 0:
            print(compute_accuracy(
                mnist.test.images, mnist.test.labels))
  • 相关阅读:
    弹框只弹一次(cookie)
    多个列表求笛卡尔积的几种方法
    mysqlrouter 8.0.17启动失败
    Eclipse启动项目成功,IDEA报错java.lang.ClassNotFoundException: javax.servlet.Filter
    java的回调机制,讲得很清楚
    Java 命令行 -D
    spring boot项目中,webservice生成客户端,wsdl可配置
    一个老项目的高并发改造,遇到的redis连接不释放问题。
    ExecuterService实现线程池
    子线程获取不到请求中变量的问题
  • 原文地址:https://www.cnblogs.com/francischeng/p/9849757.html
Copyright © 2011-2022 走看看