未完結的草稿———!大概是準備整合一下常見的層,整合完感覺就可以進行搭建了(還沒進行到這一步所以不太確定版)
(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])