tf.nn.conv2d和tf.contrib.slim.conv2d的区别
转自:http://blog.sina.com.cn/s/blog_6ca0f5eb0102wsuu.html
文中摘要:
“
在上述的API中,可以看出去除掉初始化的部分,那么两者并没有什么不同,只是tf.contrib.slim.conv2d提供了更多可以指定的初始化的部分,而对于tf.nn.conv2d而言,其指定filter的方式相比较tf.contrib.slim.conv2d来说,更加的复杂。去除掉少用的初始化部分,其实两者的API可以简化如下:
tf.contrib.slim.conv2d (
inputs,
num_outputs,[卷积核个数]
kernel_size,[卷积核的高度,卷积核的宽度]
stride=1,
padding='SAME',
)
tf.nn.conv2d(
input,(与上述一致)
filter,([卷积核的高度,卷积核的宽度,图像通道数,卷积核个数])
strides,
padding,
)
可以说两者是几乎相同的,运行下列代码也可知这两者一致
”
详细说明:
在查看代码的时候,看到有代码用到卷积层是tf.nn.conv2d,但是也有的使用的卷积层是tf.contrib.slim.conv2d,这两个函数调用的卷积层是否一致,在查看了API的文档,以及slim.conv2d的源码后,做如下总结:
首先是常见使用的tf.nn.conv2d的函数,其定义如下:
conv2d(
input,
filter,
strides,
padding,
use_cudnn_on_gpu=None,
data_format=None,
name=None
)
input指需要做卷积的输入图像,它要求是一个Tensor,具有[batch_size, in_height, in_width, in_channels]这样的shape,具体含义是[训练时一个batch的图片数量, 图片高度, 图片宽度, 图像通道数],注意这是一个4维的Tensor,要求数据类型为float32和float64其中之一
filter用于指定CNN中的卷积核,它要求是一个Tensor,具有[filter_height, filter_width, in_channels, out_channels]这样的shape,具体含义是[卷积核的高度,卷积核的宽度,图像通道数,卷积核个数],要求类型与参数input相同,有一个地方需要注意,第三维in_channels,就是参数input的第四维,这里是维度一致,不是数值一致。这里out_channels指定的是卷积核的个数,而in_channels说明卷积核的维度与图像的维度一致,在做卷积的时候,单个卷积核在不同维度上对应的卷积图片,然后将in_channels个通道上的结果相加,加上bias来得到单个卷积核卷积图片的结果。
strides为卷积时在图像每一维的步长,这是一个一维的向量,长度为4,对应的是在input的4个维度上的步长
use_cudnn_on_gpu指定是否使用cudnn加速,默认为true
data_format是用于指定输入的input的格式,默认为NHWC格式
结果返回一个Tensor,这个输出,就是我们常说的feature map
而对于tf.contrib.slim.conv2d,其函数定义如下:
weights_initializer=initializers.xavier_initializer(),
biases_initializer=init_ops.zeros_initializer(),
num_outputs指定卷积核的个数(就是filter的个数)
kernel_size用于指定卷积核的维度(卷积核的宽度,卷积核的高度)
padding为padding的方式选择,VALID或者SAME
rate这个参数不是太理解,而且tf.nn.conv2d中也没有,对于使用atrous convolution的膨胀率(不是太懂这个atrous convolution)
activation_fn用于激活函数的指定,默认的为ReLU函数
weights_initializer用于指定权重的初始化程序
weights_regularizer为权重可选的正则化程序
biases_initializer用于指定biase的初始化程序
biases_regularizer: biases可选的正则化程序
variable_collections指定所有变量的集合列表或者字典
filter,([卷积核的高度,卷积核的宽度,图像通道数,卷积核个数])
import tensorflow.contrib.slim as slim
x1 = tf.ones(shape=[1, 64, 64, 3])
# print("rank is", tf.rank(x1))
y1 = tf.nn.conv2d(x1, w, strides=[1, 1, 1, 1], padding='SAME')
y2 = slim.conv2d(x1, 64, [5, 5], weights_initializer=tf.ones_initializer, padding='SAME')
sess.run(tf.global_variables_initializer())
y1_value,y2_value,x1_value=sess.run([y1,y2,x1])
print("shapes are", y1_value.shape, y2_value.shape)
最后配上tf.contrib.slim.conv2d的API英文版
weights_initializer=initializers.xavier_initializer(),
biases_initializer=init_ops.zeros_initializer(),
"""Adds an N-D convolution followed by an optional batch_norm layer.
It is required that 1 <= N <= 3.
`convolution` creates a variable called `weights`, representing the
convolutional kernel, that is convolved (actually cross-correlated) with the
`inputs` to produce a `Tensor` of activations. If a `normalizer_fn` is
provided (such as `batch_norm`), it is then applied. Otherwise, if
`normalizer_fn` is None and a `biases_initializer` is provided then a `biases`
variable would be created and added the activations. Finally, if
`activation_fn` is not `None`, it is applied to the activations as well.
Performs atrous convolution with input stride/dilation rate equal to `rate`
if a value > 1 for any dimension of `rate` is specified. In this case
`stride` values != 1 are not supported.
inputs: A Tensor of rank N+2 of shape
`[batch_size] + input_spatial_shape + [in_channels]` if data_format does
not start with "NC" (default), or
`[batch_size, in_channels] + input_spatial_shape` if data_format starts
num_outputs: Integer, the number of output filters.
kernel_size: A sequence of N positive integers specifying the spatial
dimensions of the filters. Can be a single integer to specify the same
value for all spatial dimensions.
stride: A sequence of N positive integers specifying the stride at which to
compute output. Can be a single integer to specify the same value for all
spatial dimensions. Specifying any `stride` value != 1 is incompatible
with specifying any `rate` value != 1.
padding: One of `"VALID"` or `"SAME"`.
data_format: A string or None. Specifies whether the channel dimension of
the `input` and output is the last dimension (default, or if `data_format`
does not start with "NC"), or the second dimension (if `data_format`
starts with "NC"). For N=1, the valid values are "NWC" (default) and
"NCW". For N=2, the valid values are "NHWC" (default) and "NCHW".
For N=3, the valid values are "NDHWC" (default) and "NCDHW".
rate: A sequence of N positive integers specifying the dilation rate to use
foratrous convolution. Can be a single integer to specify the same
value for all spatial dimensions. Specifying any `rate` value != 1 is
incompatible with specifying any `stride` value != 1.
activation_fn: Activation function. The default value is a ReLU function.
Explicitly set it to None to skip it and maintain a linear activation.
normalizer_fn: Normalization function to use instead of `biases`. If
`normalizer_fn` is provided then `biases_initializer` and
`biases_regularizer` are ignored and `biases` are not created nor added.
default set to None for no normalizer function
normalizer_params: Normalization function parameters.
weights_initializer: An initializer for the weights.
weights_regularizer: Optional regularizer for the weights.
biases_initializer: An initializer for the biases. If None skip biases.
biases_regularizer: Optional regularizer for the biases.
reuse: Whether or not the layer and its variables should be reused. To be
able to reuse the layer scope must be given.
variables_collections: Optional list of collections for all the variables or
a dictionary containing a different list of collection per variable.
outputs_collections: Collection to add the outputs.
trainable: If `True` also add variables to the graph collection
`GraphKeys.TRAINABLE_VARIABLES` (see tf.Variable).
scope: Optional scope for `variable_scope`.
A tensor representing the output of the operation.
ValueError: If `data_format` is invalid.
ValueError: Both 'rate' and `stride` are not uniformly 1.
tf.nn.conv2d和tf.contrib.slim.conv2d的区别相关推荐
- TensorFlow学习——tf.nn.conv2d和tf.contrib.slim.conv2d的区别
在查看代码的时候,看到有代码用到卷积层是tf.nn.conv2d,也有的使用的卷积层是tf.contrib.slim.conv2d,这两个函数调用的卷积层是否一致,在查看了API的文档,以及slim. ...
- tf.nn.softmax_cross_entropy_with_logits 和 tf.contrib.legacy_seq2seq.sequence_loss_by_example 的联系与区别
文章目录 0.函数介绍 1.区别联系 1.1 tf.nn.softmax_cross_entropy_with_logits 1.2 tf.nn.sparse_softmax_cross_entrop ...
- 【TensorFlow】TensorFlow函数精讲之tf.nn.max_pool()和tf.nn.avg_pool()
tf.nn.max_pool()和tf.nn.avg_pool()是TensorFlow中实现最大池化和平均池化的函数,在卷积神经网络中比较核心的方法. 有些和卷积很相似,可以参考TensorFlow ...
- TensorFlow基础篇(六)——tf.nn.max_pool()和tf.nn.avg_pool()
tf.nn.max_pool()和tf.nn.avg_pool()是TensorFlow中实现最大池化和平均池化的函数,在卷积神经网络中比较核心的方法. 有些和卷积很相似,可以参考TensorFlow ...
- tf.nn.dropout和tf.keras.layers.Dropout的区别(TensorFlow2.3)与实验
这里写目录标题 场景:dropout和Dropout区别 问题描述: 结论: 深层次原因:dropout是底层API,Dropout是高层API 场景:dropout和Dropout区别 全网搜索tf ...
- TensorFlow 辨异 —— tf.add(a, b) 与 a+b(tf.assign 与 =)、tf.nn.bias_add 与 tf.add
1. tf.add(a, b) 与 a+b 在神经网络前向传播的过程中,经常可见如下两种形式的代码: tf.add(tf.matmul(x, w), b) tf.matmul(x, w) + b 简而 ...
- tensorflow 的 Batch Normalization 实现(tf.nn.moments、tf.nn.batch_normalization)
tensorflow 在实现 Batch Normalization(各个网络层输出的归一化)时,主要用到以下两个 api: tf.nn.moments(x, axes, name=None, kee ...
- tf.nn.sparse_softmax_cross_entropy_with_logits()与tf.nn.softmax_cross_entropy_with_logits的差别
这两个函数的用法类似 sparse_softmax_cross_entropy_with_logits(_sentinel=None, labels=None, logits=None, name=N ...
- 深度学习-函数-tf.nn.embedding_lookup 与tf.keras.layers.Embedding
embedding函数用法 1. one_hot编码 1.1. 简单对比 1.2.优势分析: 1.3. 缺点分析: 1.4. 延伸思考 2. embedding的用途 2.1 embedding有两个 ...
最新文章
- mysql 常用函数循环_近30个MySQL常用函数,看到就是学到,纯干货收藏!
- python的语法结构_Python特点、语法结构、编码知识
- 3MIN干完一周的工作量?快来看看应该如何部署 Kubernetes!
- 夸克浏览器有没有linux,夸克浏览器怎么样?夸克浏览器使用说明
- 【商业落地篇】Gartner第四范式全球首发AutoML系列白皮书(限时免费下载)
- 酷派手机android版本,酷派大神的手机系统是什么?酷派大神能升级安卓4.3吗?...
- PCI总线体系结构概述
- matlab和python哪个运行快_MATLAB比Python快吗?
- Nessus进行漏洞扫描的过程
- Android表格拖拽排序,Android 拖拽排序控件 DragGridView
- 洛阳计算机学校排名2015年,洛阳最好的中专学校有哪些 十大中专学校排名
- 建模算法(六)——神经网络模型
- 查看Linux版本命令
- 1核2g服务器能干什么_国内哪个云服务器比较便宜性价比高?大家有什么好推荐...
- 基于Hive数据仓库的标签画像实战
- 阿里云科技驱动“数字化转型”,助力中小企业发展“突围”
- 科幻迷福利-黑客帝国4明年1.14
- Echo写入一句话木马+分段写入
- 吹得这么牛皮的RPC,到底是个什么鬼?该如何实现呢?
- 【Lua】Lua编程相关
热门文章
- db2 import 报错 SQL3306N An SQL error -964 occurred while inserting a row into the table. - Remember
- 排序(数据结构与算法)
- 使用React Hooks 时要避免的5个错误!
- 快速搭建个人博客网站——Hexo
- 关于有盘产品市场调查
- Camera ITS当中的test_ev_compensation_basic测试
- 【Spring boot 中 Excel 模板文件损坏问题】
- XML 链接语言(XLink) 版本 1.0
- 多吃巧克力多笑脑子会更聪明
- 软件测试工程师这个岗位职责是什么?具体都需要干什么?