Pytorch之卷积层的使用详解

yipeiwu_com6年前Python基础

1.简介(torch.nn下的)

卷积层主要使用的有3类,用于处理不同维度的数据

参数 Parameters:

in_channels(int) – 输入信号的通道

out_channels(int) – 卷积产生的通道

kerner_size(int or tuple) - 卷积核的尺寸

stride(int or tuple, optional) - 卷积步长

padding (int or tuple, optional)- 输入的每一条边补充0的层数

dilation(int or tuple, `optional``) – 卷积核元素之间的间距

groups(int, optional) – 从输入通道到输出通道的阻塞连接数

bias(bool, optional) - 如果bias=True,添加偏置

class torch.nn.Conv1d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

一维卷积层。用于计算ECG等一维数据。

input: (N,C_in,L_in) N为批次,C_in即为in_channels,即一批内输入一维数据个数,L_in是是一维数据基数

output: (N,C_out,L_out) N为批次,C_in即为out_channels,即一批内输出一维数据个数,L_out是一维数据基数

class torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

二维卷积层。用于计算CT断层或MR断层,或二维超声图像,自然图像等二维数据。

self.conv1 = nn.Conv2d( # 1*28*28 -> 32*28*28
      in_channels=1,
      out_channels=32,
      kernel_size=5,
      stride=1,
      padding=2 #padding是需要计算的,padding=(stride-1)/2
    )

input: (N,C_in,H_in,W_in) N为批次,C_in即为in_channels,即一批内输入二维数据个数,H_in是二维数据行数,W_in是二维数据的列数

output: (N,C_out,H_out,W_out) N为批次,C_out即为out_channels,即一批内输出二维数据个数,H_out是二维数据行数,W_out是二维数据的列数

con2 = nn.Conv2d(1,16,5,1,2)
# con2(np.empty([1,1,28,28])) 只能接受tensor/variable
con2(torch.Tensor(1,1,28,28))
con2(Variable(torch.Tensor(1,1,28,28)))

class torch.nn.Conv3d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

三维卷积层。用于计算CT或MR等容积数据,视频数据等三维数据。

input: (N,C_in,D_in,H_in,W_in)

output: (N,C_out,D_out,H_out,W_out)

2.简介(torch.nn.functional下的)

在torch.nn.functional下也有卷积层,但是和torch.nn下的卷积层的区别在于,functional下的是函数,不是实际的卷积层,而是有卷积层功能的卷积层函数,所以它并不会出现在网络的图结构中。

torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

参数:

- input – 输入张量的形状 (minibatch x in_channels x iW)

- weight – 过滤器的形状 (out_channels, in_channels, kW)

- bias – 可选偏置的形状 (out_channels)

- stride – 卷积核的步长,默认为1

>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

>>> # With square kernels and equal stride
>>> filters = autograd.Variable(torch.randn(8,4,3,3))
>>> inputs = autograd.Variable(torch.randn(1,4,5,5))
>>> F.conv2d(inputs, filters, padding=1)

torch.nn.functional.conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

>>> filters = autograd.Variable(torch.randn(33, 16, 3, 3, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50, 10, 20))
>>> F.conv3d(inputs, filters)

以上这篇Pytorch之卷积层的使用详解就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

关于python导入模块import与常见的模块详解

0.什么是python模块?干什么的用的? Java中如果使用abs()函数,则需要需要导入Math包,同样python也是封装的,因为python提供的函数太多,所以根据函数的功能将其...

Python ldap实现登录实例代码

下面一段代码是小编给大家介绍的Python ldap实现登录实例代码,一起看看吧 ldap_config = { 'ldap_path': 'ldap://xx.xx.xx.xx...

python中defaultdict的用法详解

初识defaultdict 之前在使用字典的时候, 用的比较随意, 只是简单的使用dict. 然而这样在使用不存在的key的时候发生KeyError这样的一个报错, 这时候就该defa...

TensorFlow基于MNIST数据集实现车牌识别(初步演示版)

TensorFlow基于MNIST数据集实现车牌识别(初步演示版)

在前几天写的一篇博文《如何从TensorFlow的mnist数据集导出手写体数字图片》中,我们介绍了如何通过TensorFlow将mnist手写体数字集导出到本地保存为bmp文件。 车牌...

Python标准库06之子进程 (subprocess包) 详解

这里的内容以Linux进程基础和Linux文本流为基础。subprocess包主要功能是执行外部的命令和程序。比如说,我需要使用wget下载文件。我在Python中调用wget程序。从这...