TF之GD:基于tensorflow框架搭建GD算法利用Fashion-MNIST数据集实现多分类预测(92%)

本文介绍了一种基于TensorFlow框架的梯度下降(GD)算法,该算法应用于Fashion-MNIST数据集进行多分类预测,实现了92%的预测精度。通过详细展示代码实现过程,包括数据下载、预处理、模型搭建、训练及评估,为读者提供了一个从理论到实践的完整案例。

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

TF之GD:基于tensorflow框架搭建GD算法利用Fashion-MNIST数据集实现多分类预测(92%)

 

 

 

目录

输出结果

实现代码


 

 

 

输出结果

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.
Extracting data/fashion\train-images-idx3-ubyte.gz
Successfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.
Extracting data/fashion\train-labels-idx1-ubyte.gz
Successfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.
Extracting data/fashion\t10k-images-idx3-ubyte.gz
Successfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.
Extracting data/fashion\t10k-labels-idx1-ubyte.gz

(55000, 784)
(55000, 10)
Epoch: 0,acc: 0.7965
Epoch: 1,acc: 0.8118
Epoch: 2,acc: 0.8743
Epoch: 3,acc: 0.8997
Epoch: 4,acc: 0.9058
Epoch: 5,acc: 0.9083
Epoch: 6,acc: 0.9102
Epoch: 7,acc: 0.9117
Epoch: 8,acc: 0.9137
Epoch: 9,acc: 0.9147
Epoch: 10,acc: 0.9158
Epoch: 11,acc: 0.9166
Epoch: 12,acc: 0.9186
Epoch: 13,acc: 0.9191
Epoch: 14,acc: 0.9187
Epoch: 15,acc: 0.9195
Epoch: 16,acc: 0.9206
Epoch: 17,acc: 0.9207
Epoch: 18,acc: 0.9216
Epoch: 19,acc: 0.9215
Epoch: 20,acc: 0.9218

 

实现代码



#TF之GD:基于tensorflow框架搭建GD算法利用Fashion-MNIST数据集实现多分类预测(92%)
import  tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

fashion = input_data.read_data_sets('data/fashion', one_hot=True)

print(fashion.train.images.shape)
print(fashion.train.labels.shape)

batch_size = 100
batch_num = fashion.train.num_examples // batch_size

#定义X,Y参数
x = tf.placeholder(tf.float32, shape=[None, 784])
y = tf.placeholder(tf.float32, shape=[None, 10])
#定义W,B参数
W = tf.Variable(tf.truncated_normal([784, 10], stddev= 0.1))
b = tf.Variable(tf.zeros([10]) + 0.1)

#预测结果
prediction = tf.nn.softmax(tf.matmul(x, W) + b)
#使用交叉熵计算loss
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=prediction, labels=y))
#定义优化器
train_step = tf.train.GradientDescentOptimizer(0.2).minimize(cross_entropy)
#判断预测结果是否正确
correct_prediction = tf.equal(tf.argmax(prediction, 1), tf.argmax(y, 1))
#计算准确率,将bool值转为float32
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for epoch in range(21):
        for i in range(batch_num):
            batch_xs, batch_ys = fashion.train.next_batch(batch_size)
            sess.run(train_step, feed_dict={x: batch_xs, y:batch_ys})
        acc = sess.run(accuracy, feed_dict={x:fashion.test.images, y:fashion.test.labels})
        print('Epoch: '+str(epoch)+',acc: '+str(acc))





 

 

 

 

 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一个处女座的程序猿

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值