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. 《JAVA设计模式》之建造模式(Builder)

    在阎宏博士的<JAVA与模式>一书中开头是这样描述建造(Builder)模式的: 建造模式是对象的创建模式.建造模式可以将一个产品的内部表象(internal representation ...

  2. 5.1properties属性

    需求: 将数据库连接参数单独配置在db.properties文件中,只需在SqlMapconfig.xml中加载db.properties的属性值. 在SqlMapconfig.xml中就不需要对数据 ...

  3. Linux删除自带的openjdk,安装jdk1.8

    第一步:查看有哪些安装包 [root@localhost ~]# rpm -qa | grep javatzdata-java-2016g-2.el7.noarchpython-javapackage ...

  4. java静态方法使用泛型

    用法 import java.util.ArrayList; import java.util.Collection; import java.util.HashSet; import java.ut ...

  5. CSS中的伪元素选择器

    定义 伪元素选择器:就是有连续两个冒号的选择器,如::first-line::first- letter.::before 和::after E::first-letter文本的第一个单词或字(如中文 ...

  6. javaScript的预加载

    在有大量图片的页面中,为了避免页面加载完图片还未加载完成,我们通常会使用js的图片预加载. 这是一个预加载的demo: 首先把图片放入到一个类名为imgSrcArr的变量当中: var imgSrcA ...

  7. AngularJs双向绑定

    模型数据(Data) 模型是从AngularJS作用域对象的属性引申的.模型中的数据可能是Javascript对象.数组或基本类型,这都不重要,重要的是,他们都属于AngularJS作用域对象. An ...

  8. css盒子模型的宽度不包括margin

    看到教程上和一些博客上盒子模型的宽度 = content + padding + margin + border,应该是不包括margin的 <!DOCTYPE html> <htm ...

  9. 嵌入式系统的性能测试(1) – lmbench篇

    要评价一个系统的性能,通常有不同的指标,相应的会有不同的测试方法和测试工具.既有比较成熟的商业测试软件,也有许多优秀的开源工具来完成这个任务.本文简要介绍如何使用lmbench来完成系统综合性能测试. ...

  10. bzoj1495 [NOI2006]网络收费 复杂度分析+树上背包

    题目传送门 https://lydsy.com/JudgeOnline/problem.php?id=1495 题解 通过观察可以发现,对于一个 \(lca\),如果 \(nA \leq nB\),那 ...