#!/usr?bin/env/ python
# -*- coding:utf-8 -*-
# author: lai zheng laing
# datetime: 2020/10/29 15:27
# software: PyCharm
import torch
input = [3,4,6,5,7,
2,4,6,8,2,
1,6,7,8,4,
9,7,4,6,2,
3,7,5,4,1]
input = torch.Tensor(input).view(1,1,5,5) # view的功能 Bitch_size Channel W H转为一个张量
conv_layer = torch.nn.Conv2d(1,1,kernel_size=3,stride =2,padding=1,bias=False) # 3*3卷积核,padding =1 填充1圈0,
# bias偏置量,stride=2是步长
kernel = torch.Tensor([1,2,3,4,5,6,7,8,9]).view(1,1,3,3) # 构造卷积核,改变形状得到张量,INPUT OUTPUT W H
conv_layer.weight.data = kernel.data # 赋予卷积层的权重.data 卷积核.data变成张量,卷积层的权重做初始化
output =conv_layer(input)
print(output)
卷积神经网络2
最新推荐文章于 2024-08-13 17:25:37 发布