TensorFlow MNIST (Softmax)
来源:互联网 发布:项目管理 书籍 知乎 编辑:程序博客网 时间:2024/06/11 20:17
代码
import tensorflow as tfimport numpy as npfrom tensorflow.examples.tutorials.mnist import input_datasess = tf.InteractiveSession()#远程下载MNIST数据,建议先下载好并保存在MNIST_data目录下def DownloadData(): mnist = input_data.read_data_sets("MNIST_data/", one_hot=True) #编码格式:one-hot print(mnist.train.images.shape, mnist.train.labels.shape) #训练数据55000条 print(mnist.test.images.shape, mnist.test.labels.shape) #测试数据10000条 print(mnist.validation.images.shape, mnist.validation.labels.shape) #验证数据5000条 return mnistdef Train(mnist): #Step1 定义算法公式Softmax Regression x = tf.placeholder(tf.float32, shape=(None, 784)) #构建占位符,代表输入的图像,维度None表示样本的数量可以是任意的,维度784代表特征向量,由图像大小28*28转换而来 weights = tf.Variable(tf.zeros([784, 10])) #构建一个变量,代表训练目标weights,初始化为0 biases = tf.Variable(tf.zeros([10])) #构建一个变量,代表训练目标biases,初始化为0 y = tf.nn.softmax(tf.matmul(x, weights) + biases) #构建了一个softmax的模型:y = softmax(Wx + b),y指样本的预测值 y_ = tf.placeholder(tf.float32, [None, 10]) # 构建占位符,代表样本标签的真实值 #Step2 定义损失函数,选定优化器,并指定优化器优化损失函数 # 定义交叉熵损失函数 cross_entropy = tf.reduce_mean( -tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]) ) # 使用梯度下降算法(学习率为0.5)来最小化这个交叉熵损失函数 train = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #Step3 训练数据 tf.global_variables_initializer().run() for i in range(2000): #迭代次数 batch_x, batch_y = mnist.train.next_batch(100) #一个batch的大小为100, 使用一个batch的数据来训练模型 sess.run(train, feed_dict={x: batch_x, y_: batch_y}) #用训练数据替代占位符来执行训练 if (i % 100 == 0): print("i=%d, 准确率=%g" % (i, Test(mnist, weights, biases)) ) print("准确率=%g" % Test(mnist, weights, biases))# 在测试数据上对准确率进行评测def Test(mnist, weights, biases): x = tf.placeholder(tf.float32, [None, 784]) #测试数据 y = tf.nn.softmax(tf.matmul(x, weights) + biases) #预测值 y_ = tf.placeholder(tf.float32, [None, 10]) #真实值 # tf.argmax()返回的是某一维度上其数据最大所在的索引值,在这里即代表预测值和真实值 # 判断预测值y和真实值y_中最大数的索引是否一致,y的值为1-10概率 correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1)) accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32)) # 用平均值来统计测试准确率 return sess.run(accuracy, feed_dict={x: mnist.test.images, y_: mnist.test.labels}) # 打印测试信息def main(): mnist = DownloadData() Train(mnist)if __name__ == '__main__': main()
0 0
- TensorFlow MNIST (Softmax)
- Tensorflow 01: mnist-softmax
- TensorFlow入门-MNIST & softmax regression
- 【TensorFlow】MNIST(使用softmax)
- tensorflow 使用softmax 分类mnist数据库
- 基于TensorFLow实现MNIST和softmax回归
- Tensorflow实现Softmax Regression 手写识别MNIST
- Tensorflow学习笔记(3)-mnist(softmax regression)
- TensorFlow的softmax regression做mnist例子
- TensorFlow学习笔记(2)----Softmax Regression分类MNIST
- Logistic回归、softmax回归以及tensorflow实现MNIST识别
- TensorFlow教程02:MNIST实验——Softmax回归
- tensorflow基于softmax模型实现手写体MNIST数字集识别
- Tensorflow简单复现softmax模型对于mnist数据集分类
- TensorFlow在MNIST中的应用-Softmax回归分类
- tensorflow实现softmax回归(softmax regression)——简单的MNIST识别(第一课)
- TensorFlow Softmax
- Softmax Regression with MNIST
- PAT 1027. Colors in Mars (20)
- 位(bits)和整数(ints)的二进制表示
- 手机浏览器上,给body增加overflow:hidden;width:100%;height:100% 无效的问题
- jvm命令工具jps
- 20170313ubuntu中apache设置
- TensorFlow MNIST (Softmax)
- QT下的桌面程序启动页
- 因DataTable的字段值为DBNull引发的异常(it.属性和it["属性"]的区别
- CSU-ACM 1003: UC Browser
- git 几个常用到命令,时不时的在追加一些。
- python 文件和流
- Android Studio开发时多工程引用相同Library项目的配置方法
- fig4.16
- Android开发之解决ListView和ScrollView滑动冲突的问题