MNIST神经网络python实现

import os
import struct
import numpy as np
import matplotlib.pyplot as plt
import gzip
import tempfile
import random
import tensorflow as tf
#*****************************************************************************************
#从文件中读入数据
def load_mnist(labels_path,images_path):
    """Load MNIST data from `path`"""
    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II',
                                 lbpath.read(8))
        labels = np.fromfile(lbpath,
                             dtype=np.uint8)
    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII',
                                               imgpath.read(16))
        images = np.fromfile(imgpath,
                             dtype=np.uint8).reshape(len(labels), 784)
    return images, labels
train_labels_path = 'F:\Dataset\MNIST\MNIST/train-labels.idx1-ubyte'
train_images_path = 'F:\Dataset\MNIST\MNIST/train-images.idx3-ubyte'
test_labels_path = 'F:\Dataset\MNIST\MNIST/t10k-labels.idx1-ubyte'
test_images_path = 'F:\Dataset\MNIST\MNIST/t10k-images.idx3-ubyte'
(train_images,train_labels)= load_mnist(train_labels_path,train_images_path)
(test_images,test_labels)= load_mnist(test_labels_path,test_images_path)
#************************************************************************************************
#对数据进行转化,使其符合格式
def transfer(labels,number):#对labels进行转化,从单个数字变成one-hot向量
    final_labels=[]
    for i in range(number):
        if(labels[i] == 0):
            label = [1.0,0,0,0,0,0,0,0,0,0]
        elif(labels[i] == 1):
            label = [0,1,0,0,0,0,0,0,0,0]
        elif(labels[i] == 2):
            label = [0,0,1,0,0,0,0,0,0,0]
        elif(labels[i] == 3):
            label = [0,0,0,1,0,0,0,0,0,0]
        elif(labels[i] == 4):
            label = [0,0,0,0,1,0,0,0,0,0]
        elif(labels[i] == 5):
            label = [0,0,0,0,0,1,0,0,0,0]
        elif(labels[i] == 6):
            label = [0,0,0,0,0,0,1,0,0,0]
        elif(labels[i] == 7):
            label = [0,0,0,0,0,0,0,1,0,0]
        elif(labels[i] == 8):
            label = [0,0,0,0,0,0,0,0,1,0]
        else:
            label = [0,0,0,0,0,0,0,0,0,1]
        final_labels.append(label)
    return final_labels
train_new_images = train_images.astype(dtype = np.float)/255
test_new_images = test_images.astype(dtype = np.float)/255
test_new_labels = np.array(transfer(test_labels,10000)).astype(dtype = np.float)
train_new_labels = np.array(transfer(train_labels,60000)).astype(dtype = np.float)

def next_train_batch(number):
    i = random.randrange(0,59899)
    batch_xs = train_new_images[i:i+number]
    batch_ys = train_new_labels[i:i+number]
    return batch_xs,batch_ys
def next_test_batch(number):
    i = random.randrange(0,9959)
    batch_xs = test_new_images[i:i+number]
    batch_ys = test_new_labels[i:i+number]
    return batch_xs,batch_ys

x = tf.placeholder("float", shape=[None, 784])
y_ = tf.placeholder("float", shape=[None, 10])

def weight_variable(shape):
  initial = tf.truncated_normal(shape, stddev=0.1)
  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')

def max_pool_2x2(x):
  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])
b_conv1 = bias_variable([32])
x_image = tf.reshape(x, [-1,28,28,1])
h_conv1 = tf.nn.relu(conv2d(x_image, W_conv1) + b_conv1)
h_pool1 = max_pool_2x2(h_conv1)

W_conv2 = weight_variable([5, 5, 32, 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([7 * 7 * 64, 1024])
b_fc1 = bias_variable([1024])
h_pool2_flat = tf.reshape(h_pool2, [-1, 7*7*64])
h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, W_fc1) + b_fc1)

keep_prob = tf.placeholder("float")
h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)

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)

cross_entropy = -tf.reduce_sum(y_*tf.log(y_conv))
train_step = tf.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float"))
sess = tf.Session()
sess.run(tf.initialize_all_variables())
for i in range(5000):
  batch_xs ,batch_ys = next_train_batch(50)
  if i%100 == 0:
    train_accuracy = sess.run(accuracy,feed_dict={x:batch_xs, y_: batch_ys, keep_prob: 1.0})
    print ("step %d, training accuracy %g"%(i, train_accuracy))
  sess.run(train_step,feed_dict={x: batch_xs, y_: batch_ys, keep_prob: 0.5})
print('train completed')
temp_accuracy = []
for i in range(300):
    test_batch_xs,test_batch_ys = next_test_batch(50)
    temp = sess.run(accuracy, feed_dict={x:test_batch_xs, y_: test_batch_ys,keep_prob: 1.0})
    temp_accuracy.append(temp)
sum = 0
#print(temp_accuracy)
b = len(temp_accuracy)
for i in temp_accuracy:
    sum += i 
print(sum/b)

你可能感兴趣的:(MNIST神经网络python实现)