pytorch学习笔记-自定义卷积
未完结的草稿———!大概是准备整合一下常见的层,整合完感觉就可以进行搭建了(还没进行到这一步所以不太确定版)
(ps我将在完结这一篇的时候删除上面的小字and二编一下整篇文章的结构,如果看到了这部分文字也是很有缘分了/doge
这一部分感觉也没啥好说的= =
也就是reshape部分值得注意一下?剩下的感觉就是了解一下用法就可以
import torch
import torch.nn as nn
import torch.nn.functional as F# 输入数据(图片之类的是二维的),这里采用二维的数据
input = torch.tensor([[1, 2, 0, 3, 1],[0, 1, 2, 3, 1],[1, 2, 1, 0, 0],[5, 2, 3, 1, 1],[2, 1, 0, 1, 1]])# 定义卷积核
kernel = torch.tensor([[1, 2, 1],[0, 1, 0],[2, 1, 0]])
reshape操作
# input – input tensor of shape (minibatch,in_channels,iH,iW)
# 官网上对input的输入格式要求是上面这样的,显然我们输入的不满足要求,进行修正
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
对输入数据进行卷积
# 使用自定义的卷积核对输入数据进行卷积操作
# stride为步长,默认不进行padding
# 更多参数可以参见官方文档
output = F.conv2d(input, kernel, stride=1)print(output)
print(output.shape)# tensor([[[[10, 12, 12],
# [18, 16, 16],
# [13, 9, 3]]]])
# torch.Size([1, 1, 3, 3])