• MNIST_data手写辨识使用tensorflow框架


    # -*- coding: utf-8 -*-
    """
    Created on Sat May 26 14:31:09 2018


    @author: 被遗弃的庸才
    """


    import tensorflow as tf
    import numpy as np
    from tensorflow.examples.tutorials.mnist import input_data
    #28*28=784的像素点,y是10个位置上面的数据
    mnist=input_data.read_data_sets('MNIST_data',one_hot=True)#如果没有数据源会帮我们下载,如果有会用本地的数据源


    def compute_accuracy(v_xs,v_ys):
        #global prediction
        y_pre=sess.run(prediction,feed_dict={xs:v_xs,keep_prob:1.0})
        correct_prediction=tf.equal(tf.argmax(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
        


    def add_layer(inputs,in_size,out_size,activiation_function=None):
        Weight=tf.Variable(tf.random_normal([in_size, out_size]))*0.1 #如果relu参数初始化不理想,前向运算结果为负值,则进过梯度计算时候全部变零,在反向运算的时候没有响应,这就是“死区”现象。
        #如果使用sigmoid就不会出现这样的问题。。。。。。。。。。。当然前提是网络还不是很深的情况,如果很深可能会出现vanishing grading
        biases=tf.Variable(tf.zeros([1]))          
        Wx_plus_b=tf.matmul(inputs,Weight)+biases
        Wx_plus_b=tf.nn.dropout(Wx_plus_b,keep_prob)
        if activiation_function is None:
            outputs=Wx_plus_b
        else:
            outputs=activiation_function(Wx_plus_b)
        return outputs


    #定义输入的x,y变量
    keep_prob=tf.placeholder(tf.float32)
    xs=tf.placeholder(tf.float32,[None,784])
    ys=tf.placeholder(tf.float32,[None,10])


    #构建神经网络的结构
    l1=add_layer(xs,784,200,activiation_function=tf.nn.relu)
    l2=add_layer(l1,200,200,activiation_function=tf.nn.relu)
    l3=add_layer(l2,200,200,activiation_function=tf.nn.relu)






    prediction=add_layer(l3,200,10,activiation_function=tf.nn.softmax)


    #loss function 交叉熵
    loss=tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))


    #训练
    train=tf.train.AdamOptimizer(0.008).minimize(loss)#手动生成学习率


    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())#初始化所有的参数
        for i in range(501):
            batch_x,batch_y=mnist.train.next_batch(100)
            sess.run(train,feed_dict={xs:batch_x,ys:batch_y,keep_prob:0.5})
            if i%50==0:
                print(compute_accuracy(mnist.test.images,mnist.test.labels))
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
                
  • 相关阅读:
    计算机网络为什么是这样样子?
    MySQL技术内幕 InnoDB存储引擎 B+树索引的使用 笔记
    后端性能-batch 化的想法
    稳定高效的服务来自于稳定而合理的数据结构
    gRPC 学习了解记录
    Go 进阶训练营 Week02: error 错误处理
    生活小感受
    Nginx 499 排查到docker 中一个进程一直在空转
    方法论和原理总结
    Debug
  • 原文地址:https://www.cnblogs.com/csnd/p/16675643.html
Copyright © 2020-2023  润新知