对tensorflow中tf.nn.conv1d和layers.conv1d的区别详解
作者:孤独的猿行客 发布时间:2022-07-17 02:28:27
在用tensorflow做一维的卷积神经网络的时候会遇到tf.nn.conv1d和layers.conv1d这两个函数,但是这两个函数有什么区别呢,通过计算得到一些规律。
1.关于tf.nn.conv1d的解释,以下是Tensor Flow中关于tf.nn.conv1d的API注解:
Computes a 1-D convolution given 3-D input and filter tensors.
Given an input tensor of shape [batch, in_width, in_channels] if data_format is "NHWC", or [batch, in_channels, in_width] if data_format is "NCHW", and a filter / kernel tensor of shape [filter_width, in_channels, out_channels], this op reshapes the arguments to pass them to conv2d to perform the equivalent convolution operation.
Internally, this op reshapes the input tensors and invokes `tf.nn.conv2d`. For example, if `data_format` does not start with "NC", a tensor of shape [batch, in_width, in_channels] is reshaped to [batch, 1, in_width, in_channels], and the filter is reshaped to [1, filter_width, in_channels, out_channels]. The result is then reshaped back to [batch, out_width, out_channels] whereoutwidthisafunctionofthestrideandpaddingasinconv2dwhereoutwidthisafunctionofthestrideandpaddingasinconv2d and returned to the caller.
Args: value: A 3D `Tensor`. Must be of type `float32` or `float64`. filters: A 3D `Tensor`. Must have the same type as `input`. stride: An `integer`. The number of entries by which the filter is moved right at each step. padding: 'SAME' or 'VALID' use_cudnn_on_gpu: An optional `bool`. Defaults to `True`. data_format: An optional `string` from `"NHWC", "NCHW"`. Defaults to `"NHWC"`, the data is stored in the order of [batch, in_width, in_channels]. The `"NCHW"` format stores data as [batch, in_channels, in_width]. name: A name for the operation (optional).
Returns:
A `Tensor`. Has the same type as input.
Raises:
ValueError: if `data_format` is invalid.
什么意思呢?就是说conv1d的参数含义:(以NHWC格式为例,即,通道维在最后)
1、value:在注释中,value的格式为:[batch, in_width, in_channels],batch为样本维,表示多少个样本,in_width为宽度维,表示样本的宽度,in_channels维通道维,表示样本有多少个通道。 事实上,也可以把格式看作如下:[batch, 行数, 列数],把每一个样本看作一个平铺开的二维数组。这样的话可以方便理解。
2、filters:在注释中,filters的格式为:[filter_width, in_channels, out_channels]。按照value的第二种看法,filter_width可以看作每次与value进行卷积的行数,in_channels表示value一共有多少列(与value中的in_channels相对应)。out_channels表示输出通道,可以理解为一共有多少个卷积核,即卷积核的数目。
3、stride:一个整数,表示步长,每次(向下)移动的距离(TensorFlow中解释是向右移动的距离,这里可以看作向下移动的距离)。
4、padding:同conv2d,value是否需要在下方填补0。
5、name:名称。可省略。
首先从参数列表可以看出value指的输入的数据,stride就是卷积的步长,这里我们最有疑问的就是filters这个参数,那么我们对filter进行简单的说明。从上面可以看到filters的格式为:[filter_width, in_channels, out_channels],这是一个数组的维度,对应的是卷积核的大小,输入的channel的格式,和卷积核的个数,下面我们用例子说明问题:
import tensorflow as tf
import numpy as np
if __name__ == '__main__':
inputs = tf.constant(np.arange(1, 6, dtype=np.float32), shape=[1, 5, 1])
w = np.array([1, 2], dtype=np.float32).reshape([2, 1, 1])
# filter width, filter channels and out channels(number of kernels)
cov1 = tf.nn.conv1d(inputs, w, stride=1, padding='VALID')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(cov1)
print(out)
其输出为:
[[[ 5.],
[ 8.],
[11.],
[14.]]]
我们分析一下,输入的数据为[[[1],[2],[3],[4],[5]]],有5个特征,分别对应的数值为1,2,3,4,5,那么经过卷积的结果为5,8,11,14,那么这个结果是怎么来的呢,我们根据卷积的计算,可以得到5 = 1*1 + 2*2, 8=2*1+ 3*2, 11 = 3*1+4*2, 14=4*1+5*2, 也就是W1=1, W2=2,正好和我们先面filters设置的数值相等,
w = np.array([1, 2], dtype=np.float32).reshape([2, 1, 1])
所以可以看到这个filtes设置的是是卷积核矩阵的,换句话说,卷积核矩阵我们是可以设置的。
2. 1.关于tf.layers.conv1d,函数的定义如下
tf.layers.conv1d(
inputs,
filters,
kernel_size,
strides=1,
padding='valid',
data_format='channels_last',
dilation_rate=1,
activation=None,
use_bias=True,
kernel_initializer=None,
bias_initializer=tf.zeros_initializer(),
kernel_regularizer=None,
bias_regularizer=None,
activity_regularizer=None,
kernel_constraint=None,
bias_constraint=None,
trainable=True,
name=None,
reuse=None
)
比较重要的几个参数是inputs, filters, kernel_size,下面分别说明
inputs : 输入tensor, 维度(None, a, b) 是一个三维的tensor
None : 一般是填充样本的个数,batch_size
a : 句子中的词数或者字数
b : 字或者词的向量维度
filters : 过滤器的个数
kernel_size : 卷积核的大小,卷积核其实应该是一个二维的,这里只需要指定一维,是因为卷积核的第二维与输入的词向量维度是一致的,因为对于句子而言,卷积的移动方向只能是沿着词的方向,即只能在列维度移动。一个例子:
import tensorflow as tf
import numpy as np
if __name__ == '__main__':
inputs = tf.constant(np.arange(1, 6, dtype=np.float32), shape=[1, 5, 1])
cov2 = tf.layers.conv1d(inputs, filters=1, kernel_size=2, strides=1, padding='VALID')
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
out = sess.run(cov2)
print(out)
输出结果:
[[[-1.9953331]
[-3.5520997]
[-5.108866 ]
[-6.6656327]]]
也许你得到的结果和我得到的结果不同,因为在这个函数里面只是设置了卷积核的尺寸和步长,没有设置具体的卷积核矩阵,所以这个卷积核矩阵是随机生成的,就会出现可能运行上面的程序出现不同结果的情况。
来源:https://blog.csdn.net/u013323018/article/details/90444952


