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. Python 根据入栈顺利判定出栈顺序

    1.读取入栈,出栈数据: 2.把数据分别转化成整数列表: 3.新建栈列表,用入栈数据进行压栈:如果栈列表不为空,并且栈顶层数据为出栈的元素:删除栈列表的顶层数据: 4.如果栈列表不为空,说明栈列表里面 ...

  2. 国内常用Linux镜像站点

    网易镜像站点 http://mirrors.163.com/ 搜狐镜像站点 http://mirrors.sohu.com/ 阿里云镜像站点 http://mirrors.aliyun.com/ 北京 ...

  3. 【五一qbxt】day7-1 引水入城

    [noip2010 洛谷p1514]引水入城 Before: 线段覆盖问题#1:(我们所需要的) 一个区间,若干条线段,现在求最少多少条线段覆盖满整个区间 区间长度8,可选的覆盖线段[2,6],[1, ...

  4. Codeforces 1082G(最大权闭合子图)

    题面 传送门 分析 没想到压轴题是道模板裸题 由于子图的权值=边权和-点权和 将边和点都看成新图中的点 S向原来的边i连边,权值为边权 点i向T连边,权值为点权 边i:(u,v,w)向u,v,连边,权 ...

  5. django的orm操作优化

    django的orm操作优化 models.py from django.db import models class Author(models.Model): name = models.Char ...

  6. hdu6311 Cover (欧拉路径输出)

    hdu6311Cover 题目传送门 题意:有最少用多少条边不重复的路径可以覆盖一个张无向图. 分析:对于一个连通块(单个点除外),如果奇度数点个数为 k,那么至少需要max{k/2,1}  条路径. ...

  7. datagridview里面的checkbox全选和取消全选

    全选 设置全选button,选中所有的checkbox private void selectAll_Click(object sender, EventArgs e) { //遍历datagridv ...

  8. scp - 安全复制(远程文件复制程序)

    总览 SYNOPSIS scp -words [-pqrvBC1246 ] [-F ssh_config ] [-S program ] [-P port ] [-c cipher ] [-i ide ...

  9. hibernate批量写入

    public int insertChanDaoTaskModel(List<T> t) { // TODO Auto-generated method stub Session sess ...

  10. golang对象

    对象和组合 package main import ( "fmt" ) type father struct { name string sex int } type sun st ...