【torch】自定义卷积权重

自定义卷积权重

  • 在这里插入图片描述
import torch
import torch.nn as nn
import torch.nn.functional as F


class CNN(nn.Module):
    def __init__(self,
                 in_channels=1,
                 out_channels=2,
                 kernel=3,
                 stride=1,
                 padding=0,
                 weights=None,
                 bias=None):
        super(CNN, self).__init__()
        self.stride = stride
        self.padding = padding
        if weights is not None:
            assert weights.shape[0] == out_channels and weights.shape[
                1] == in_channels and weights.shape[
                    3] == kernel, 'please check the weight shape'
            self.weight = nn.Parameter(torch.tensor(weights))
        else:
            self.weight = nn.Parameter(
                torch.randn(out_channels, in_channels, kernel,
                            kernel))  # 自定义的权值
        if bias is not None:
            self.bias = nn.Parameter(torch.tensor(bias))
        else:
            self.bias = nn.Parameter(torch.randn([out_channels]))  # 自定义的偏置

    def forward(self, x):
        out = F.conv2d(input=x,
                       weight=self.weight,
                       bias=self.bias,
                       stride=self.stride,
                       padding=self.padding)
        return out


def run_conv(in_channel=1,
             out_channel=2,
             input=None,
             weight=None,
             bias=None,
             kernel=3,
             stride=1,
             padding=0):
    assert input is not None, 'check input first'
    input = torch.tensor(input, dtype=torch.float)
    if weight is not None:
        weight = torch.unsqueeze(torch.tensor(weight, dtype=torch.float), 1)

    if bias is not None:
        bias = torch.tensor(bias, dtype=torch.float)

    conv = CNN(in_channels=in_channel,
               out_channels=out_channel,
               kernel=kernel,
               stride=stride,
               padding=padding,
               weights=weight,
               bias=bias)

    return conv(input)


if __name__ == '__main__':
    # 这里有个bug,最好是out_channel=1,否则,未知原因,第二个channel的值算不出来
    kernel = 3
    stride = 1
    # 对应valid、same、full卷积的padding
    pad = [0, kernel // 2, max(kernel - 1, 0)]
    input = [[[[5, 6, 0, 1, 8, 2], [0, 9, 8, 4, 6, 5], [2, 6, 5, 3, 8, 4],
               [6, 3, 4, 9, 1, 0], [7, 5, 9, 1, 6, 7], [2, 5, 9, 2, 3, 7]]]]

    bias = [0]
    weight = [[[-1, -1, -1], [-1, 9, -1], [-1, -1, -1]],
              [[0, -1, 1], [1, 0, 0], [0, -1, 1]]]
    for p in pad:
        for w in weight:
            out = run_conv(out_channel=1,
                           in_channel=1,
                           input=input,
                           weight=[w],
                           bias=bias,
                           stride=stride,
                           kernel=kernel,
                           padding=p)
            print(out)
参考:
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

椰子奶糖

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值