————————————————————————————————————

写在开头:此文参照莫烦python教程(墙裂推荐!!!)

————————————————————————————————————

dropout解决overfitting问题

  • overfitting:当机器学习学习得太好了,就会出现过拟合(overfitting)问题。所以,我们就要采取一些措施来避免过拟合的问题。此实验就来看一下dropout对于解决过拟合问题的效果。
  • 例子实验内容:识别手写数字。此实验的步骤和上一篇的识别手写数字步骤很相似。
  • 例子实验的数据集:sklearn中的datasets

  • 主要运用的函数tf.nn.dropout()

  • 主要参数keep_prob。keep_prob表示留下来的结果的百分比,比如你要drop0.4,那么keep_prob就为0.6
import tensorflow as tf
from sklearn.datasets import load_digits
from sklearn.cross_validation import train_test_split
from sklearn.preprocessing import LabelBinarizer #加载数据
digits = load_digits()
X = digits.data
y = digits.target
y = LabelBinarizer().fit_transform(y) #把数字变成1x10的向量
X_train,X_test,y_train,y_test = train_test_split(X,y,test_size = .3) #把数据分成train数据和test数据 #定义添加层
def add_layer(inputs,in_size,out_size,activation_function=None):
#定义添加层内容,返回这层的outputs
Weights = tf.Variable(tf.random_normal([in_size,out_size]))#Weigehts是一个in_size行、out_size列的矩阵,开始时用随机数填满
biases = tf.Variable(tf.zeros([1,out_size])+0.1) #biases是一个1行out_size列的矩阵,用0.1填满
Wx_plus_b = tf.matmul(inputs,Weights)+biases #预测
#实现dropout,keep_drop为丢弃后剩下的百分比
Wx_plus_b = tf.nn.dropout(Wx_plus_b, keep_prob)
if activation_function is None: #如果没有激励函数,那么outputs就是预测值
outputs = Wx_plus_b
else: #如果有激励函数,那么outputs就是激励函数作用于预测值之后的值
outputs = activation_function(Wx_plus_b)
return outputs #定义计算正确率的函数
def t_accuracy(t_xs,t_ys):
global prediction
y_pre = sess.run(prediction,feed_dict={xs:t_xs,keep_prob:1})#测试结果不dropout
correct_pre = tf.equal(tf.argmax(y_pre,1),tf.argmax(t_ys,1))
accuracy = tf.reduce_mean(tf.cast(correct_pre,tf.float32))
result = sess.run(accuracy,feed_dict={xs:t_xs,ys:t_ys,keep_prob:1})
return result #定义输入输出值,和keep_drop值
keep_prob = tf.placeholder(tf.float32)
xs = tf.placeholder(tf.float32, [None, 64]) # 8x8
ys = tf.placeholder(tf.float32, [None, 10]) #添加层
l1 = add_layer(xs, 64, 50,activation_function=tf.nn.tanh)
prediction = add_layer(l1, 50, 10,activation_function=tf.nn.softmax) #误差
cross_entropy = tf.reduce_mean(-tf.reduce_sum(ys * tf.log(prediction),reduction_indices=[1])) # loss #训练
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy) #开始训练
sess = tf.Session()
merged = tf.summary.merge_all()
init = tf.global_variables_initializer()
sess.run(init)
for i in range(1000):
# 设置keep_drop为1,即不进行dropout
sess.run(train_step, feed_dict={xs: X_train, ys: y_train, keep_prob: 1})
if i % 50 == 0:
# 输出正确率
print (t_accuracy(X_test,y_test))
0.20925926
0.7574074
0.81296295
0.8388889
0.85555553
0.8537037
0.84814817
0.8537037
0.85555553
0.8537037
0.85555553
0.8537037
0.8574074
0.85555553
0.8574074
0.8574074
0.8611111
0.8574074
0.85925925
0.8611111
for i in range(1000):
# 设置keep_drop为0.5
sess.run(train_step, feed_dict={xs: X_train, ys: y_train, keep_prob: 0.5})
if i % 50 == 0:
# 输出正确率
print (t_accuracy(X_test,y_test))
0.86851853
0.89444447
0.91481483
0.9166667
0.91481483
0.9222222
0.9259259
0.9222222
0.9296296
0.94074076
0.94074076
0.9351852
0.9351852
0.9351852
0.9351852
0.93333334
0.94074076
0.9351852
0.93703705
0.9351852

由上面的结果可知,当dropout为0.5时,效果明显比一点儿也不丢弃的好!


*点击[这儿:TensorFlow]发现更多关于TensorFlow的文章*


