IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> Pytorch学习笔记(9)———基本的层layers -> 正文阅读

[人工智能]Pytorch学习笔记(9)———基本的层layers

卷积神经网络常见的层

类型名称作用
Conv卷积层提取特征
ReLU激活层激活
Pool池化——
BatchNorm批量归一化——
Linear(Full Connect)全连接层——
Dropout————
ConvTranspose反卷积——

pytorch中各种层的用法

卷积 Convolution

卷积类型作用
torrch.nn.Conv1d一维卷积
torch.nn.Conv2d二维卷积
torch.nn.Conv3d三维卷积
torch.nn.ConvTranspose1d一维反卷积
torch.nn.ConvTranspose2d二维反卷积
torch.nn.ConvTranspose3d三维反卷积
m = nn.Conv1d(in_channels=16,
              out_channels=33,
              kernel_size=3,
              padding=1,
              stride=1)
input = torch.randn(1, 16, 50)
output = m(input)
print(output.size())

# nn.Conv2d()
m = nn.Conv2d(in_channels=16,
              out_channels=33,
              kernel_size=3,
              stride=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)
print(output.size())


m = nn.Conv2d(in_channels=16,
              out_channels=33,
              kernel_size=(3, 5),
              stride=(2, 1),
              padding=(4, 2),
              dilation=(3, 1))
output = m(input)
print(output.size())

# nn.ConvTranspose2d()
# 2d transpose convolution operator
m = nn.ConvTranspose2d(in_channels=16,
                       out_channels=33,
                       kernel_size=3,
                       stride=2)
input = torch.randn(20, 16, 50, 100)
output = m(input)   # (20,33,101,201)
print(output.size())

# exact output size can be also specified as an argument
input = torch.randn(1, 16, 12, 12)
downsample = nn.Conv2d(in_channels=16,
                       out_channels=16,
                       kernel_size=3,
                       stride=2,
                       padding=1)
upsample = nn.ConvTranspose2d(in_channels=16,
                              out_channels=16,
                              kernel_size=3,
                              stride=2,
                              padding=1,
                              output_padding=1)
output = downsample(input)  # 下采样, (1x16x6x6)
print(output.size())

output = upsample(output)   # 上采样, (1x16x12x12)
print(output.size())


对图像进行卷积操作

  • 图像读取: 采用PIL.Image读取
  • Tensor转换: 采用torch.from_numpy() 将图像转为Tensor
  • 维度变换: 采用 tensor.squeeze() 进行降维度, tensor.unsqueeze()升维
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
# 载入图像
image = Image.open('./data/lena_gray.jpg')
# 图像转换为Tensor

dir(torch)
torch.set_default_dtype(torch.float)

x = torch.from_numpy(np.array(image, dtype=np.float32))
print(x.size())
# 4D Tensor
x = x.unsqueeze(0)
x = x.unsqueeze(0)
print(x.size())

filter1 = torch.tensor([[-1, 0, 1],
                        [-2, 0, 2],
                        [-1, 0, 1]], dtype=torch.float32)
filter1 = filter1.unsqueeze(0)
filter1 = filter1.unsqueeze(0)
out = F.conv2d(x, filter1)
print(out.size())

out = out.squeeze(0)
out = out.squeeze(0)

plt.imshow(out.numpy().astype(np.uint8), cmap='gray')
plt.axis('off')
plt.show()

池化层 Pooling

一般池化采用这两种方法:最大池化和平均池化

pytorch中提供的池化API:

# https://pytorch.org/docs/stable/nn.html#pooling-layers
# torch.nn.MaxPool1d/2d/3d
# torcn.nn.MaxUnpool1d/2d/3d
# torch.nn.AvgPool1d/2d/3d
# torch.nn.FractionMaxPool2d
# torch.nn.AdaptiveMaxPool1d/2d/3d
# torch.nn.AdaptiveAvgPool1d/2d/3d
# nn.MaxPool2d()
m = nn.MaxPool2d(kernel_size=3, stride=2)
input = torch.randn(20, 16, 50, 32)
output = m(input)
print(output.size())    # (20,16, 24, 15)

m = nn.MaxPool2d(kernel_size=(3, 2), stride=(2, 1))
output = m(input)
print(output.size())    # (10,16,24,31)

Dropout层

Dropout层的特点: 输出Tensor与输出Tensor尺寸相同(Output is of the same shape as input)

计算过程: tensor_out = 1/(1-p) * tensor_input

# torch.nn.Dropout
# torch.nn.Dropout2d
# torch.nn.Dropout3d
# torch.nn.AlphaDropout
m = nn.Dropout(p=0.2, inplace=False)
input = torch.randn(1, 5)
output = m(input)
print('input:', input, '\n',
      output, output.size())

m = nn.Dropout2d(p=0.2)
input = torch.randn(1, 1, 5, 5)
output = m(input)
print(output, output.size())

1* 1/(1-0.2) = 1.25
在这里插入图片描述

全连接层 / 线性层 Linear

import torch
import torch.nn as nn
import torch.nn.functional as F


# https://pytorch.org/docs/stable/nn.html#linear-layers
# torch.nn.Linear()
# torch.nn.Bilinear()   # 双线性变换

# torch.nn.Linear()
m = nn.Linear(in_features=20, out_features=30)
input = torch.randn(128, 20)
output = m(input)
print(output.size())    # (128, 30)

# torch.nn.Bilinear()----???
# Applies a bilinear transformation to the incoming data
m = nn.Bilinear(in1_features=20, in2_features=30, out_features=40)
input1 = torch.randn(128, 20)
input2 = torch.randn(128, 30)
output = m(input1, input2)
print(output.size())    # (128, 40)

Normalization层

mport torch
import torch.nn as nn


# https://blog.csdn.net/wzy_zju/article/details/81262453
# https://pytorch.org/docs/stable/nn.html#normalization-layers

# torch.nn.BatchNorm1d/2d/3d
# torch.nn.GroupNorm
# torch.nn.InstanceNorm1d/2d/3d
# torch.nn.LayerNorm
# torch.LocalResponseNorm  (LRN)

# nn.BatchNorm1d (same shape as input)
# affine=False,  without Learnable Parameters
m = nn.BatchNorm1d(num_features=10, affine=False)
input = torch.randn(20, 10)
output = m(input)
print(output, output.size())


# nn.BatchNorm2d  (same shape as input)
# affine=True, with learnable parameters
m = nn.BatchNorm2d(num_features=100, affine=True)
input = torch.randn(20, 100, 35, 45)
output = m(input)
print(output.size())

链接:https://www.jianshu.com/p/343e1d994c39

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2021-09-12 13:09:55  更:2021-09-12 13:10:32 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 20:59:54-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码