卷积层
此卷积非彼卷积,实际上为二维信号的互相关运算,使用scipy.signal.correlate(x, kernel, ‘valid’)进行运算
from util_pkg import *
from scipy import signal
import torch
from torch import nn
class Conv2D(nn.Module):
def __init__(self, kernel_size):
super().__init__()
self.kernel = nn.Parameter(torch.randn(kernel_size))
self.bias = nn.Parameter(torch.randn(1))
def forward(self, x):
return self.corr2d(x, self.kernel) + self.bias
@staticmethod
def corr2d(x, kernel):
H, W = x.shape
h, w = kernel.shape
ret = torch.zeros(H - h + 1, W - w + 1)
for i in range(ret.shape[0]):
for j in