在此基础上加上了自己(粘贴)的笔记。
MNIST 数据
首先准备数据(MNIST库)
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# number 1 to 10 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
数据中包含55000张训练图片,每张图片的分辨率是28×28,所以我们的训练网络输入应该是28×28=784个像素数据。
搭建网络
def add_layer(inputs, in_size, out_size, activation_function=None,):
# add one more layer and return the output of this layer
Weights = tf.Variable(tf.random_normal([in_size, out_size]))
biases = tf.Variable(tf.zeros([1, out_size]) + 0.1,)
Wx_plus_b = tf.matmul(inputs, Weights) + biases
if activation_function is None:
outputs = Wx_plus_b
else:
outputs = activation_function(Wx_plus_b,)
return outputs
def compute_accuracy(v_xs, v_ys):
global prediction
y_pre = sess.run(prediction, feed_dict={xs: v_xs})
correct_prediction = tf.equal(tf.argmax(y_pre,1), tf.argmax(v_ys,1)) # tf.equal返回tf.bool
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # tf.cast 转换数据类型的
result = sess.run(accuracy, feed_dict={xs: v_xs, ys: v_ys}) #result 这一步不加ys:v_ys也是可以的,在result的计算中没有用到ys
return result
# define placeholder for inputs to network
xs = tf.placeholder(tf.float32, [None, 784]) # 28x28
[None, 784]
:
不规定有多少sample,每一个sample的大小是784
每张图片都表示一个数字,所以我们的输出是数字0到9,共10类。
ys = tf.placeholder(tf.float32, [None, 10])
调用add_layer函数搭建一个最简单的训练网络结构,只有输入层和输出层。
# add output layer
prediction = add_layer(xs, 784, 10, activation_function=tf.nn.softmax)
WARNING:tensorflow:From C:\Anaconda\lib\site-packages\tensorflow\python\framework\op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.
Instructions for updating:
Colocations handled automatically by placer.
其中输入数据是784个特征,输出数据是10个特征,激励采用softmax函数
Cross entropy loss
loss函数(即最优化目标函数)选用交叉熵函数。交叉熵用来衡量预测值和真实值的相似程度,如果完全相同,它们的交叉熵等于零。
# the error between prediction and real data
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),
reduction_indices=[1])) # loss
train方法(最优化算法)采用梯度下降法。
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.Session()
# tf.initialize_all_variables() 这种写法马上就要被废弃
# 替换成下面的写法:
sess.run(tf.global_variables_initializer())
WARNING:tensorflow:From C:\Anaconda\lib\site-packages\tensorflow\python\ops\math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.
Instructions for updating:
Use tf.cast instead.
训练
现在开始train,每次只取100张图片,免得数据太多训练太慢。
for i in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100) #train data
sess.run(train_step, feed_dict={xs: batch_xs, ys: batch_ys})
if i % 50 == 0:
print(compute_accuracy(
mnist.test.images, mnist.test.labels)) #test data
0.0618
0.6388
0.7329
0.7789
0.8001
0.8204
0.8284
0.8399
0.8478
0.8515
0.8522
0.8607
0.859
0.8655
0.8691
0.8706
0.8714
0.8736
0.8759
0.8763
每训练50次输出一下预测精度
把最前边的代码当中的bias 定义当中的 -0.1 去掉,最终的准确度就可以达到0.98
网友评论