深度学习

卷积层

提取数据特征,矩阵点乘求和运算

import torch
from torch import nn
conv = nn.Conv2d(in_channels=1, out_channels=1, kernel_size=3,
                 stride=1, padding=1, dilation=1, groups=1, bias=True)
print(conv)
"""
output:
Conv2d(1,1, kernel_size=(3,3), stride=(1,1), padding=(1,1))
"""

print("weight:", conv.weight.data, '\n', conv.weight.shape)
"""
output:
weight: tensor([[[[ 0.2703, -0.2022, -0.2629],
                  [ 0.2879, -0.1953, -0.0796],
                  [ 0.0695, -0.1182, -0.2219]]]]) 
torch.Size([1, 1, 3, 3])
"""

print("bias:", conv.bias.data, '\n', conv.bias.shape)
"""
output:
bias: tensor([0.3176]) 
torch.Size([1])
"""

input = torch.ones(1, 1, 5, 5)
output= conv(input)
print(input.shape, output.shape)
"""
output:
torch.Size([1, 1, 5, 5]) torch.Size([1, 1, 5, 5])
"""

激活函数

逼近任意非线性函数,解决卷积运算无法形成复杂表达空间,难提取高语义信息

input= torch.randn(1,1,2,2)
print(input)
"""
output:
tensor([[[[0.3452, 0.0251],
          [0.2547, 2.3131]]]])
"""

sigmoid= nn.Sigmoid()
print("sigmoid:", sigmoid(input))
"""
output:
sigmoid: tensor([[[[0.5855, 0.5063],
                   [0.5633, 0.9100]]]])
"""

relu= nn.ReLU(inplace= True)
print("relu:", relu(input))
"""
output:
relu: tensor([[[[0.3452, 0.0251],
                [0.2547, 2.3131]]]])
"""

leakyrelu= nn.LeakyReLU(0.04, inplace=True)
print("leakyrelu:", leakyrelu(input))
"""
output:
leakyrelu: tensor([[[[0.3452, 0.0251],
                     [0.2547, 2.3131]]]])
"""

import torch.nn.functional as F
score= torch.randn(2, 4)
print("score:", score)
"""
output:
score: tensor([[ 0.7259,  0.5147, -0.5623,  1.5574],
               [-0.3192, -0.2654, -1.0786,  0.0291]])
"""

s= F.softmax(score, 1)
print("softmax:", s.sum(1))
"""
output:
softmax: tensor([1., 1.])
"""

池化层

降采样操作,降低参数量,增加感受野,是较强先验,使模型关注全局特征而非局部出现的位置,保留重要特征信息,提升容错能力,防止过拟合

# kernel_size, stride 池化区域 步长
max_pooling= nn.MaxPool2d(2, stride=2)
aver_pooling= nn.AvgPool2d(2, stride=2)
input= torch.randn(1, 1, 4, 4)
print(input)
print("max_pooling:", max_pooling(input))
print("aver_pooling:", aver_pooling(input))
"""
output:
tensor([[[[-0.0737,  0.3496, -0.1539,  0.6446],
          [ 0.2737, -0.1972,  0.6658, -0.1790],
          [-1.1142,  0.6309,  0.1658, -0.9450],
          [ 0.7880,  1.3876,  0.2205,  0.7212]]]])
max_pooling: tensor([[[[0.3496, 0.6658],
                       [1.3876, 0.7212]]]])
aver_pooling: tensor([[[[0.0881, 0.2444],
                        [0.4231, 0.0406]]]])
"""

Dropout正则化

解决参数过多训练样本又少而产生过拟合现象
训练时,每个神经元以概率p保留,使得模型不过于依赖局部特征,增强泛化能力

dropout= nn.Dropout(0.5, inplace=False)
input= torch.randn(2, 64, 7, 7)
#print(dropout(input))

BN层

标准化+线性变换
解决浅层参数变化经过多层线性变换与激活函数后会被放大,改变每一层的输入分布,造成深层网络需要不断调整适应,导致模型难收敛

bn= nn.BatchNorm2d(num_features= 64)  #num_features为通道数
input= torch.randn(4, 64, 224, 224)
#print(bn(input))

全连接层

每个节点与上下层所有节点相连,输入输出都被延展成一维向量

input= torch.randn(4, 1024)
linear= nn.Linear(1024, 4096)
output= linear(input)
print(input.shape, output.shape)
"""
output:
torch.Size([4, 1024]) torch.Size([4, 4096])
"""

空洞卷积

卷积核大小k,空洞数d
k'=k+ (k-1)*(d-1)

conv1= nn.Conv2d(3,256, 3, stride=1, padding=1, dilation=1)
print(conv1)
"""
output:
Conv2d(3, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
"""

conv2= nn.Conv2d(3,256, 3, stride=1, padding=1, dilation=2)
print(conv2)
"""
output:
Conv2d(3, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), dilation=(2, 2))
"""
posted @ 2024-11-01 13:13  sgqmax  阅读(7)  评论(0编辑  收藏  举报