欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TensorFlow第三步 :单层网络-Mnist手写数字识别

程序员文章站 2022-07-07 09:46:58
...

一、载入数据Mnist,并检验数据

# coding=utf-8
import os  
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error 

"""
mnist_loader
~~~~~~~~~~~~
A library to load the MNIST image data.  For details of the data
structures that are returned, see the doc strings for ``load_data``
from tensorflow.python.ops.distributions.kullback_leibler import cross_entropy
from lib2to3.tests.data.infinite_recursion import sess_cert_st
"""
#### Libraries
# Standard library
import pickle
import gzip
# Third-party libraries
import numpy as np

def load_data():
    """Return the MNIST data as a tuple containing the training data,
    the validation data, and the test data.

    The ``training_data`` is returned as a tuple with two entries.
    The first entry contains the actual training images.  This is a
    numpy ndarray with 50,000 entries.  Each entry is, in turn, a
    numpy ndarray with 784 values, representing the 28 * 28 = 784
    pixels in a single MNIST image.

    The second entry in the ``training_data`` tuple is a numpy ndarray
    containing 50,000 entries.  Those entries are just the digit
    values (0...9) for the corresponding images contained in the first
    entry of the tuple.

    The ``validation_data`` and ``test_data`` are similar, except
    each contains only 10,000 images.
    """
    f = gzip.open('../data/mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f,encoding='bytes')
    f.close()
    return (training_data, validation_data, test_data)

def vectorized_result(j):
    """Return a 10-dimensional unit vector with a 1.0 in the jth
    position and zeroes elsewhere.  This is used to convert a digit
    (0...9) into a corresponding desired output from the neural
    network."""
    e = np.zeros(10)
    e[j] = 1.0
    return e

import tensorflow as tf
import matplotlib.pyplot as plt
from random import randint

logs_path=r'c:/temp/log_mnist_softmax'
batch_size=100
learning_rate=0.005 #当>0.05时误差很大
training_epochs=2

training_data, validation_data, test_data = load_data()
trainData_in=training_data[0]
trainData_out=[vectorized_result(j) for j in training_data[1]]
validData_in=validation_data[0]
validData_out=[vectorized_result(j) for j in validation_data[1]]
testData_in=test_data[0]
testData_out=[vectorized_result(j) for j in test_data[1]]

print(np.shape(trainData_in))
print(np.shape(trainData_out))
print(trainData_out[0])
I=trainData_in[0]
J=trainData_out[0]
I.resize(28,28)
plt.imshow(I,cmap='Greys_r')
plt.show()
print (list(J).index(max(J))) #J是array,转成list才有index
with tf.Session() as sess:
    print(sess.run(tf.argmax(J)))

(50000, 784)

(50000, 10)

[0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]

5

5

TensorFlow第三步 :单层网络-Mnist手写数字识别

二、单层前馈网络10个神经元、用交叉熵损失函数,softmax激励函数(各神经元输出值可表示为正确输出的概率,全部和为1)。用training_data训练网络,用test_data检验训练的结果。最后保存网络模型。

x_input=tf.placeholder(tf.float32, [None,784], name='x_input')
y_desired=tf.placeholder(tf.float32,[None,10])
w=tf.Variable(tf.zeros([784,10]))
b=tf.Variable(tf.zeros([10]))
y_output=tf.nn.softmax(tf.matmul(x_input,w)+b,name='y_output')
lossFun_crossEntropy=-tf.reduce_mean(y_desired*tf.log(y_output))*1000.0

correct_prediction=tf.equal(tf.argmax(y_output,1),\
                             tf.argmax(y_desired,1)) #1:按行索引,每行得一索引值
accuracy=tf.reduce_mean(tf.cast(correct_prediction,\
                                tf.float32))#将逻辑型变成数字型,再求均值

train_step=tf.train.GradientDescentOptimizer(learning_rate).minimize(lossFun_crossEntropy)

tf.summary.scalar('cost',lossFun_crossEntropy)
tf.summary.scalar('accuracy',accuracy)
summary_op=tf.summary.merge_all()

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    logs_writer=tf.summary.FileWriter(logs_path,graph=tf.get_default_graph())
    for epoch in range(training_epochs):
        batch_count=int(len(trainData_in)/batch_size)
        for i in range(batch_count):
            batch_x=trainData_in[batch_size*i:batch_size*(i+1)]
            batch_y=trainData_out[batch_size*i:batch_size*(i+1)]
            _,summary=sess.run([train_step,summary_op],\
                               feed_dict={x_input:batch_x,\
                                          y_desired:batch_y})
            logs_writer.add_summary(summary,\
                                     epoch*batch_count+i)
            print('Epoch',epoch)
            print('Accuracy:',accuracy.eval\
                  (feed_dict={x_input:testData_in,
                              y_desired:testData_out}))
            print('Done')
            
    n=randint(0,len(testData_in))
    try_input=testData_in[n] 
    try_desired=testData_out[n]  
    print(try_desired)
    print(y_output.eval(feed_dict={x_input:[try_input]}))
    try_input.resize(28,28)
    plt.imshow(try_input,cmap='Greys_r')
    plt.show()
    
    saver=tf.train.Saver()
save_path=saver.save(sess,'c:/temp/saved_mnist_cnn')
print('Model saved to %s' % save_path)

Epoch 1

Accuracy: 0.9145

Done

[0. 0. 1. 0. 0. 0. 0. 0. 0. 0.]

[[8.5065767e-06 1.5296960e-02 9.3040824e-01 4.9656801e-02 7.9419806e-08

  2.0631231e-05 2.8203747e-03 4.7460157e-06 1.7745445e-03 9.0651583e-06]]

TensorFlow第三步 :单层网络-Mnist手写数字识别

三、在新的程序或者shell中,载入网络模型,并用test_data进行检验。

# coding=utf-8
import os  
os.environ["TF_CPP_MIN_LOG_LEVEL"]='2' # 只显示 warning 和 Error 

import pickle
import gzip
# Third-party libraries
import numpy as np

def load_data():
    f = gzip.open('../data/mnist.pkl.gz', 'rb')
    training_data, validation_data, test_data = pickle.load(f,encoding='bytes')
    f.close()
    return (training_data, validation_data, test_data)

def vectorized_result(j):
    e = np.zeros(10)
    e[j] = 1.0
    return e

import tensorflow as tf
import matplotlib.pyplot as plt

training_data, validation_data, test_data = load_data()
testData_in=test_data[0]
testData_out=[vectorized_result(j) for j in test_data[1]]

sess=tf.InteractiveSession()
new_saver=tf.train.import_meta_graph(r'c:/temp/saved_mnist_cnn/saved_mnist_cnn.ckp.meta')
new_saver.restore(sess, r'c:/temp/saved_mnist_cnn/saved_mnist_cnn.ckp')
tf.get_default_graph().as_graph_def()
x_input=sess.graph.get_tensor_by_name('x_input:0')
y_output=sess.graph.get_tensor_by_name('y_output:0')

try_input=testData_in[6] 
try_desired=testData_out[6]  
print(try_desired)
print(y_output.eval(feed_dict={x_input:[try_input]}))
try_input.resize(28,28)
plt.imshow(try_input,cmap='Greys_r')
plt.show()

[0. 0. 0. 0. 1. 0. 0. 0. 0. 0.]

[[7.1434711e-06 8.6740383e-06 3.3307720e-06 5.0231582e-04 9.4077229e-01

  2.3238571e-02 2.3933682e-04 3.0216873e-03 1.7731976e-02 1.4474570e-02]]

TensorFlow第三步 :单层网络-Mnist手写数字识别