import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data #download data
mnist=input_data.read_data_sets('data/',one_hot=True)
trainimg=mnist.train.images
trainlabel=mnist.train.labels
testimg=mnist.test.images print("downloading...")
print("type:%s" % (type(mnist)))
print("tain data size:%d" % (mnist.train.num_examples))
print("test data size:%d" % (mnist.test.num_examples))
print("tarin lable's shape: %s" % (trainlabel.shape,)) #show example
# nsample = 5
# randidx=np.random.randint(trainimg.shape[0],size=nsample)
# for i in randidx:
# cur_img=np.reshape(trainimg[i,:],(28,28))
# cur_label=np.argmax(trainlabel[i,:])
# plt.matshow(cur_img)
# print(""+str(i)+"th training data,"+"which label is:"+str(cur_label))
# plt.show() #batch
batch_size=100
batch_xs,batch_ys=mnist.train.next_batch(batch_size)#x-data,y-label ####start train
#1.set up
numClasses=10
inputSize=784#28*28
trainningIterations=50000#total steps
batchSize=64# #2.model #64:x(1*784)*w(784*10)+b1(10)=y(1*10)
X=tf.placeholder(tf.float32,shape=[None,inputSize])
y=tf.placeholder(tf.float32,shape=[None,numClasses]) #2.1 initial
W1 = tf.Variable(tf.zeros([784,10]))
B1 = tf.Variable(tf.zeros([10])) #2.2 model set
y_pred=tf.nn.softmax(tf.matmul(X,W1)+B1)#10*1
loss=tf.reduce_mean(tf.square(y-y_pred))
cross_entropy=-tf.reduce_sum(y*tf.log(y_pred))
opt=tf.train.GradientDescentOptimizer(learning_rate=0.05).minimize(cross_entropy)
correct_prediction=tf.equal(tf.argmax(y_pred,1),tf.argmax(y,1))#
accuracy=tf.reduce_mean(tf.cast(correct_prediction,"float"))#bool 2 float #2.3 run train
sess=tf.Session()
init=tf.global_variables_initializer()
sess.run(init)
for i in range(trainningIterations):
batch=mnist.train.next_batch(batch_size)
batchInput=batch[0]
batchLabels=batch[1]
sess.run(opt,feed_dict={X:batchInput,y:batchLabels})
if i%1000 == 0:
train_accuracy=sess.run(accuracy,feed_dict={X:batchInput,y:batchLabels})
print("step %d, tarinning accuracy %g" % (i,train_accuracy)) #2.4 run test to accuracy
batch=mnist.test.next_batch(batch_size)
testAccuracy=sess.run(accuracy,feed_dict={X:batch[0],y:batch[1]})
print("test accuracy %g" % (testAccuracy))

理论参考:

http://www.tensorfly.cn/tfdoc/tutorials/mnist_beginners.html

