博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
tensorflow 笔记11:tf.nn.dropout() 的使用
阅读量:7104 次
发布时间:2019-06-28

本文共 3324 字,大约阅读时间需要 11 分钟。

tf.nn.dropout:函数 官网说明:
tf.nn.dropout(    x,    keep_prob,    noise_shape=None,    seed=None,    name=None)Defined in tensorflow/python/ops/nn_ops.py.See the guides: Layers (contrib) > Higher level ops for building neural network layers, Neural Network > Activation FunctionsComputes dropout.With probability keep_prob, outputs the input element scaled up by 1 / keep_prob, otherwise outputs 0. The scaling is so that the expected sum is unchanged.By default, each element is kept or dropped independently. If noise_shape is specified, it must be broadcastable to the shape of x, and only dimensions with noise_shape[i] == shape(x)[i] will make independent decisions. For example, if shape(x) = [k, l, m, n] and noise_shape = [k, 1, 1, n], each batch and channel component will be kept independently and each row and column will be kept or not kept together.Args:x: A floating point tensor.keep_prob: A scalar Tensor with the same type as x. The probability that each element is kept.noise_shape: A 1-D Tensor of type int32, representing the shape for randomly generated keep/drop flags.seed: A Python integer. Used to create random seeds. See tf.set_random_seed for behavior.name: A name for this operation (optional).Returns:A Tensor of the same shape of x.Raises:ValueError: If keep_prob is not in (0, 1] or if x is not a floating point tensor.

使用说明:

参数 keep_prob: 表示的是保留的比例,假设为0.8 则 20% 的数据变为0,然后其他的数据乘以 1/keep_prob;keep_prob 越大,保留的越多;

参数 noise_shape:干扰形状。    此字段默认是None,表示第一个元素的操作都是独立,但是也不一定。比例:数据的形状是shape(x)=[k, l, m, n],而noise_shape=[k, 1, 1, n],则第1和4列是独立保留或删除,第2和3列是要么全部保留,要么全部删除。

代码举例:

import osimport numpy as npimport tensorflow as tfx = tf.Variable(tf.ones([10, 10]))inputs = tf.nn.dropout(x, 0.8)init = tf.initialize_all_variables()                                                                             with tf.Session() as sess:     sess.run(init)     print (x.eval())     print (inputs.eval())

输出结果:

[[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.] [1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]][[1.25 1.25 0.   1.25 1.25 1.25 1.25 1.25 1.25 0.  ] [0.   1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25] [1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25 1.25] [1.25 1.25 1.25 1.25 1.25 0.   1.25 1.25 1.25 1.25] [1.25 1.25 1.25 1.25 1.25 0.   1.25 1.25 0.   1.25] [1.25 1.25 1.25 1.25 1.25 0.   0.   1.25 1.25 1.25] [0.   1.25 1.25 0.   1.25 1.25 1.25 0.   1.25 0.  ] [1.25 0.   0.   1.25 1.25 1.25 1.25 1.25 1.25 1.25] [1.25 1.25 1.25 0.   1.25 1.25 1.25 0.   0.   0.  ] [1.25 1.25 0.   0.   0.   0.   1.25 1.25 1.25 1.25]]

加入 noise:

import osimport numpy as npimport tensorflow as tfx = tf.Variable(tf.ones([3,3,3]))inputs = tf.nn.dropout(x, 0.5,[3,1,3])                                                                           init = tf.initialize_all_variables()with tf.Session() as sess:     sess.run(init)     print (x.eval())     print (inputs.eval())

输出:

[[[1. 1. 1.]  [1. 1. 1.]  [1. 1. 1.]] [[1. 1. 1.]  [1. 1. 1.]  [1. 1. 1.]] [[1. 1. 1.]  [1. 1. 1.]  [1. 1. 1.]]][[[0. 2. 2.]  [0. 2. 2.]  [0. 2. 2.]] [[2. 2. 2.]  [2. 2. 2.]  [2. 2. 2.]] [[0. 2. 2.]  [0. 2. 2.]  [0. 2. 2.]]]

 

转载地址:http://ydchl.baihongyu.com/

你可能感兴趣的文章
一家德资企业的网络管理心得
查看>>
IBM WebSphere Portal 6.0的主题与皮肤开发
查看>>
我的友情链接
查看>>
软件研发中缺失的一环:人
查看>>
《云计算》教材配套课件合集
查看>>
linux进程管理
查看>>
java中资源的加载方法
查看>>
python——twisted
查看>>
我的友情链接
查看>>
监听异常关闭
查看>>
通过WifiManager,DhcpInfo获取android IP地址及网关等信息(两种方式)
查看>>
Akka学习笔记:Actor消息传递(1)
查看>>
JDOM xml转map
查看>>
7.1 安装软件包的三种方法 7.2 rpm包介绍 7.3 rpm工具用法 7.4 yum工具用法 7.5 yum搭建本地仓库...
查看>>
CentOS6.5 安装rabbitmq
查看>>
责任链模式
查看>>
微软官方Windows主题 英国之美 高分辨率的壁纸
查看>>
6.自定义Nagios监控项
查看>>
outlook离线地址薄更新失败:缺少对象
查看>>
基于RHEL 6.5安装Oracle 11g详细教程(4)——安装Oracle前的准备
查看>>