Pytorch学习笔记(三)

一、torch.nn

这里不讲解关于卷积神经网络的内容了,对这方面不了解的朋友可以自己搜索学习一下啦!

1、Module

使用一个例子来说明如何使用Module

class Test(nn.Module):  #创建的类需要继承Module类
	def __init__(self):
		super().__init__()  #初始化函数中一定要调用父类的初始化函数
	
	def forward(self, input):
		output = input + 1
		return output

test = Test()
x = torch.tensor(1.0)
output = test(x)  #这里调用了forward函数
print(output)

2、Conv2d

主要参数:
in_channels:输入的通道数
out_channels:输出通道数
kernel_size:卷积核的大小
stride:卷积过程中的步的大小
padding:对输入图像的边缘进行填充
padding_mode:对图像进行padding时选择的填充方式
dilation:卷积过程中核中元素之间的距离(空洞卷积)

dataset = torchvision.datasets.CIFAR10("./dataset", train=False, transform=torchvision.transforms.ToTensor(),download=True)
dataloader = DataLoader(dataset, batch_size=64)

class Test(nn.Module):
	def __init__(self):
		super(Test, self).__init__()
		self.conv1 = Conv2d(in_channel=3, out_channel=6, kernel_size=3, stride=1, padding=0)
		#卷积核内的值是自动生成的不需要进行设置
		
	def forward(self, x):
		self.conv1(x)
		return x

test = Test()
writer = SummaryWriter("logs")
for data in dataloader:
	imgs, target = data
	output = test(imgs)
	print(output.shape)  #此时观察输出的大小,channel已经变成了6
	writer.add_images("input", imgs, step)
	#output此时的channel变成6,因此直接输出会报错,需要reshape->[xxx, 3, 30, 30]
	torch.reshape(output, (-1, 3, 30, 30))
	writer.add_images("output", output, step)
	step = step + 1

3、Pooling layers——MaxPool2d

主要参数:
kernel_size:核的大小
stride:池化过程中的步的大小
padding:对输入图像的边缘进行填充

dataset = torchvision.datasets.CIFAR10("./dataset", train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64)

class Test(nn.Module):
	def __init__(self):
		super(Test, self).__init__()
		self.maxpool1 = MaxPool2d(kernel_size=3, ceil_mode=True)  #这里ceil_mode是指,在进行stride步数移动kernel时,如果每行或每列最后一次移动后如果kernel对应的图片部分比kernel的大小要小,是否要舍掉
	def forward(self, input):
		output = self.maxpool1(input)
		return output

test = Test()

writer = SummaryWriter("logs_maxpool")
step = 0
for data in dataloader:
	imgs, target = data
	writer.add_images("input", imgs, step)
	output = test(imgs)
	#经过池化层的输出channel没有改变,所以不需要reshape
	writer.add_images("output", output, step)
	step = step + 1

writer.close()	

4、非线性激活函数——Relu

非线性激活函数有很多,可以通过官方文档查看,大部分使用方法是一样的,用Relu来演示一下使用方法

class Test(nn.Module):
	def __init__(self):
		super(Test, self).__init__()
		self.relu1 = ReLU()
	
	def forward(self, input):
		output = self.relu1(input)
		return output

5、线性层和其它层

dataset = torchvision.datasets.CIFAR10("./dataset", train=False, download=True, transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset, batch_size=64, drop_last=True)

class Test(nn.Module):
	def __init__(self):
		super(Test, self).__init__()
		self.linear = Linear(196608, 10)
		#图片为64*3*32*32,将图片展平变成1*1*1*196608
	def forward(self, input):
		output = self.linear1(input)
		return output

test = Test()
for data in dataloader:
	imgs, target = data
	print(img.shape)
	output = torch.flatten(imgs)  #将输入展平
	print(output.shape)
	output = test(output)
	print(output.shape)

6、搭建神经网络以及Sequential层

CIFAR10网络模型搭建

class Test(nn.Module):
	def __init__(self):
		super(Test, self).__init__()
		#使用Sequential提高代码的简洁可读性
		self.model1 = Sequential{
			Conv2d(3, 32, 5, padding=2),
			MaxPool2d(2),
			Conv2d(32, 64, 5, padding=2),
			MaxPool2d(2),
			Conv2d(32, 64, 5, padding=2),
			MaxPool2d(2),
			Flatten(),
			Linear(1024, 64),
			Linear(64, 10)
		)

	def forward(self, x):
		x = self.model1(x)
		return x

CIFAR10模型
图片来源于网络

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值