猜你喜欢
- 一组有序项目的集合可变的数据类型【可进行增删改查】列表中可以包含任何数据类型,也可包含另一个列表【可任意组合嵌套】列表是以方括号“[]”包围
- 介绍还记得你在小学时学习如何加减数字吗?现在,你也可以对图像做同样的事情!输入图像可以进行算术运算,例如加法、减法和按位运算(AND、OR、
- el-col-group"el-col-group" 是一个 Vue.js 函数式组件,允许您在 "el-ta
- 效果图实例代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transit
- 建造者模式:将一个复杂对象的构建与他的表示分离,使得同样的构建过程可以创建不同的表示。基本思想某类产品的构建由很多复杂组件组成;这些组件中的
- 本文提供了三种不同的方式在Python(IPython Notebook)中调用ggplot。在大数据时代,数据可视化是一个非常热门的话题。
- 痛点在传统的工作中,发送会议纪要是一个比较繁琐的任务,需要手动输入邮件内容、收件人、抄送人等信息,每次发送都需要重复操作,不仅费时费力,而且
- 目录概述语法定义接口实现接口空接口接口的组合总结概述Go 语言中的接口就是方法签名的集合,接口只有声明,没有实现,不包含变量。语法定义接口t
- 作用有局限性,必须在指定的环境下,才能匹配成功,是受到很多因素的影响,所以有一定的适应性模板匹配是一种最原始、最基本的模式识别方法,研究某一
- python里面可以将路径里面的\替换成/避免转义。os.walk方法可以将目标路径下文件的root,dirs,files提取出来。后面对每
- cmake-2.8.3.tar.gzmysql-5.5.8.tar.gz一,cmake-2.8.3的安装:tar -zxf cmake-2.
- 今天也碰到了el表达式无法解析的事情,于是在网上查询了下,大多说是因为web.xml中声明的版本问题于是收集了如下版本:web-app_2_
- python代码生成API接口如果要将我们写好的Python代码生成API接口时,我们需要借助Flask框架1. 安装Flaskpip in
- 1.原生js操作domconst dom = getElementById(‘box')2.vue官方方法:refvue中的ref是
- 我们有时候希望回车键敲在文本框(input element)里来提交表单(form),但有时候又不希望如此。比如搜索行为,希望输入完关键词之
- 什么是性能分析?性能分析是衡量应用程序在代码级别的相对性能。性能分析将捕捉的事件包括:CPU的使用,内存的使用,函数的调用时长和次数,以及调
- python断言assert语句assert语句的格式是【assert 表达式,返回数据】,当表达式为False时则触发AssertionE
- Pycharm 作为一款针对 Python 的编辑器,配置简单、功能强大、使用起来省时省心,对初学者友好,这也是为什么编程教室一直推荐新手使
- python保存numpy数据:numpy.savetxt("result.txt", numpy_data);保存li
- 本文实例讲述了Python实现基本数据结构中栈的操作。分享给大家供大家参考,具体如下:#! /usr/bin/env python#codi