MINST数据softmax进行数字识别
来源:互联网 发布:淘宝金牌客服培训 编辑:程序博客网 时间:2024/05/24 01:34
1.首先安装TensorFlow最好Python在3.5以上。
2.下载intput_data.py进行下载minst数据和使用数据。
3.修改intput_data.py中的错误。
return numpy.frombuffer(bytestream.read(4), dtype=dt)修改为下面return numpy.frombuffer(bytestream.read(4), dtype=dt)[0]
4.实现softmax
#导入Minst数据集import input_datamnist = input_data.read_data_sets("MINST_DATA",one_hot=True)#导入tensorflow库import tensorflow as tf#输入变量,把28*28的图片变成一维数组(丢失结构信息)x = tf.placeholder("float",[None,784])#权重矩阵,把28*28=784的一维输入,变成0-9这10个数字的输出w = tf.Variable(tf.zeros([784,10]))#偏置b = tf.Variable(tf.zeros([10]))#核心运算,其实就是softmax(x*w+b)y = tf.nn.softmax(tf.matmul(x,w) + b)#这个是训练集的正确结果y_ = tf.placeholder("float",[None,10])#交叉熵,作为损失函数cross_entropy = -tf.reduce_sum(y_ * tf.log(y))#梯度下降算法,最小化交叉熵train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)#初始化,在run之前必须进行的init = tf.initialize_all_variables()#创建session以便运算sess = tf.Session()sess.run(init)#迭代1000次for i in range(1000): #获取训练数据集的图片输入和正确表示数字 batch_xs, batch_ys = mnist.train.next_batch(100) #运行刚才建立的梯度下降算法,x赋值为图片输入,y_赋值为正确的表示数字 sess.run(train_step,feed_dict = {x:batch_xs, y_: batch_ys})#tf.argmax获取最大值的索引。比较运算后的结果和本身结果是否相同。#这步的结果应该是[1,1,1,1,1,1,1,1,0,1...........1,1,0,1]这种形式。#1代表正确,0代表错误correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))#tf.cast先将数据转换成float,防止求平均不准确。#tf.reduce_mean由于只有一个参数,就是上面那个数组的平均值。accuracy = tf.reduce_mean(tf.cast(correct_prediction,"float"))#输出print(sess.run(accuracy,feed_dict={x:mnist.test.images,y_: mnist.test.labels}))
结果:
C:\python35\python.exe C:/Users/User/PycharmProjects/nlpdemo/tensorflowlearn/softmaxlearn.pyExtracting MINST_DATA\train-images-idx3-ubyte.gzExtracting MINST_DATA\train-labels-idx1-ubyte.gzExtracting MINST_DATA\t10k-images-idx3-ubyte.gzExtracting MINST_DATA\t10k-labels-idx1-ubyte.gzWARNING:tensorflow:From C:\python35\lib\site-packages\tensorflow\python\util\tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.Instructions for updating:Use `tf.global_variables_initializer` instead.2017-10-10 20:19:21.813200: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.2017-10-10 20:19:21.813200: W C:\tf_jenkins\home\workspace\rel-win\M\windows\PY\35\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.0.9021Process finished with exit code 0
阅读全文
0 0
- MINST数据softmax进行数字识别
- Softmax回归——识别MINST数据库
- mnsit 手写数据集 python3.x的读入 以及利用softmax回归进行数字识别
- TensorFlow学习笔记(1):使用softmax对手写体数字(MNIST数据集)进行识别
- Tensorflow - Tutorial (2) : 利用softmax回归进行手写数字识别
- 用TensorFlow的Softmax Regression进行手写数字识别
- [DL]2.使用Softmax回归进行手写数字识别
- 【TensorFlow-windows】(一)实现Softmax Regression进行手写数字识别(mnist)
- softmax手写数字识别 python代码
- TensorFlow实现Softmax Regression识别手写数字
- TensorFlow实现Softmax Regression手写数字识别
- TensorFlow 实现 Softmax Regression 识别手写数字
- TensorFlow 实现Softmax Regression 识别数字
- TensorFlow实现Softmax Regression识别手写数字
- TensorFlow 实现Softmax Regression识别手写数字
- tensorflow实现softmax regression识别手写数字
- Tensorflow实现Softmax Regression识别手写数字
- TensorFlow学习笔记(3)--实现Softmax逻辑回归识别手写数字(MNIST数据集)
- html点击按钮切换图片,并自动定时轮换
- 第三周项目一 顺序表的基本运算
- SDUT 3399 数据结构实验之排序二-交换排序
- ImageLoader的配置
- 反射
- MINST数据softmax进行数字识别
- Spring定时任务的实现方式--ScheduledExecutorService and ScheduledExecutorFactoryBean的简单源码解析以及使用
- nrm —— 快速切换 NPM 源 (附带测速功能)
- linux php添加pdo_mysql 扩展时报错 fatal error: ext/mysqlnd/mysqlnd.h: No such file or directory
- 未能加载文件或程序集“System.Web.Http.WebHost, Version=4.0.0.0, ”或它的某一个依赖项。系统找不到指定的文件。
- ScrollView和RecyclerView嵌套使用显示不全问题解决
- ES6之"let"能替代"var"吗?
- 九度OJ——1107搬水果
- Hibernate框架简述