先放代码
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 = 'C:/Users/cxt66/Desktop/MNISTdata/MNISTdata/train-labels.idx1-ubyte'
train_images_path = 'C:/Users/cxt66/Desktop/MNISTdata/MNISTdata/train-images.idx3-ubyte'
test_labels_path = 'C:/Users/cxt66/Desktop/MNISTdata/MNISTdata/t10k-labels.idx1-ubyte'
test_images_path = 'C:/Users/cxt66/Desktop/MNISTdata/MNISTdata/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_labelstrain_new_images = train_images.astype(dtype = np.float)/255
test_new_images = test_images.astype(dtype = np.float)/255test_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_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_ysx = 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))train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)
init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)for i in range(10000):
batch_xs,batch_ys = next_batch(100)
print('loss',sess.run(cross_entropy,feed_dict = {x: batch_xs,y_: batch_ys}))
sess.run(train_step,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_new_images, y_: test_new_labels}))
这个是在极客学院的入门代码上改的。
因为没法直接下载到解数据的函数,只能自己写。
可以从网上找到压缩包,解压之后是这样的
四个ubyte文件
然后用第一段的函数读进来,再用第二段的函数转成标准数据流。
第三段是核心的训练过程,第四段是用测试集来进行检验。
原文地址:
http://wiki.jikexueyuan.com/project/tensorflow-zh/tutorials/mnist_beginners.html
作为刚转专业过来,还没入门的小菜鸟,表示写一写很头痛,又很开心,遇到好多问题,慢慢的都解决掉了,希望以后能再接再厉。