import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data # 载入数据 mnist = input_data.read_data_sets('F:\Pycharm projection\MNIST_data', one_hot=True) # 每个批次的大小 batch_size = 100 # 计算一共有多少批次 n_batch = mnist.train.num_examples // batch_size # 定义两个placehoder x = tf.placeholder(tf.float32, [None, 784]) # 行与批次有关,一会儿计算之后传入,列是一张是28*28像素 y = tf.placeholder(tf.float32, [None, 10]) # y 是标签0-9包括10类 # 创建一个简单的神经网络 """ 只用到输入层和输出层,不加隐藏层 输入层:784个神经元 输出层:10个标签,就10个神经元 """ w = tf.Variable(tf.zeros([784,10])) # 权值 b = tf.Variable(tf.zeros([10])) # 偏置值 prediction = tf.nn.softmax(tf.matmul(x, w)+b) # 预测值,使用sofrmax函数 # 二次代价函数 loss = tf.reduce_mean(tf.square(y-prediction)) # 定义一个梯度下降发来训练优化器 train_step = tf.train.GradientDescentOptimizer(0.2).minimize(loss) # 给出学习率0.2 # 初始化变量 init =tf.global_variables_initializer() """ 模型训练好之后,要测试下,模型的准确率 定义求准确率的方法 """ # 结果存在一个布尔型列表中 # argmax返回一维张量中最大值所在的位置 correct_prediction = tf.equal(tf.argmax(y, 1),tf.argmax(prediction, 1)) # 求准确率 accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # cast作用把布尔型转成32位的float with tf.Session() as sess: sess.run(init) for epoch in range(21): for batch in range(n_batch): # 获得一个批次(100张图片),它的大小100;图片数据保存在 :batch_xs,标签保存在:batch_ys 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.8307Iter1 Testing accuracy0.8708
Iter2 Testing accuracy0.881
Iter3 Testing accuracy0.889
Iter4 Testing accuracy0.8943
Iter5 Testing accuracy0.898
Iter6 Testing accuracy0.9004
Iter7 Testing accuracy0.9024
Iter8 Testing accuracy0.9028
Iter9 Testing accuracy0.9043
Iter10 Testing accuracy0.9068
Iter11 Testing accuracy0.9074
Iter12 Testing accuracy0.9088
Iter13 Testing accuracy0.909
Iter14 Testing accuracy0.9099
Iter15 Testing accuracy0.9111
Iter16 Testing accuracy0.912
Iter17 Testing accuracy0.9123
Iter18 Testing accuracy0.9128
Iter19 Testing accuracy0.9139
Iter20 Testing accuracy0.913
使用交叉熵运行结果:代码部分只需更改loss二次代价函数为释然代价函数,其余不用改。
# 对数释然代价函数(交叉熵) loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))Iter0 Testing accuracy0.8242
Iter0 Testing accuracy0.8242
Iter1 Testing accuracy0.8823
Iter2 Testing accuracy0.8989
Iter3 Testing accuracy0.9043Iter4 Testing accuracy0.9079
Iter5 Testing accuracy0.9096
Iter6 Testing accuracy0.911
Iter7 Testing accuracy0.9129
Iter8 Testing accuracy0.9147
Iter9 Testing accuracy0.9164
Iter10 Testing accuracy0.9164
Iter11 Testing accuracy0.9175
Iter12 Testing accuracy0.9187
Iter13 Testing accuracy0.9196
Iter14 Testing accuracy0.9188
Iter15 Testing accuracy0.9203
Iter16 Testing accuracy0.9196
Iter17 Testing accuracy0.9208
Iter18 Testing accuracy0.9216
Iter19 Testing accuracy0.9211
Iter20 Testing accuracy0.9206
总结:使用S型曲线或者softmax时,使用交叉熵代价函数
如果输出神经元是线性的,那么二次代价函数就是一种合适的选择。
有助于模型快速收敛,可以节省训练模型的时间。