4 TensorFlow入门之dropout解决overfitting问题的更多相关文章

  1. tensorflow学习之(八)使用dropout解决overfitting(过拟合)问题

    #使用dropout解决overfitting(过拟合)问题 #如果有dropout,在feed_dict的参数中一定要加入dropout的值 import tensorflow as tf from ...

  2. TensorFlow实战第七课(dropout解决overfitting)

    Dropout 解决 overfitting overfitting也被称为过度学习,过度拟合.他是机器学习中常见的问题. 图中的黑色曲线是正常模型,绿色曲线就是overfitting模型.尽管绿色曲 ...

  3. tensorflow用dropout解决over fitting-【老鱼学tensorflow】

    在机器学习中可能会存在过拟合的问题,表现为在训练集上表现很好,但在测试集中表现不如训练集中的那么好. 图中黑色曲线是正常模型,绿色曲线就是overfitting模型.尽管绿色曲线很精确的区分了所有的训 ...

  4. tensorflow用dropout解决over fitting

    在机器学习中可能会存在过拟合的问题,表现为在训练集上表现很好,但在测试集中表现不如训练集中的那么好. 图中黑色曲线是正常模型,绿色曲线就是overfitting模型.尽管绿色曲线很精确的区分了所有的训 ...

  5. #tensorflow入门(1)

    tensorflow入门(1) 关于 TensorFlow TensorFlow™ 是一个采用数据流图(data flow graphs),用于数值计算的开源软件库.节点(Nodes)在图中表示数学操 ...

  6. TensorFlow入门(五)多层 LSTM 通俗易懂版

    欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @creat_date: 2017-03-09 前言: 根据我本人学习 TensorFlow 实现 LSTM 的经 ...

  7. 转:TensorFlow入门(六) 双端 LSTM 实现序列标注(分词)

    http://blog.csdn.net/Jerr__y/article/details/70471066 欢迎转载,但请务必注明原文出处及作者信息. @author: huangyongye @cr ...

  8. TensorFlow 入门之手写识别CNN 三

    TensorFlow 入门之手写识别CNN 三 MNIST 卷积神经网络 Fly 多层卷积网络 多层卷积网络的基本理论 构建一个多层卷积网络 权值初始化 卷积和池化 第一层卷积 第二层卷积 密集层连接 ...

  9. (转)TensorFlow 入门

        TensorFlow 入门 本文转自:http://www.jianshu.com/p/6766fbcd43b9 字数3303 阅读904 评论3 喜欢5 CS224d-Day 2: 在 Da ...

随机推荐

  1. emblog后台拿shell

    emlog版本:5.3.1 先本地弄好shell 新建一个文件夹,里面放shell,shell名称和文件名要一致.压缩为zip 然后在安装插件处上传. 成功后的路径content/plugins/te ...

  2. springmvc中配置servlet初始化类

    <bean  id="InitStart" lazy-init="false" init-method="InitSystem" cl ...

  3. hbase练习题

    -- 配置环境变量,因为在hbase中有的地方可能用到了环境变量-- bin/start-hbase.sh-- bin/hbase shell-- 访问http://mini0:16010/ 可以看浏 ...

  4. Fly (From Wikipedia)

    True flies are insects of the order Diptera, the name being derived from the Greek δι- di- "two ...

  5. STM32F10x_RTC秒中断

    Ⅰ.概述 RTC(Real Time Clock)是实时时钟的意思,它其实和TIM有点类似,也是利用计数的原理,选择RTC时钟源,再进行分频,到达计数的目的. 该文主要讲述关于RTC的秒中断功能,这个 ...

  6. leetcode || 64、Minimum Path Sum

    problem: Given a m x n grid filled with non-negative numbers, find a path from top left to bottom ri ...

  7. linux系统中-E,-S,-c的区别和作用(怎么讲代码转化为机器识别的语言)

    1707 许多初学者都有比较大的疑惑,电脑是怎么识别我们写的代码并进行处理的呢?其实这个问题对我们初学者来说是很重要的,只有了解机器的运行原理,我们才能真正地学号留下.那么今天我就以此为题为大家略讲一 ...

  8. ZABBIX监控原理

    zabbix实现原理及架构详解   想要用好zabbix进行监控,那么我们首要需要了解下zabbix这个软件的实现原理及它的架构.建议多阅读官方文档. 一.总体上zabbix的整体架构如下图所示: 重 ...

  9. 蓝桥杯 第三届C/C++预赛真题(6) 大数乘法(数学题)

    对于32位字长的机器,大约超过20亿,用int类型就无法表示了,我们可以选择int64类型,但无论怎样扩展,固定的整数类型总是有表达的极限!如果对超级大整数进行精确运算呢?一个简单的办法是:仅仅使用现 ...

  10. hdu 4539(状压dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4539 思路:跟poj1185简直就是如出一辙! #include<iostream> #i ...