2.tensorflow——Softmax回归的更多相关文章

  1. 手写数字识别 ----Softmax回归模型官方案例注释(基于Tensorflow,Python)

    # 手写数字识别 ----Softmax回归模型 # regression import os import tensorflow as tf from tensorflow.examples.tut ...

  2. TensorFlow实现Softmax回归(模型存储与加载)

    # -*- coding: utf-8 -*- """ Created on Thu Oct 18 18:02:26 2018 @author: zhen "& ...

  3. 利用TensorFlow识别手写的数字---基于Softmax回归

    1 MNIST数据集 MNIST数据集主要由一些手写数字的图片和相应的标签组成,图片一共有10类,分别对应从0-9,共10个阿拉伯数字.原始的MNIST数据库一共包含下面4个文件,见下表. 训练图像一 ...

  4. 10分钟搞懂Tensorflow 逻辑回归实现手写识别

    1. Tensorflow 逻辑回归实现手写识别 1.1. 逻辑回归原理 1.1.1. 逻辑回归 1.1.2. 损失函数 1.2. 实例:手写识别系统 1.1. 逻辑回归原理 1.1.1. 逻辑回归 ...

  5. 使用Softmax回归将神经网络输出转成概率分布

    神经网络解决多分类问题最常用的方法是设置n个输出节点,其中n为类别的个数.对于每一个样例,神经网络可以得到一个n维数组作为输出结果.数组中的每一个维度(也就是每一个输出节点)对应一个类别,通过前向传播 ...

  6. Haskell手撸Softmax回归实现MNIST手写识别

    Haskell手撸Softmax回归实现MNIST手写识别 前言 初学Haskell,看的书是Learn You a Haskell for Great Good, 才刚看到Making Our Ow ...

  7. Softmax回归

    Reference: http://ufldl.stanford.edu/wiki/index.php/Softmax_regression http://deeplearning.net/tutor ...

  8. Softmax回归(Softmax Regression)

    转载请注明出处:http://www.cnblogs.com/BYRans/ 多分类问题 在一个多分类问题中,因变量y有k个取值,即.例如在邮件分类问题中,我们要把邮件分为垃圾邮件.个人邮件.工作邮件 ...

  9. DeepLearning之路(二)SoftMax回归

    Softmax回归   1. softmax回归模型 softmax回归模型是logistic回归模型在多分类问题上的扩展(logistic回归解决的是二分类问题). 对于训练集,有. 对于给定的测试 ...

随机推荐

  1. Mysql 实现基于binlog的主从同步

    工作原理 1.主节点必须启用二进制日志,记录任何修改了数据库数据的事件.2.从节点开启一个线程(I/O Thread)把自己扮演成 mysql 的客户端,通过 mysql 协议,请求主节点的二进制日志 ...

  2. sql注入判断流程(结合sqli-labs学习)

    sql注入判断流程(结合sqli-labs学习) 类型一 类型判断 ?id=1 and 1=2 --+ 如果返回结果正常,说明不是数字类型 and 为两方都为真才算争取 ?id=1' --+ 显示不正 ...

  3. Dp状态设计与方程总结

    1.不完全状态记录<1>青蛙过河问题<2>利用区间dp 2.背包类问题<1> 0-1背包,经典问题<2>无限背包,经典问题<3>判定性背包问 ...

  4. <搬运> SQL语句百万数据量优化方案

    一:理解sql执行顺序 在sql中,第一个被执行的是from语句,每一个步骤都会产生一个虚拟表,该表供下一个步骤查询时调用,比如语句:select top 10 column1,colum2,max( ...

  5. 1.openshift搭建

    第1章 主机规划和所需文件 1.1 主机规划 IP地址 域名 用途 11.11.233.125 master01.song.test.cnpc 容器编排.etcd 11.11.233.126 mast ...

  6. HR面试总结

    求职面试HR最欣赏的自我介绍 2015-02-25 来源:www.cnrencai.com 浏览:391   明明很有能力的你,在面试中却不能发挥出色?来看看是不是自我介绍环节出了问题. 回答面试题目 ...

  7. tornado ioloop current和instance的一些区别

    import tornado.ioloop # 此时_current没有instance print dir(tornado.ioloop.IOLoop._current) # 通过instance ...

  8. elasticsearch 基础 —— Inner hits

    Inner hits The parent-join and nested 功能允许返回具有不同范围匹配的文档.在父/子案例中,基于子文档中的匹配返回父文档,或者基于父文档中的匹配返回子文档.在嵌套的 ...

  9. elasticsearch 基础 —— 分布式文档存储原理

    路由一个文档到一个分片中 当索引一个文档的时候,文档会被存储到一个主分片中. Elasticsearch 如何知道一个文档应该存放到哪个分片中呢?当我们创建文档时,它如何决定这个文档应当被存储在分片  ...

  10. 脚本_根据 md5 校验码,检测文件是否被修改

    #!bin/bash#功能:根据 md5 校验码,检测文件是否被修改#作者:liusingbon#本示例脚本检测的是/etc 目录下所有的 conf 结尾的文件,根据实际情况,您可以修改为其他目录或文 ...