代码

import tensorflow as tf
import numpy as np
from 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-hotprint(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 Regressionx = tf.placeholder(tf.float32, shape=(None, 784))            #构建占位符,代表输入的图像,维度None表示样本的数量可以是任意的,维度784代表特征向量,由图像大小28*28转换而来weights = tf.Variable(tf.zeros([784, 10]))                    #构建一个变量,代表训练目标weights,初始化为0biases = tf.Variable(tf.zeros([10]))                        #构建一个变量,代表训练目标biases,初始化为0y = 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()

TensorFlow MNIST (Softmax)相关推荐

  1. 逻辑回归实现多分类任务(python+TensorFlow+mnist)

    逻辑回归实现多分类任务(python+TensorFlow+mnist) 逻辑回归是统计学中的一种经典方法,虽然叫回归,但在机器学习领域,逻辑回归通常情况下当成一个分类任务,softmax就是由其演变 ...

  2. TensorFlow实现Softmax

    TensorFlow实现Softmax Regression识别手写数字 本文是按照黄文坚.唐源所著的<TensorFlow实战>一书,进行编写.在TensorFlow实战之余,力求简洁地 ...

  3. tensorflow mnist 1

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data import keras.back ...

  4. Tensorflow mnist 数据集测试代码 + 自己下载数据

    https://blog.csdn.net/weixin_39673686/article/details/81068582 import tensorflow as tf from tensorfl ...

  5. Tensorflow— MNIST数据集分类简单版本

    代码: import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data#载入数据集 #当前路径 m ...

  6. TensorFlow MNIST初级学习

    2019独角兽企业重金招聘Python工程师标准>>> MNIST MNIST 是一个入门级计算机视觉数据集,包含了很多手写数字图片,如图所示: 数据集中包含了图片和对应的标注,在 ...

  7. TensorFlow MNIST TensorBoard版本

    代码 import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_ ...

  8. TensorFlow MNIST CNN LeNet5模型

    代码 import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_datamnist = input_d ...

  9. TensorFlow MNIST 入门 代码

    其实就是按照TensorFlow中文教程的内容一步步跟着敲的代码. 不过在运行代码的时候遇到代码中加载不到MNIST数据资源,似乎是被墙了((⊙﹏⊙)b) 于是自己手动下载了数据包,放到 MNIST_ ...

最新文章

  1. UWP 图片剪切旋转工具
  2. 使用SAP Cloud for Customer Product OData服务读取产品主数据和其图片信息
  3. 程序员求职之道(《程序员面试笔试宝典》)之学业与求职,孰轻孰重?
  4. 51nod 1250 排列与交换——dp
  5. LeetCode 750. 角矩形的数量(DP)
  6. Alpha冲刺阶段博客汇总
  7. 盘点 8 种最坑的 SQL 错误用法
  8. C++程序员Protocol Buffers基础指南
  9. 基于ggplot2网络可视化(二)
  10. 【水果识别】基于matalb GUI水果分类系统【含Matlab源码 174期】
  11. 概率图模型(PGM)学习笔记(一)动机与概述
  12. Centos7.X安装mariadb及卸载mariadb安装mysql方法
  13. lorawan 科普文
  14. gpu运行pycharm
  15. 使用python(turtle)画小猪佩奇
  16. 五部搞定Android开发环境部署——费UC噶不过详细的Android开发环境搭建教程
  17. 单位转换html代码,JavaScript实现长度单位转换
  18. 计算机网络部分复习题
  19. Java学习笔记整理-知识梳理+JDK1.8详细文档
  20. bulls与cows的c语言程序设计,关于cows与bulls的跨学科观点:cow(母牛)

热门文章

  1. PHP客户端缓存控制
  2. Win8 官方培训课程
  3. msconfig深解
  4. 洛谷 - P3321 [SDOI2015]序列统计(原根+NTT)
  5. ZOJ - 2972 Hurdles of 110m(记忆化搜索/动态规划)
  6. Linux中高斯分布的参数设置,华为openGauss 配置操作系统参数
  7. IP地址的三种表示格式 及 在Socket编程中的应用
  8. VS2008部分使用技巧
  9. python 下载拉钩教育AES加密视频
  10. Python2与Python3之间的区别?