RuntimeError Given groups=1, weight of size [6, 5, 3, 3], expected input[4, 2, 6, 6] to have…及问题解决

RuntimeError: Given groups=1, weight of size [64, 512, 3, 3], expected input[4, 2048, 66, 66] to have 512 channels, but got 2048 channels instead.

错误分析

Given groups=1 weights of size [64, 512, 3, 3]

代表要进行的卷积操作的卷积核的channel 大小为从521到64 ,大小为1*1

expected input [4, 2048, 66, 66] to have 512 channels

代表输入的要卷积的feature的大小,channel为2048。 其实我们期望的输入feature大小channel 为 512 个通道,因为卷积操作的channel是521。

but got 16 channel instead

代表我们得到16个channels 的feature 与预期64个channel不一样。
———————————————————————————————————————
首先要查看一下过程涉及的张量的大小:

print(tensor1.shape)
or
print(tensor1.size())

输出的是张量的各个维度的大小,类型是 tensor.size()
但是可以用

tensor1.shape[0]

来索引出此张量第几维度的大小,返回值类型是int
———————————————————————————————————————
介绍一下卷积操作:
卷积层可以理解为函数,本质是类

torch.nn.Conv2d(in_channels, out_channels, kernel_size, stride=1, padding=0, dilation=1, groups=1, bias=True)

参数
in_channels ( int ) – 输入图像中的通道数

out_channels ( int ) – 卷积产生的通道数

kernel_size ( int or tuple ) – 卷积核的大小

stride ( int or tuple , optional ) – 卷积的步幅。默认值:1

padding ( int , tuple或str , optional ) – 添加到输入的所有四个边的填充。默认值:0

padding_mode (字符串,可选) – ‘zeros’, ‘reflect’, ‘replicate’或’circular’. 默认:’zeros’

dilation ( int or tuple , optional ) – 内核元素之间的间距。默认值:1

groups ( int , optional ) – 从输入通道到输出通道的阻塞连接数。默认值:1

bias ( bool , optional ) – If True,向输出添加可学习的偏差。默认:True

Original: https://blog.csdn.net/qq_43507388/article/details/124266512
Author: 当像鸟飞向你的山
Title: RuntimeError Given groups=1, weight of size [6, 5, 3, 3], expected input[4, 2, 6, 6] to have…及问题解决

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/518245/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球