手書きデジタル識別------softmax回帰モデル公式ケースコメント(Tensorflow,Pythonに基づく)

10210 ワード

#         ----Softmax    
# regression
import os
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
data = input_data.read_data_sets("/tmp/data/", one_hot=True)  #      mnist        ,   Numpy            、   、   。


#
#
# 1、                      (         )   ,
#        (      softmax      ,         ),
#
# 2、softmax  :            (          ,    ,      softmax     )
#
# ①     ,     
#         x,        i    
# evidencei =∑i(wi, jxj) + bi
# evidence_i =∑_i(w_{i, j}x_j)+b_ievidencei=∑i​(wi, jxj) + bi
# ②  softmax   evidence     , 
# y = softmax(evidence)
# y = softmax(evidence)
# y = softmax(evidence)
#
#        
# y = softmax(Wx + b)
# y = softmax(Wx + b)
# y = softmax(Wx + b)
# 3、     python                    ,TensorFlow      ,              ,      python   。
#     placeholder           :
# ---------------------
#   :Crystal 
#   :CSDN
#   :https: // blog.csdn.net / weixin_43226400 / article / details / 82749769
#     :         ,         !


#http://www.cnblogs.com/rgvb178/p/6052541.html     
# Softmax Regression Model        Softmax    
def regression(x):
    W = tf.Variable(tf.zeros([784, 10]), name="W")
    b = tf.Variable(tf.zeros([10]), name="b")
    y = tf.nn.softmax(tf.matmul(x, W) + b)
    # print(y)
    return y, [W, b]


# model      
with tf.variable_scope("regression"):
    x = tf.placeholder(tf.float32, [None, 784])
    y, variables = regression(x)

#     (cross - entropy)        ,     
# Hy′(y) =−∑iy′ilog(yi)
#   y        ,y’        (           ,   one - hotvector)  


# train        
y_ = tf.placeholder("float", [None, 10])
#      
cross_entropy = -tf.reduce_sum(y_ * tf.log(y))
# tensorflow            ,                  
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
# tf.argmax                    ,            , 
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
# equal       , cast                  
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

saver = tf.train.Saver(variables)
with tf.Session() as sess:
    #      
    sess.run(tf.global_variables_initializer())
    for _ in range(10000):
        batch_xs, batch_ys = data.train.next_batch(100)
        #           ,             100       batch
        sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})

    #                  
    print(sess.run(accuracy, feed_dict={x: data.test.images, y_: data.test.labels}))

    #       
    # print(os.path.join(os.path.dirname(__file__), 'data', 'regression.ckpt'))
    # //                         :'mnist/data/regression.ckpt'
    path = saver.save(
        sess, 'mnist/data/regression.ckpt',
        write_meta_graph=False, write_state=False)
    print("Saved:", path)

    # path = saver.save(
    #     sess, os.path.join(os.path.dirname(__file__), 'mnist\data', 'regression.ckpt'),write_meta_graph=False, write_state=False)
    # print("Saved:", path)        write_meta_graph=False, write_state=False)
    print("Saved:", path)

    # path = saver.save(
    #     sess, os.path.join(os.path.dirname(__file__), 'mnist\data', 'regression.ckpt'),write_meta_graph=False, write_state=False)
    # print("Saved:", path)