神经网络的构建:
没有使用中间层,将一张图片[24,24]的矩阵784个点,对应为
[0,0,...,1,0...] (只有一个位置为1,表示该下标为对应的图片数字)的10个点
神经网络可优化参数为,784x10 的权重,和10个偏置值
prediction = tf.nn.softmax(tf.matmul(x,w)+b)
prediction 为一个一维数组,里面为识别该图片为每个数字的概率
#-*-coding:utf-8-*-
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
#载入数据集
minist = input_data.read_data_sets('MNIST_data',one_hot=True)
#每个批次的大小
batch_size = 100
#一共有多少批次
n_batch = minist.train.num_examples//batch_size
#定义两个placeholder
#当用训练集定义每批次100个时,x为[100,784]的矩阵,y为[100,10]的矩阵
x = tf.placeholder(tf.float32,[None,784])
y = tf.placeholder(tf.float32,[None,10])
#创建一个简单的神经网络
w = tf.Variable(tf.zeros([784,10]))
b = tf.Variable(tf.zeros([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 返回一维张量中最大值所在的位置
correct_prediction = tf.equal(tf.argmax(y,1),tf.argmax(prediction,1))
#求准确率
accuracy = tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
with tf.Session() as sess:
sess.run(init)
for epoch in range(21):
for batch in range(n_batch):
#训练是用的训练集,按照分批次来做
batch_xs,batch_ys = minist.train.next_batch(batch_size)
sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})
#此处的准确率是针对现有的参数,对于所有的测试集(测试图片,测试标签)来计算准确率
acc = sess.run(accuracy,feed_dict={x:minist.test.images,y:minist.test.labels})
print('iterator: '+str(epoch)+' accuracy: '+str(acc))
改进方法:
代价函数和激活函数相互结合,对调节速度产生影响。
使用交叉熵代价函数,softmax()激活函数:
#使用交叉熵代价函数
#loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y,logits=prediction))
运行结果为: