当前位置:网站首页>转置卷积相关
转置卷积相关
2022-07-26 22:37:00 【Ap21ril】
卷积可以理解为压缩,转置卷积为解压缩。

重新排列输入和核



前言
卷积不会增大输出的高和宽,要么不变,要么减小。
转置卷积可以增大输出的高和宽,实现过程如下图所示。
思想
输入里的每一个元素与卷积核中的元素相乘,以2X2输入为例,相乘后会得到四个矩阵,让这四个矩阵相加就是输出。
输出可由以下公式得到:
K表示kernel,Y为输出,X为输入

代码实现
import torch
from torch import nn
from d2l import torch as d2l
def trans_conv(X,K):
h,w = K.shape
Y = torch.zeros((X.shape[0]+h-1,X.shape[1]+w-1))
for i in range(X.shape[0]):
for j in range(X.shape[1]):
Y[i:i+h,j:j+w] += X[i,j]*K
return Y
X = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
K = torch.tensor([[0.0, 1.0], [2.0, 3.0]])
trans_conv(X, K)

我们也可以调用pytorch中的API来实现,具体代码如下:
X, K = X.reshape(1, 1, 2, 2), K.reshape(1, 1, 2, 2) #批量大小,通道数,输入长,宽
tconv = nn.ConvTranspose2d(1, 1, kernel_size=2,bias=False)
tconv.weight.data = K
tconv(X)

步幅,填充,多通道
填充
常规卷积将填充应用在输入上,转置卷积则应用在输出上。当将高和宽两侧的填充数指定为1时,转置卷积的输出中将删除第一和最后的行与列。
当padding=0时,输出为:
当padding=1时,输出为:
把最外面的一行和一列删掉了。
代码实现如下:
tconv = nn.ConvTranspose2d(1, 1, kernel_size=2, padding=1, bias=False)
tconv.weight.data = K
tconv(X)
步幅
转置卷积中的步幅与卷积中并无差别,具体实例如下:
当步幅为1时:
当步幅为2时:
多通道
对于多个输入和输出通道,转置卷积与常规卷积以相同方式运作。 假设输入有ci个通道,且转置卷积为每个输入通道分配了一个kh×kw的卷积核张量。 当指定多个输出通道时,每个输出通道将有一个ci×kh×kw的卷积核。
同样,如果我们将X代入卷积层f来输出Y=f(X),并创建一个与f具有相同的超参数、但输出通道数量是X中通道数的转置卷积层g,那么g(Y)的形状将与X相同。 下面的示例可以解释这一点。
X = torch.rand(size=(1, 10, 16, 16))
conv = nn.Conv2d(10, 20, kernel_size=5, padding=2, stride=3)
tconv = nn.ConvTranspose2d(20, 10, kernel_size=5, padding=2, stride=3)
tconv(conv(X)).shape == X.shape

边栏推荐
- Design of alcohol detector based on 51 single chip microcomputer
- Dynamic memory management
- In depth interpretation of the investment logic of the consortium's participation in the privatization of Twitter
- Practice of data storage scheme in distributed system
- Design of electronic scale based on 51 single chip microcomputer
- [C language] classic recursion problem
- 会议OA项目排座功能以及送审功能
- Flink SQL (II) Kafka connector
- 数据库:MySQL基础+CRUD基本操作
- 简单的SQL优化
猜你喜欢

Chapter 1 Introduction and use skills of interceptors

4-4 object lifecycle

New features of ES6
![[Gorm] model relationship -hasone](/img/90/3069059ddd09dc538c10f76d659b08.png)
[Gorm] model relationship -hasone

Azure Synapse Analytics 性能优化指南(3)——使用具体化视图优化性能(下)

In simple terms, cchart daily lesson - happy high school lesson 57 new starting point, the old tree and new bud of colorful interface library

Complete backpack and 01 Backpack

At 12:00 on July 17, 2022, the departure of love life on June 28 was basically completed, and it needs to rebound

Recent answers - column

ResNet论文解读及代码实现(pytorch)
随机推荐
11_ Weather case - monitoring properties
10_ Name Case - Calculation attribute
15_ Key function and principle
13_ conditional rendering
3 esp8266 nodemcu network server
14_ Basic list
Baidu website Collection
Oracle remote connection configuration
第2章 开发用户流量拦截器
第6节:cmake语法介绍
Identity server4 authorization successful page Jump encountered an error: exception: correlation failed Solution of unknown location
Codeforces E. maximum subsequence value (greed + pigeon nest principle)
C语言数组
Upload files to the server
Recent answers - column
Pytorch learning record (II): tensor
Iptables prevent nmap scanning and binlog
In depth interpretation of the investment logic of the consortium's participation in the privatization of Twitter
Training team lpoj round10 d come minion!
Modulo (remainder) operation in the range of real numbers: how to find the remainder of negative numbers