import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data '''数据下载'''
mnist=input_data.read_data_sets('Mnist_data',one_hot=True)
#one_hot标签 '''生成层 函数'''
def add_layer(input,in_size,out_size,n_layer='layer',activation_function=None):
layer_name='layer %s' % n_layer
'''补充知识'''
#tf.name_scope:Wrapper for Graph.name_scope() using the default graph.
#scope名字的作用域
#sprase:A string (not ending with '/') will create a new name scope, in which name is appended to the prefix of all operations created in the context.
#If name has been used before, it will be made unique by calling self.unique_name(name).
with tf.name_scope('weights'):
Weights=tf.Variable(tf.random_normal([in_size,out_size]),name='w')
tf.summary.histogram(layer_name+'/wights',Weights)
#tf.summary.histogram:output summary with histogram直方图
#tf,random_normal正太分布
with tf.name_scope('biases'):
biases=tf.Variable(tf.zeros([1,out_size])+0.1)
tf.summary.histogram(layer_name+'/biases',biases)
#tf.summary.histogram:k
with tf.name_scope('Wx_plus_b'):
Wx_plus_b=tf.matmul(input,Weights)+biases
if activation_function==None:
outputs=Wx_plus_b
else:
outputs=activation_function(Wx_plus_b)
tf.summary.histogram(layer_name+'/output',outputs)
return outputs
'''准确率'''
def compute_accuracy(v_xs,v_ys):
global prediction
y_pre=sess.run(prediction,feed_dict={xs:v_xs})#<
#tf.equal()对比预测值的索引和实际label的索引是否一样,一样返回True,否则返回false
correct_prediction=tf.equal(tf.argmax(y_pre,1),tf.argmax(v_ys,1))
#correct_prediction-->[ True False True ..., True True True]
'''补充知识-tf.argmax'''
#tf.argmax:Returns the index with the largest value across dimensions of a tensor.
#tf.argmax()----->
accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))
#正确cast为1,错误cast为0
'''补充知识 tf.cast'''
#tf.cast: Casts a tensor to a new type.
## tensor `a` is [1.8, 2.2], dtype=tf.float
#tf.cast(a, tf.int32) ==> [1, 2] # dtype=tf.int32
result=sess.run(accuracy,feed_dict={xs:v_xs,ys:v_ys})
#print(sess.run(correct_prediction,feed_dict={xs:v_xs,ys:v_ys}))
#ckc=tf.cast(correct_prediction,tf.float32)
#print(sess.run(ckc,feed_dict={xs:v_xs,ys:v_ys}))
return result '''占位符'''
xs=tf.placeholder(tf.float32,[None,784])
ys=tf.placeholder(tf.float32,[None,10]) '''添加层''' prediction=add_layer(xs,784,10,activation_function=tf.nn.softmax)
#sotmax激活函数,用于分类函数 '''计算'''
#交叉熵cross_entropy损失函数,参数分别为实际的预测值和实际的label值y,re
'''补充知识'''
#reduce_mean()
# 'x' is [[1., 1. ]]
# [2., 2.]]
#tf.reduce_mean(x) ==> 1.5
#tf.reduce_mean(x, 0) ==> [1.5, 1.5]
#tf.reduce_mean(x, 1) ==> [1., 2.]
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys*tf.log(prediction),reduction_indices=[1]))
'''补充知识'''
#reduce_sum
# 'x' is [[1, 1, 1]]
# [1, 1, 1]]
#tf.reduce_sum(x) ==> 6
#tf.reduce_sum(x, 0) ==> [2, 2, 2]
#tf.reduce_sum(x, 1) ==> [3, 3]
#tf.reduce_sum(x, 1, keep_dims=True) ==> [[3], [3]]
#tf.reduce_sum(x, [0, 1]) ==> 6 train_step=tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) '''Session_begin'''
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
for i in range(1000):
batch_xs,batch_ys=mnist.train.next_batch(100) #逐个batch去取数据
sess.run(train_step,feed_dict={xs:batch_xs,ys:batch_ys})
if(i%50==0):
print(compute_accuracy(mnist.test.images,mnist.test.labels))

