1、上一篇在介绍tensorflow常用函数中,利用tensorflow库函数,实现了Softmax(手写字体分类)。在另外一片中也介绍了用纯手写实现Maxsoft(3种类型点分类)
2、在手写字体分类时,在数据读入(专门针对Mnist数据集)有很强大的函数,不仅能够读入数据,而且还能够传入数据时随机传入固定的数据量(减少训练时间)。
3、自己手写的读入数据函数
区别:
①库函数可以实现每次随机固定量的训练量
②手写的数据读入,只能全部填入充当训练数据
③训练数据很大,很耗时,并且数据量很大,学习率就需要很小,训练更耗时
④结论:库函数好使(但是该库函数只能针对该训练集Mnist)
#coding:utf-8 import tensorflow as tf import numpy as np import struct import matplotlib.pyplot as plt #读入数据,有一个库函数已经实现好了 #这里我们自己读取idx3-ubyte和idx1-ubyte文件(数据量很大,训练的很慢) def Load_data(path): with open(path,'rb') as op: data=op.read() index=0 magic_number, num_images, num_rows, num_cols=struct.unpack_from('>iiii',data,index) image_size = num_rows * num_cols index += struct.calcsize('>iiii') fmt_image = '>' + str(image_size) + 'B' images = np.empty((num_images, num_rows, num_cols)) for i in range(num_images): #print('已解析 %d' % (i + 1) + '张') #plt.imshow(images[i-1],cmap=plt.cm.gray) #plt.show() images[i] = np.array(struct.unpack_from(fmt_image, data, index)).reshape((num_rows, num_cols)) index += struct.calcsize(fmt_image) return images def Load_label(path): with open(path,'rb') as op: data=op.read() index=0 magic_number, num_images=struct.unpack_from('>ii',data,index) image_size = 1 index += struct.calcsize('>ii') fmt_image = '>' + str(image_size) + 'B' images = np.empty((num_images, 10)) for i in range(num_images): num=struct.unpack_from(fmt_image, data, index) images[i][num] = 1 index += struct.calcsize(fmt_image) return images path="C:\\Users\\Administrator\\Desktop\\MNIST\\" Train_path = path + "train-images.idx3-ubyte" Train_label = path + "train-labels.idx1-ubyte" Test_path = path + "t10k-images.idx3-ubyte" Test_label = path + "t10k-labels.idx1-ubyte" train_X=Load_data(Train_path).reshape(60000,-1) train_Y=Load_label(Train_label) test_X=Load_data(Test_path).reshape(10000,-1) test_Y=Load_label(Test_label) print(train_X.shape) print(train_Y.shape) print(test_X.shape) print(test_Y.shape) 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]) #代价函数 y = tf.clip_by_value(y, 1e-10, 1.0) cross_entropy = -tf.reduce_sum(y_*tf.log(y)) #迭代 train_step = tf.train.GradientDescentOptimizer(0.00002).minimize(cross_entropy) init = tf.initialize_all_variables() sess = tf.Session() sess.run(init) for i in range(1): batch_xs, batch_ys =train_X,train_Y sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys}) if i%100==0: print(sess.run(cross_entropy, feed_dict={x: batch_xs, y_: batch_ys})) #输出 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_X, y_: test_Y})) print (sess.run(accuracy, feed_dict={x: train_X, y_: train_Y}))