引用部分代码:https://blog.csdn.net/simple_the_best/article/details/75267863
# -*- coding: utf-8 -*- import tensorflow as tf import os import struct import numpy as np import matplotlib.pyplot as plt #数据处理 """ 60000行的训练数据集(mnist.train)和10000行的测试数据集(mnist.test)。 一张包含手写数字的图片mnist.train.images 和一个对应的标签 mnist.train.labels。 mnist.train.images[60000, 784] mnist.train.images[1至60000张的图片索引, 每张图片中的像素点] 每一张图片包含28X28个像素点,总共784个像素点, 文件 = 'train-images.idx3-ubyte' mnist.train.labels[60000, 10] mnist.train.labels[1至60000张的图片索引, 每张图片中表示的数字] 数字n将表示成一个只有在第n维度(从0开始)数字为1的10维向量。 比如,标签0将表示成([1,0,0,0,0,0,0,0,0,0,0])。 文件 = train-labels.idx1-ubyte """ train_images_path =r'D:\MNIST_data\train-images.idx3-ubyte' train_labels_path =r'D:\MNIST_data\train-labels.idx1-ubyte' train_images=[] #=mnist.train.images train_labels=[] #=mnist.train.labels """Load MNIST data from `path`""" with open(train_labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II',lbpath.read(8)) train_labels = np.fromfile(lbpath,dtype=np.uint8) with open(train_images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16)) train_images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(train_labels), 784) #train_labels 修改形状和值(=1) new_train_labels=np.zeros((60000, 10),np.float32) i=0 for item in train_labels: new_train_labels[i][item]=1.0 i=i+1 train_labels=new_train_labels #验证形状 print('train_images.shape =>' +str(train_images.shape)) print('train_labels.shape =>' +str(train_labels.shape)) print(train_labels[0:10]) x = tf.placeholder(tf.float32, [None, 784]) W = tf.Variable(tf.zeros([784,10])) b = tf.Variable(tf.zeros([10])) y = tf.nn.softmax(tf.matmul(x,W) + b) y_ = tf.placeholder("float", [None,10]) #计算交叉熵 cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #计算交叉熵 #以0.01的学习速率最小化交叉熵 train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy) #初始化我们创建的变量 init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) #每批训练数量 banch_size=64 #训练 for i in range(1000): batch_xs=train_images[i*banch_size:((i+1)*banch_size)] batch_ys=train_labels[i*banch_size:((i+1)*banch_size)] sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) #下面验证测试部分 test_images_path =r'D:\MNIST_data\t10k-images.idx3-ubyte' test_labels_path =r'D:\MNIST_data\t10k-labels.idx1-ubyte' test_images=[] #=mnist.train.images test_labels=[] #=mnist.train.labels """Load MNIST data from `path`""" with open(test_labels_path, 'rb') as lbpath: magic, n = struct.unpack('>II',lbpath.read(8)) test_labels = np.fromfile(lbpath,dtype=np.uint8) with open(test_images_path, 'rb') as imgpath: magic, num, rows, cols = struct.unpack('>IIII',imgpath.read(16)) test_images = np.fromfile(imgpath,dtype=np.uint8).reshape(len(test_labels), 784) #test_labels 修改形状和值(=1) new_test_labels=np.zeros((10000, 10),np.float32) i=0 for item in test_labels: new_test_labels[i][item]=1.0 i=i+1 test_labels=new_test_labels #验证形状 print('test_images.shape =>' +str(test_images.shape)) print('test_labels.shape =>' +str(test_labels.shape)) correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, "float")) print(sess.run(accuracy, feed_dict={x: test_images, y_: test_labels}))
#显示0至24索引的图片 fig, ax = plt.subplots( nrows=5, ncols=5, sharex=True, sharey=True, ) ax = ax.flatten() for i in range(25): img = train_images[i].reshape(28, 28) ax[i].imshow(img, cmap='Greys', interpolation='nearest') ax[0].set_xticks([]) ax[0].set_yticks([]) plt.tight_layout() plt.show()