tensorflow学习3---mnist的更多相关文章

  1. tensorflow学习001——MNIST

    1.MNIST是一个入门级的计算机视觉数据集,它包含各种手写数字图片 数据集被分成两部分:60000行的训练数据集(mnist.train)和10000行的测试数据集(mnist.test). 这样的 ...

  2. tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试

    刚开始学习tf时,我们从简单的地方开始.卷积神经网络(CNN)是由简单的神经网络(NN)发展而来的,因此,我们的第一个例子,就从神经网络开始. 神经网络没有卷积功能,只有简单的三层:输入层,隐藏层和输 ...

  3. Tensorflow学习笔记(一):MNIST机器学习入门

    学习深度学习,首先从深度学习的入门MNIST入手.通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念. 一  MNIST数据集 MNIST是入门级的计算机视觉数据集,包含了各种手写数 ...

  4. Tensorflow学习笔记(对MNIST经典例程的)的代码注释与理解

    1 #coding:utf-8 # 日期 2017年9月4日 环境 Python 3.5  TensorFlow 1.3 win10开发环境. import tensorflow as tf from ...

  5. 学习TensorFlow,浅析MNIST的python代码

    在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...

  6. 学习TensorFlow,邂逅MNIST数据集

    如果说"Hello Word!"是程序员的第一个程序,那么MNIST数据集,毫无疑问是机器学习者第一个训练的数据集,本文将使用Google公布的TensorFLow来学习训练MNI ...

  7. 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识

    深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...

  8. tensorflow中使用mnist数据集训练全连接神经网络-学习笔记

    tensorflow中使用mnist数据集训练全连接神经网络 ——学习曹健老师“人工智能实践:tensorflow笔记”的学习笔记, 感谢曹老师 前期准备:mnist数据集下载,并存入data目录: ...

  9. 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别

    深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...

  10. TensorFlow学习笔记(三)MNIST数字识别问题

    一.MNSIT数据处理 MNSIT是一个非常有名的手写体数字识别数据集.包含60000张训练图片,10000张测试图片.每张图片是28X28的数字. TonserFlow提供了一个类来处理 MNSIT ...

随机推荐

  1. vue mounted中监听div的变化

    vue mounted中监听div的变化 <div style="width:200px;height:30px;background: #0e90d2" id=" ...

  2. [LeetCode] 643. Maximum Average Subarray I_Easy tag: Dynamic Programming(Sliding windows)

    Given an array consisting of n integers, find the contiguous subarray of given length k that has the ...

  3. 带上RESTful的金手铐,你累吗?

    1. 首先RESTful是一套规范,不是框架,它是来约束你的.也不关心生产效率的提高.就好像使用汇编开发应用,性能是快了,但是生产效率很低.RESTful它需要你在路由上定义很多规则来解释的URL,假 ...

  4. js关卡函数,throat函数实现,定时运行函数

    function throat(callback,num){ var timer = null; callback = callback || function(){}; return functio ...

  5. 过拟合VS欠拟合、偏差VS方差

    1. 过拟合 欠拟合 过拟合:在训练集(training set)上表现好,但是在测试集上效果差,也就是说在已知的数据集合中非常好,但是在添加一些新的数据进来训练效果就会差很多,造成这样的原因是考虑影 ...

  6. 委托 匿名 lambda表达式

    #region 委托 delegate int ACT(int a, int b); static void Main(string[] args) { ACT act = new ACT(add); ...

  7. 办法总比困难多--JMeter压测小记

    标签: JMeter, 代理接口 前段时间溢+支付接口慢,有商户直接反映掉单严重.这时,我接到了压测溢+支付接口的任务.JMeter我并不熟悉,公司也没有搞自动化测试的QA,一开始team里的一个同学 ...

  8. HDU 3117 Fibonacci Numbers(矩阵)

    Fibonacci Numbers [题目链接]Fibonacci Numbers [题目类型]矩阵 &题解: 后4位是矩阵快速幂求,前4位是用log加Fibonacci通项公式求,详见上一篇 ...

  9. Vue项目中跨域的几种方式

    经常使用vue + webpack搭建项目,但在请求某些json数据时存在跨域问题,此时有几种修改方法 1. 修改后台header, 但如果只是请求外部数据,是没法修改后台配置的 header('Ac ...

  10. web前端 ajax请求报415/400错

    一.当使用jQuery 的ajax发送请求时 1.请求路劲没有出错并且不存在跨域,出现415错误 解决方法:在ajax请求中加上contentType: 'application/json', $.a ...