tensorflow学习3---mnist
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的更多相关文章
- tensorflow学习001——MNIST
1.MNIST是一个入门级的计算机视觉数据集,它包含各种手写数字图片 数据集被分成两部分:60000行的训练数据集(mnist.train)和10000行的测试数据集(mnist.test). 这样的 ...
- tensorflow学习笔记四:mnist实例--用简单的神经网络来训练和测试
刚开始学习tf时,我们从简单的地方开始.卷积神经网络(CNN)是由简单的神经网络(NN)发展而来的,因此,我们的第一个例子,就从神经网络开始. 神经网络没有卷积功能,只有简单的三层:输入层,隐藏层和输 ...
- Tensorflow学习笔记(一):MNIST机器学习入门
学习深度学习,首先从深度学习的入门MNIST入手.通过这个例子,了解Tensorflow的工作流程和机器学习的基本概念. 一 MNIST数据集 MNIST是入门级的计算机视觉数据集,包含了各种手写数 ...
- Tensorflow学习笔记(对MNIST经典例程的)的代码注释与理解
1 #coding:utf-8 # 日期 2017年9月4日 环境 Python 3.5 TensorFlow 1.3 win10开发环境. import tensorflow as tf from ...
- 学习TensorFlow,浅析MNIST的python代码
在github上,tensorflow的star是22798,caffe是10006,torch是4500,theano是3661.作为小码农的我,最近一直在学习tensorflow,主要使用pyth ...
- 学习TensorFlow,邂逅MNIST数据集
如果说"Hello Word!"是程序员的第一个程序,那么MNIST数据集,毫无疑问是机器学习者第一个训练的数据集,本文将使用Google公布的TensorFLow来学习训练MNI ...
- 深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识
深度学习-tensorflow学习笔记(1)-MNIST手写字体识别预备知识 在tf第一个例子的时候需要很多预备知识. tf基本知识 香农熵 交叉熵代价函数cross-entropy 卷积神经网络 s ...
- tensorflow中使用mnist数据集训练全连接神经网络-学习笔记
tensorflow中使用mnist数据集训练全连接神经网络 ——学习曹健老师“人工智能实践:tensorflow笔记”的学习笔记, 感谢曹老师 前期准备:mnist数据集下载,并存入data目录: ...
- 深度学习-tensorflow学习笔记(2)-MNIST手写字体识别
深度学习-tensorflow学习笔记(2)-MNIST手写字体识别超级详细版 这是tf入门的第一个例子.minst应该是内置的数据集. 前置知识在学习笔记(1)里面讲过了 这里直接上代码 # -*- ...
- TensorFlow学习笔记(三)MNIST数字识别问题
一.MNSIT数据处理 MNSIT是一个非常有名的手写体数字识别数据集.包含60000张训练图片,10000张测试图片.每张图片是28X28的数字. TonserFlow提供了一个类来处理 MNSIT ...
随机推荐
- 【Oracle】使用bbed手动提交事务
有时候数据库挂掉,起库会出现ORA-00704错误,而导致ORA-00704错误的根本原因是訪问OBJ$的时候.ORACLE须要回滚段中的数据,而訪问回滚段的时候须要的undo数据已经被覆盖,此时我们 ...
- what' the python之面向对象(进阶)
面向对象的知识点补充(进阶版) classmethod和staticmethod:这两个函数的用途就是可以不用实例化对象就可以调用方法 class Classmethod_Demo(): role = ...
- cxListView和dbgrid联动
procedure TForm1.FormCreate(Sender: TObject); begin ClientDataSet1.First; while not ClientDataSet1.E ...
- Windows系统重装工具 WinToHDD Enterprise v2.8 企业破解版
WinToHDD 是一款实用的 Windows 系统硬盘安装工具,有点类似于NT6 HDD Installer,它能不依靠光驱和U盘,让你直接通过本机硬盘来新装.重装和克隆 Windows 系统,支持 ...
- 敏捷开发— —Scrum 学习笔记
敏捷开发模式是一种从1990年代开始逐渐引起广泛关注的一些新型软件开发方法,是一种应对快速变化的需求的一种软件开发能力.它们的具体名称.理念.过程.术语都不尽相同,相对于"非敏捷" ...
- C语言ini格式配置文件的读写
依赖的类 /*1 utils.h *# A variety of utility functions. *# *# Some of the functions are duplicates of we ...
- LNMP安装目录及配置文件
LNMP安装目录及配置文件位置 LNMP相关软件安装目录Nginx 目录: /usr/local/nginx/MySQL 目录 : /usr/local/mysql/MySQL数据库所在目录:/usr ...
- lua加载函数require和dofile
lua加载函数require和dofile Lua提供高级的require函数来加载运行库.粗略的说require和dofile完成同样的功能但有两点不同: 1. require会搜索目录加载文件; ...
- np.Linear algebra学习
转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html 1.分解 //其中我觉得可以的就是svd奇异值分解吧 ...
- GENIA语料库学习【转载】
来自论文:GENIA corpus—a semantically annotated corpus for bio-textmining 2003 1.介绍 GENIA corpus, a sema ...