1000字范文,内容丰富有趣,学习的好帮手!
1000字范文 > TF之GD:基于tensorflow框架搭建GD算法利用Fashion-MNIST数据集实现多分类预测(92%)

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

时间:2024-05-31 19:42:29

相关推荐

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

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

目录

输出结果

实现代码

输出结果

Successfully downloaded train-images-idx3-ubyte.gz 9912422 bytes.Extracting data/fashion\train-images-idx3-ubyte.gzSuccessfully downloaded train-labels-idx1-ubyte.gz 28881 bytes.Extracting data/fashion\train-labels-idx1-ubyte.gzSuccessfully downloaded t10k-images-idx3-ubyte.gz 1648877 bytes.Extracting data/fashion\t10k-images-idx3-ubyte.gzSuccessfully downloaded t10k-labels-idx1-ubyte.gz 4542 bytes.Extracting data/fashion\t10k-labels-idx1-ubyte.gz(55000, 784)(55000, 10)Epoch: 0,acc: 0.7965Epoch: 1,acc: 0.8118Epoch: 2,acc: 0.8743Epoch: 3,acc: 0.8997Epoch: 4,acc: 0.9058Epoch: 5,acc: 0.9083Epoch: 6,acc: 0.9102Epoch: 7,acc: 0.9117Epoch: 8,acc: 0.9137Epoch: 9,acc: 0.9147Epoch: 10,acc: 0.9158Epoch: 11,acc: 0.9166Epoch: 12,acc: 0.9186Epoch: 13,acc: 0.9191Epoch: 14,acc: 0.9187Epoch: 15,acc: 0.9195Epoch: 16,acc: 0.9206Epoch: 17,acc: 0.9207Epoch: 18,acc: 0.9216Epoch: 19,acc: 0.9215Epoch: 20,acc: 0.9218

实现代码

#TF之GD:基于tensorflow框架搭建GD算法利用Fashion-MNIST数据集实现多分类预测(92%)import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datafashion = input_data.read_data_sets('data/fashion', one_hot=True)print(fashion.train.images.shape)print(fashion.train.labels.shape)batch_size = 100batch_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)#使用交叉熵计算losscross_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值转为float32accuracy = 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))

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。