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 ...
随机推荐
- ThreadLocal(一):Thread 、ThreadLocal、ThreadLocalMap
一.ThreadLocalMap是ThreadLocal的内部类.Thread持有ThreadLocalMap的引用 Entry类继承了WeakReference<ThreadLocal< ...
- 火币网API文档——WebSocket API简介
WebSocket API简介 WebSocket协议是基于TCP的一种新的网络协议.它实现了客户端与服务器之间在单个 tcp 连接上的全双工通信,由服务器主动发送信息给客户端,减少了频繁的身份验证等 ...
- jmeter之最佳实践
官方文档: http://jmeter.apache.org/usermanual/best-practices.html 翻译: 16.最佳实践 16.1 始终使用最新版本的JMeter JMete ...
- 006-Object.assign
一.Object.assign简要使用 是ES6新添加的接口,主要的用途是用来合并多个JavaScript的对象. Object.assign()接口可以接收多个参数,第一个参数是目标对象,后面的都是 ...
- 116A
#include <stdio.h> int main() { int n; int a1,a2; int min=0; int cap=0; scanf("%d",& ...
- LINUX的DNS怎么设置?linux下如何修改DNS地址
LINUX的DNS怎么设置?linux下如何修改DNS地址 https://jingyan.baidu.com/article/870c6fc32c028eb03fe4be30.html Linux下 ...
- 7.C# 多态的实现
C# 多态的实现 封装.继承.多态,面向对象的三大特性,前两项理解相对容易,但要理解多态,特别是深入的了解,对于初学者而言可能就会有一定困难了.我一直认为学习OO的最好方法就是结合实践,封装.继承在实 ...
- 3.C#的访问权限修饰符
C#里类及类成员的修饰符有以下五个如下:public 公开 类及类成员的修饰符 对访问成员没有级别限制private 私有 类成员的修饰符 只能在类的内部访问protected 受保护的 类成员的修饰 ...
- vue滚动事件销毁,填坑
eg:富文本的头部固定,当滚轮大于200时(举例)固定在浏览器头部,距离大于富文本时,头部消失 效果: 在富文本下面加一个空div 这么写: mounted() { $(window).scroll( ...
- cocos2d JS 艺术字特殊符号的显示
this.setSocreAtion(score, this.tfMoneyList[index],mun); //传入分数与对象,调用下面的函数 setSocreAtion : function ( ...