Tensorflow— MNIST数据集分类简单版本

代码:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data


#载入数据集
#当前路径
mnist = input_data.read_data_sets("MNISt_data", one_hot=True)

运行结果:

Extracting MNISt_data/train-images-idx3-ubyte.gz
Extracting MNISt_data/train-labels-idx1-ubyte.gz
Extracting MNISt_data/t10k-images-idx3-ubyte.gz
Extracting MNISt_data/t10k-labels-idx1-ubyte.gz

代码:

#每个批次的大小
#以矩阵的形式放进去
batch_size = 100
#计算一共有多少个批次
n_batch = mnist.train.num_examples // batch_size


#定义两个placeholder
#28 x 28 = 784
x = tf.placeholder(tf.float32, [None, 784])
y = tf.placeholder(tf.float32, [None, 10])


#创建一个简单的神经网络
#输入层784,没有隐藏层,输出层10个神经元
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([1, 10]))
prediction = tf.nn.softmax(tf.matmul(x, W) + b)


#二次代价函数
loss = tf.reduce_mean(tf.square(y - prediction))


#使用梯度下降法
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss)

#初始化变量
init = tf.global_variables_initializer()



#结果存放在一个布尔型列表中
#tf.argmax(y, 1)与tf.argmax(prediction, 1)相同返回True,不同则返回False
#argmax返回一维张量中最大的值所在的位置
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))

#求准确率
#tf.cast(correct_prediction, tf.float32) 将布尔型转换为浮点型
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))


with tf.Session() as sess:
    sess.run(init)
    #总共21个周期
    for epoch in range(21):
        #总共n_batch个批次
        for batch in range(n_batch):
            #获得一个批次
            batch_xs, batch_ys = mnist.train.next_batch(batch_size)
            sess.run(train_step, feed_dict={x:batch_xs, y:batch_ys})
        
        #训练完一个周期后准确率
        acc = sess.run(accuracy, feed_dict={x:mnist.test.images, y:mnist.test.labels})
        print("Iter" + str(epoch) + ", Testing Accuracy" + str(acc))

运行结果:

Iter0, Testing Accuracy0.8331
Iter1, Testing Accuracy0.8715
Iter2, Testing Accuracy0.8811
Iter3, Testing Accuracy0.8885
Iter4, Testing Accuracy0.8938
Iter5, Testing Accuracy0.8967
Iter6, Testing Accuracy0.9005
Iter7, Testing Accuracy0.9022
Iter8, Testing Accuracy0.9043
Iter9, Testing Accuracy0.9048
Iter10, Testing Accuracy0.9062
Iter11, Testing Accuracy0.907
Iter12, Testing Accuracy0.908
Iter13, Testing Accuracy0.9088
Iter14, Testing Accuracy0.9099
Iter15, Testing Accuracy0.9113
Iter16, Testing Accuracy0.911
Iter17, Testing Accuracy0.9124
Iter18, Testing Accuracy0.9131
Iter19, Testing Accuracy0.914
Iter20, Testing Accuracy0.9136

猜你喜欢

转载自blog.csdn.net/wangsiji_buaa/article/details/80202424