pytorch学习笔记-自定义卷积

未完结的草稿---------!大概是准备整合一下常见的层,整合完感觉就可以进行搭建了(还没进行到这一步所以不太确定版)

(ps我将在完结这一篇的时候删除上面的小字and二编一下整篇文章的结构,如果看到了这部分文字也是很有缘分了/doge

这一部分感觉也没啥好说的= =

也就是reshape部分值得注意一下?剩下的感觉就是了解一下用法就可以

python 复制代码
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操作

python 复制代码
# 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))

对输入数据进行卷积

python 复制代码
# 使用自定义的卷积核对输入数据进行卷积操作
# 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])
相关推荐
智者知已应修善业21 小时前
【用74LS151的实现(16序列信号发生器)】2024-6-1
驱动开发·经验分享·笔记·硬件架构·硬件工程
「維他檸檬茶」21 小时前
大模型算法学习2026.6.13
学习·算法
代码续发21 小时前
AI Agent的学习记录
学习
是上好佳佳佳呀21 小时前
【LangChain|Day04】RAG 全流程基础笔记:Document 、 Loader 和 Splitter
笔记·langchain·rag
俊男无期1 天前
【无标题】
笔记·其他
m0_719084111 天前
es我的笔记
大数据·笔记·elasticsearch
努力写A题的小菜鸡1 天前
01-PyTorch加载数据初认识(dataset运用)
人工智能·pytorch·python
ken22321 天前
文本编辑器默认字体 收集
学习
H__Rick1 天前
C51学习-DAY6
单片机·嵌入式硬件·学习
YM52e1 天前
手写模型集合书籍鸿蒙PC ArkTS 对象字面量类型问题约束深度解析
学习·华为·harmonyos·鸿蒙