当前位置:网站首页>Hands on deep learning - multiple input and output channels in the convolution layer
Hands on deep learning - multiple input and output channels in the convolution layer
2022-06-11 17:22:00 【Orange acridine 21】
1、 Multiple input channels
Color images may have RGB Three channels , Converting to grayscale will lose information .
Each channel has a convolution kernel , The result is the sum of the convolution results of all channels .

Expressed by formula :

2、 Multiple output channels
No matter how many input channels there are , So far, we only use single output channel ; We can have multiple three-dimensional convolution kernels , Each core generates an output channel .

Each output channel can recognize a specific pattern ; The input channel kernel identifies and combines patterns in the input .
3、1x1 The convolution of layer
kh=kw=1 Is a popular choice , He doesn't recognize spatial patterns , It's just a fusion channel ;
1x1 The convolution layer can also be called the full connection layer ;
import torch
from torch import nn
import sys
sys.path.append("..")
import d2lzh_pytorch as d2l
""" Multiple input channels
Realize the cross-correlation operation with multiple input channels . We just need to do cross-correlation calculation for each channel , And then through add_n Function to accumulate
"""
def corr2d_multi_in(X, K):
# Along X and K Of the 0 dimension ( Channel dimension ) Calculate separately and add up
res = d2l.corr2d(X[0, :, :], K[0, :, :])
for i in range(1, X.shape[0]):
res += d2l.corr2d(X[i, :, :], K[i, :, :])
return res
# Construct an array x And an array k To verify the output of the cross-correlation operation
X = torch.tensor([[[0, 1, 2], [3, 4, 5], [6, 7, 8]],[[1, 2, 3], [4, 5, 6], [7, 8, 9]]])
K = torch.tensor([[[0, 1], [2, 3]], [[1, 2], [3, 4]]])
print(corr2d_multi_in(X, K))
""" Multiple output channels
Implement a cross-correlation function to calculate the output of multiple channels
"""
def corr2d_multi_in_out(X, K):
# Yes K Of the 0 Dimension traversal , Simultaneous transmission each time ⼊X Do cross correlation calculations . All the results make ⽤stack Functions are merged in ⼀ rise
return torch.stack([corr2d_multi_in(X, k) for k in K])
K = torch.stack([K, K + 1, K + 2]) # Construct a convolution kernel with three channels
print(K.shape)
# There are three channels at the output
print(corr2d_multi_in_out(X, K))
"""1x1 Convolution layer
Window size is 1x1 Multichannel convolution , transport ⼊ And output have the same ⾼ And width . Each element in the output to ⾃ transport ⼊ In the ⾼ And the weight of elements in the same position on the width between different channels ᯿ Add up .
Suppose we treat the channel dimension as a feature dimension , take ⾼ And elements in wide dimensions as data samples , that Convolution layer ⽤ Equivalent to the full connection layer .
"""
# Use the matrix multiplication in the full connection layer to realize 1x1 Convolution .
def corr2d_multi_in_out_1x1(X, K):
c_i, h, w = X.shape
c_o = K.shape[0]
X = X.view(c_i, h * w)
K = K.view(c_o, c_i)
Y = torch.mm(K, X) # Matrix multiplication of full connection layer
return Y.view(c_o, h, w)
X = torch.rand(3, 3, 3)
K = torch.rand(2, 3, 1, 1)
Y1 = corr2d_multi_in_out_1x1(X, K)
Y2 = corr2d_multi_in_out(X, K)
print((Y1 - Y2).norm().item() < 1e-6)
边栏推荐
- Message queue push / pull mode Learning & ActiveMQ and JMS learning
- Create database instance
- Is the stock account recommended by qiniu safe? Is it reliable
- [pytest learning] after the pytest case fails to execute, the others will not be executed
- Global and Chinese molten carbonate fuel cell industry outlook and market panoramic Research Report 2022-2028
- 导出数据提示--secure-file-priv选项问题的解决方法
- Talk about the interview questions of the collection
- 一套ThinkPHP微信小程序商城源码带后台管理
- What subclasses inherit, polymorphism, and upward transformation
- Characteristics of interfaces & comparison between interfaces and abstract classes
猜你喜欢

搜狐全員遭詐騙,暴露哪些問題?

How to become an optimist organization?

Global and China Mobile Network Optimization (MnO) industry development panoramic survey and Investment Strategy Research Report 2022-2028

满k叉树编号为 i 的节点的孩子编号公式推导

Derivation of child numbering formula for nodes numbered I in full k-ary tree

ShellBrowser . NET Crack

Custom or subscription? What is the future development trend of China's SaaS industry?

10 times faster than 5g. Are you ready for 10 Gigabit communication?

DFS和BFS笔记(一)基于C语言的广度优先搜索

Tornado environment construction and basic framework construction -- familiar Hello World
随机推荐
How to become an optimist organization?
LeetCode——24. Exchange the nodes in the linked list in pairs (three pointers)
Vscode automatic eslint formatting when saving code
Derivation of child numbering formula for nodes numbered I in full k-ary tree
ShellBrowser . NET Crack
Le compte de titres de l'école de commerce kainiu est - il sécurisé? Frais d'ouverture de compte
RecyclerView缓存复用解析,源码解读
Create database instance
Message queue push / pull mode Learning & ActiveMQ and JMS learning
二级造价工程师值得考吗?发展前景如何?
从制造到“智造”,探索制造企业破局之道
Error: error summary of pointer as function parameter
DFS和BFS笔记(一)基于C语言的广度优先搜索
Characteristics of interfaces & comparison between interfaces and abstract classes
Katalon Studio Enterprise
AXI协议基础知识
Meituan won the first place in fewclue in the small sample learning list! Prompt learning+ self training practice
golang中的异常处理和异常使用
Typescript learning notes (II)
Learning C language from scratch day 039