Tensorflow—交叉熵

本文介绍了一个基于TensorFlow的手写数字识别模型。通过加载MNIST数据集,并使用简单的神经网络进行训练,最终实现了较高的识别准确率。文章详细展示了代码实现过程及训练效果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码:

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.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.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.8505
Iter1, Testing Accuracy0.895
Iter2, Testing Accuracy0.9028
Iter3, Testing Accuracy0.9061
Iter4, Testing Accuracy0.9084
Iter5, Testing Accuracy0.9101
Iter6, Testing Accuracy0.9114
Iter7, Testing Accuracy0.914
Iter8, Testing Accuracy0.9145
Iter9, Testing Accuracy0.916
Iter10, Testing Accuracy0.9178
Iter11, Testing Accuracy0.918
Iter12, Testing Accuracy0.919
Iter13, Testing Accuracy0.9188
Iter14, Testing Accuracy0.9198
Iter15, Testing Accuracy0.9199
Iter16, Testing Accuracy0.9212
Iter17, Testing Accuracy0.9213
Iter18, Testing Accuracy0.9212
Iter19, Testing Accuracy0.9211
Iter20, Testing Accuracy0.922
注: 交叉熵能够快速收敛
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值