tensorflow dropout函数应用
1、dropout
dropout 是指在深度学习网络的训练过程中,按照一定的概率将一部分神经网络单元暂时从网络中丢弃,相当于从原始的网络中找到一个更瘦的网络,这篇博客中讲的非常详细

import tensorflow as tf
import numpy as np x_data=np.linspace(-1.,1.,300)[:, np.newaxis]
noise=np.random.normal(0,0.05,x_data.shape)
y_data=np.square(x_data)+noise def add_layer(input,in_size,out_size,activation_function=None,dropout_function=False):
Weights=tf.Variable(tf.random_normal([in_size,out_size]))
biases=tf.Variable(tf.zeros([1,out_size])+0.1)
Wx_plus_b=tf.matmul(input,Weights)+biases if dropout_function==True:
Wx_plus_b=tf.nn.dropout(Wx_plus_b,keep_prob=0.5)
else:
pass if activation_function is None:
outputs=Wx_plus_b
else:
outputs=activation_function(Wx_plus_b)
return outputs xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1]) layer1=add_layer(xs,1,10,activation_function=tf.nn.relu,dropout_function=True)
predict=add_layer(layer1,10,1) loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-predict),reduction_indices=[1]))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss) init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init) for i in range(100):
sess.run(train_step,feed_dict={xs: x_data,ys: y_data})
if i%5==0:
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
输出结果:
1.87953
0.294975
0.173229
0.122831
0.0859246
0.0712419
0.0839021
0.0572673
0.0676766
0.0584741
0.0556422
0.0576011
0.0534098
0.0472549
0.0528496
0.0427771
0.0485248
0.0502765
0.0454531
0.0516633
不用dropout:
import tensorflow as tf
import numpy as np x_data=np.linspace(-1.,1.,300)[:, np.newaxis]
noise=np.random.normal(0,0.05,x_data.shape)
y_data=np.square(x_data)+noise def add_layer(input,in_size,out_size,activation_function=None,dropout_function=False):
Weights=tf.Variable(tf.random_normal([in_size,out_size]))
biases=tf.Variable(tf.zeros([1,out_size])+0.1)
Wx_plus_b=tf.matmul(input,Weights)+biases if dropout_function==True:
Wx_plus_b=tf.nn.dropout(Wx_plus_b,keep_prob=1)
else:
pass if activation_function is None:
outputs=Wx_plus_b
else:
outputs=activation_function(Wx_plus_b)
return outputs xs = tf.placeholder(tf.float32, [None, 1])
ys = tf.placeholder(tf.float32, [None, 1]) layer1=add_layer(xs,1,10,activation_function=tf.nn.relu)
predict=add_layer(layer1,10,1) loss=tf.reduce_mean(tf.reduce_sum(tf.square(ys-predict),reduction_indices=[1]))
train_step=tf.train.GradientDescentOptimizer(0.1).minimize(loss) init=tf.global_variables_initializer()
sess=tf.Session()
sess.run(init) for i in range(100):
sess.run(train_step,feed_dict={xs: x_data,ys: y_data})
if i%5==0:
print(sess.run(loss,feed_dict={xs:x_data,ys:y_data}))
输出结果:
0.256632
0.0532672
0.0364148
0.0264385
0.0202409
0.0164314
0.0141813
0.0129079
0.0121878
0.0117478
0.0114399
0.0111906
0.0109715
0.0107702
0.0105785
0.010393
0.0102144
0.0100426
0.00987562
0.00971407
这里举例 用过dropout后,loss变得更大,是因为我这里所用数据较少,用更多的训练集去测试,dropout会变现的很好!
tensorflow dropout函数应用的更多相关文章
- 深度学习TensorFlow常用函数
tensorflow常用函数 TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU 还是 GPU, Tensor ...
- tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...
- TensorFlow 常用函数汇总
本文介绍了tensorflow的常用函数,源自网上整理. TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CPU ...
- TensorFlow 常用函数与方法
摘要:本文主要对tf的一些常用概念与方法进行描述. tf函数 TensorFlow 将图形定义转换成分布式执行的操作, 以充分利用可用的计算资源(如 CPU 或 GPU.一般你不需要显式指定使用 CP ...
- 『TensorFlow』函数查询列表_神经网络相关
tf.Graph 操作 描述 class tf.Graph tensorflow中的计算以图数据流的方式表示一个图包含一系列表示计算单元的操作对象以及在图中流动的数据单元以tensor对象表现 tf. ...
- Tensorflow常用函数说明(一)
首先最开始应该清楚一个知识,最外面的那个[ [ [ ]]]括号代表第一维,对应维度数字0,第二个对应1,多维时最后一个对应数字-1:因为后面有用到 1 矩阵变换 tf.shape(Tensor) 返回 ...
- TensorFlow——dropout和正则化的相关方法
1.dropout dropout是一种常用的手段,用来防止过拟合的,dropout的意思是在训练过程中每次都随机选择一部分节点不要去学习,减少神经元的数量来降低模型的复杂度,同时增加模型的泛化能力. ...
- tensorflow softmax_cross_entropy_with_logits函数
1.softmax_cross_entropy_with_logits tf.nn.softmax_cross_entropy_with_logits(logits, labels, name=Non ...
- tensorflow l2_loss函数
1.l2_loss函数 tf.nn.l2_loss(t, name=None) 解释:这个函数的作用是利用 L2 范数来计算张量的误差值,但是没有开方并且只取 L2 范数的值的一半,具体如下: out ...
随机推荐
- windows embedded compact 2013 正版免费下载
不知道wince2013是不是真的免费了,不过可以试一下! 下载地址:http://www.microsoft.com/en-us/download/details.aspx?id=39268 你仍然 ...
- Java NIO学习笔记一 Java NIO概述
Java NIO概述 Java NIO(新的IO)是Java的替代IO API(来自Java 1.4),这意味着替代标准的 java IO和java Networking API.Java NIO提供 ...
- Bottle源码阅读笔记(二):路由
前言 程序收到请求后,会根据URL来寻找相应的视图函数,随后由其生成页面发送回给客户端.其中,不同的URL对应着不同的视图函数,这就存在一个映射关系.而处理这个映射关系的功能就叫做路由.路由的实现分为 ...
- 用JMX远程监控Tomcat
要通过JMX远程监控Tomcat,首先需要进行Tomcat的JMX远程配置. 注意:此配置添加在catalina.bat文件开头的注释行(rem)后面即可. 不需鉴权的配置: 先修改Tomcat的启动 ...
- PHP代理访问网络资源
第一种:使用DOMDocument <? PHP $doc = new \DOMDocument(); $opts = array( 'http' => array( ...
- java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleExcept问题解决方案
在部署Dynamic Web Project时,如果正确配置web.xml或者标注时,仍然出现以上异常的话,可以尝试以下内容讲解的方法: 首先,双击eclipse中的servers,位置如下图&quo ...
- JAVA 基础知识学习笔记 名称解释
Java ee: IDE: itegrity development environment 集成开发环境 JMS: java Message Service java 信息服务 JM ...
- JAVAEE——spring01:介绍、搭建、概念、配置详解、属性注入和应用到项目
一.spring介绍 1.三层架构中spring位置 2.spring一站式框架 正是因为spring框架性质是属于容器性质的. 容器中装什么对象就有什么功能.所以可以一站式. 不仅不排斥其他框架,还 ...
- JS读写浏览器cookie及读取页面参数
JS读写浏览器cookie及读取页面参数 var zbrowser = { //设置浏览器cookie,exdays是cookie有效时间 setCookie: function (c_name, v ...
- 一个编程菜鸟的进阶之路(C/C++)
学编程是一条不归路,但我义无反顾.只能往前冲,知道这个过程是痛苦的,所以我开通这个博客,记录自己在编程中遇到的问题和心得,一是希望可以帮助跟我一样遇到同样问题的人,二是把这作为对自己的勉励及回忆: