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. 如何调用finecms指定栏目的描述关键词

    有时我们在用finecms建站时需要调用指定栏目的描述和关键词,实现个性化需求,比如id为23的栏目很重要,要让它在首页展示出来,这时我们要如何调用呢?{dr_cat_value(23, 'name' ...

  2. MySQL 5.7 的SSL加密方法

    MySQL 5.7 的SSL加密方法 MySQL 5.7.6或以上版本 (1)创建证书开启SSL验证--安装opensslyum install -y opensslopenssl versionOp ...

  3. python实现根据当前时间创建目录并输出日志

    举个例子:比如我们要实现根据当前时间的年月日来新建目录来存放每天的日志,当前时间作为日志文件名称:代码如下: #!/usr/bin/env python3 # _*_ coding: utf-8 _* ...

  4. Object.keys(),Object.values() 用法

    ES8新特性 Object.keys() 用法 返回键名组成的数组, let arr={ name:'js', sex:'body' } let keys=Object.keys(arr); cons ...

  5. keras后端设置【转载】

    转自:https://keras.io/backend/ At this time, Keras has three backend implementations available: the Te ...

  6. shell for 循环数组

    name=(aa bb) ;i<${#name[*]};i++)) do name=${name[i]} echo "$name" done

  7. zabbix 监控openshift pod状态

    需求: pod中的容器重启一次则报警通知 pod非Runing 状态则报警 pod中的容器非true状态则报警 三个需求其实是有点重叠的 pod重启期间pod肯定会有非Running状态,只要有重启报 ...

  8. [LeetCode] 364. Nested List Weight Sum II_Medium tag:DFS

    Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...

  9. MySQL--4操作数据表中的记录小结

    最常用,最复杂的语句: 每一项的: 表的参照  From 条件    WHERE 进行记录的分组 GROUP BY 分组的时候对分组的条件进行设定  HAVING 对结果进行排序  ORDER BY ...

  10. robot framework自定义python库

    自定义python库的好处: robot framework填表式,将python的灵活性弄没了,但是不要担心,RF早就想到了解决办法,就是扩充自己的库. 1.在python应用程序包目录下创建一个新 ...