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设计】。

相关文章

django从请求到响应的过程深入讲解

django从请求到响应的过程深入讲解

django启动 我们在启动一个django项目的时候,无论你是在命令行执行还是在pycharm直接点击运行,其实都是执行'runserver'的操作,而ruserver是使用djan...

python实现剪切功能

python实现剪切功能

本文实例为大家分享了python实现剪切功能的具体代码,供大家参考,具体内容如下 #!/usr/bin/env python #coding: utf8 import sys m...

python列表生成式与列表生成器的使用

列表生成式:会将所有的结果全部计算出来,把结果存放到内存中,如果列表中数据比较多,就会占用过多的内存空间,可能会导致MemoryError内存错误或者导致程序在运行时出现卡顿的情况 列表...

python实现两个文件合并功能

python实现两个文件合并功能

本文将会分析一个文件合并的程序,并指出在合并文件过程中需要注意的问题。 下面是需要合并的文件示例: 分析思路: 要将两个文件合并,首先要将文件读到内存中,成为列表。再将列表...

python图像处理入门(一)

python图像处理入门(一)

一、环境 由于这学期开了图像处理这门课,所以想着在各种实验开始之前自己先动手试一下 图像处理那首先要配个环境嘛,配环境真的是我长久以来的噩梦了,每次都会出现奇奇怪怪的问题,首先上网查找了...