TensorFlow学习(3)二次代价函数和交叉熵代价函数

二次代价函数(quadratic cost)

假如我们的目标是收敛到1,A点为0.82距离目标远,梯度比较大,权值调整比较大,B点为0.98距离目标近,梯度比较小,权值调整比较小,调整方案合理

假如我们的目标是收料到0,A点为0.82距离目标近,梯度比较大,权值调整比较大,B点为0.98距离目标远,梯度比较小,权值调整比较小,调整方案不合理

交叉熵代价函数(cross-entropy)

对数释然代价函数(log-likelihood cost)

在 tensorflow 中用:

  • tf.nn.sigmoid_cross_entropy_with_logits() 来表示 sigmoid 与交叉熵搭配使用
  • tf.nn.softmax_cross_entropy_with_logits() 来表示 softmax 与交叉熵搭配使用

实际使用

loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))
import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
# 载入数据集
mnist = input_data.read_data_sets('MNIST_data',one_hot=True)
# 不是一张张图片放入神经网络,定义一个批次,一次 100
batch_size = 100
# 计算一个有多少批次,整除
n_batch = mnist.train.num_examples // batch_size

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))
# 交叉熵代价函数与 softmax 配合使用
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y, logits=prediction))

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

init = tf.global_variables_initializer()

# 结果存放在布尔型列表中
# tf.equal 相等返回 True,否则 False,argmax 比较 y 中哪个元素的值为 1,返回该元素下标
correct_predition = tf.equal(tf.argmax(y, 1), tf.argmax(prediction, 1))
# 求准确率
# tf.cast 将布尔型转换为32位浮点型,True -> 1.0,False -> 0.0,然后求平均值,如有 9 个 1,1 个 0,平均值为 0.9,准确率为 0.9
accuracy = tf.reduce_mean(tf.cast(correct_predition, tf.float32))

with tf.Session() as sess:
    sess.run(init)
    # 循环 21 个周期,每个周期批次为 100,每个周期将所有图片都训练一次
    for epoch in range(20):
        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))

从结果看使用交叉熵函数明显增加收敛速度

猜你喜欢

转载自blog.csdn.net/HAIYUANBOY/article/details/89841903