寒假PyTorch工具第六天

课程记录

从卷积层到线性层到激活函数


课程代码


作业

1.      采用1个尺寸为2*2的卷积核对2通道的3*3图像进行卷积,padding=0, stride=1,dilation=0

其中 input shape = (3, 3, 2), kernel size = 2*2, 第一个卷积核所有权值均为1,

计算输出的feature map尺寸以及所有像素值

卷积操作

import torch 
import numpy as np 
import torch.nn as nn


a = torch.tensor([[[1, 1], [2, 2], [3, 3]], [[4, 4], [5, 5], [6, 6]], [[7, 7], [8, 8], [9, 9]]], dtype=torch.float64) # 3 * 3 * 2
a = a.transpose(1, 2)
a = a.transpose(0, 1)
a = a.unsqueeze(dim = 0)

print(a, a.shape)

conv_layer = nn.Conv2d(in_channels = 2, out_channels = 1, kernel_size = (2, 2), bias = 0)
print('weight:', conv_layer.weight.shape, conv_layer.weight)

conv_weight_set = torch.ones((1, 2, 2, 2), dtype = torch.float64)
conv_layer.weight.data = conv_weight_set

b = conv_layer(a)

print('weight:', conv_layer.weight.shape, conv_layer.weight)
print('bias:', conv_layer.bias)
print('out:', b)

猜你喜欢

转载自blog.csdn.net/u013625492/article/details/114230938