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. C#=> 栈模仿堆的操作

    //原理,利用两个栈,互相作用,来模仿堆的效果,先进先出.. using System; using System.Collections.Generic; using System.Linq; us ...

  2. composer 版本号前置~与^符号的区别

    语义化版本https://semver.org/lang/zh-CN/ 了解版本号分 所以用这个说法来理解composer.json里面版本的控制

  3. Java双链表

    一.概述 二.英雄类 class HeroNode { //值域 public int id; public String name; public String nickName; //指针域 pu ...

  4. python第一部分小结

       1.python的种类                                                                              Cpython: ...

  5. jar包 war包

    jar包和war包的区别: war是一个web模块,其中需要包括WEB-INF,是可以直接运行的WEB模块.而jar一般只是包括一些class文件,在声明了Main_class之后是可以用java命令 ...

  6. 使用网易云web 版外部链接

    右击打开控制台,找到音乐列表 ——> 点开详情 -------> 生成外部链接器 ,如: outchain/0/3073492173    ,这是每一个音乐的编号. 然后拼接上它的官网域名 ...

  7. Django学习——开发你的第一个Django应用2

    接着上一节的内容来说.我们将继续关注与上一节制作的polls应用以及Django自动产生额度管理网站. 产生一个管理员用户 首先我们需要产生一个管理员用户,运行如下命令: python manage. ...

  8. valueOf()对象返回值

    valueOf()对象返回值 Array数组的元素被转换为字符串,这些字符串由逗号分隔,连接在一起.其操作与 Array.toString 和 Array.join 方法相同. Boolean为Boo ...

  9. 2018-8-10-win10-uwp-毛玻璃

    title author date CreateTime categories win10 uwp 毛玻璃 lindexi 2018-08-10 19:16:50 +0800 2018-2-13 17 ...

  10. 在Linux服务器上运行jar包,并且使jar包一直处于后台执行

    1.我jar包在linux的目录为/a/bbb.jar         正常情况下,使用在/a目录下使用  java -jar bbb.jar 可以直接运行该jar包的项目,运行成功之后使用crtl+ ...