厦门旅游网站设计,电影项目做产品众筹哪个网站好,营销师,百度收录网站的图片文章目录 省流异常报错异常截图异常代码原因解释修正代码执行结果 省流 nn.Conv2d 需要的输入张量格式为 (batch_size, channels, height, width)#xff0c;但您的示例输入张量 x 是 (batch_size, height, width, channels)。因此#xff0c;需要对输入张量进行转置。 注意… 文章目录 省流异常报错异常截图异常代码原因解释修正代码执行结果 省流 nn.Conv2d 需要的输入张量格式为 (batch_size, channels, height, width)但您的示例输入张量 x 是 (batch_size, height, width, channels)。因此需要对输入张量进行转置。 注意TensorFlow使用NHWC批次、高度、宽度、通道格式而PyTorch使用NCHW批次、通道、高度、宽度格式
异常报错
RuntimeError: Given groups1, weight of size [16, 3, 2, 3],
expected input[8, 65, 66, 3] to have 3 channels,
but got 65 channels instead异常截图 异常代码
def down_shifted_conv2d(x , num_filters , filters_size [2,3],stride 1, **kwargs):batch_size,H,W,channels x.shapepadding (0,0,int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),int(filters_size[0]) - 1 , 0,0,0)x_paded nn.functional.pad(x, padding)print(x_paded.shape)conv_layer nn.Conv2d(in_channelschannels, out_channelsnum_filters, kernel_sizefilters_size,stridestride, **kwargs)return conv_layer(x_paded)
# Example usage
x torch.randn(8, 64, 64, 3) # Example input with batch size 8, height and width 64, and 3 channels
num_filters 16
output down_shifted_conv2d(x, num_filters)
print(output.shape)原因解释 在pytorch中“nn.Conv2d”需要输入的张量格式为batch_size,channels,height,width原图输入的x的格式是batch_size,height ,weight,channel所以需要对tensor进行转置。 矩阵交换维度的函数permute按照编号将新的顺序填好即可。
def down_shifted_conv2d(x , num_filters , filters_size [2,3], stride 1, **kwargs):batch_size, H, W, channels x.shape# Transpose the input tensor to (batch_size, channels, height, width)x x.permute(0, 3, 1, 2)# Paddingpadding (int((filters_size[1] - 1) / 2), int((filters_size[1] - 1) / 2),filters_size[0] - 1, 0)x_paded F.pad(x, padding)修正代码
def down_shifted_conv2d(x , num_filters , filters_size [2,3],stride 1, **kwargs):batch_size,H,W,channels x.shape# 按照顺序对4个维度分别进行填充padding (0,0,int(((filters_size[1]) - 1) / 2 ) , int((int(filters_size[1]) - 1) / 2),int(filters_size[0]) - 1 , 0,0,0)x_paded nn.functional.pad(x, padding)x_paded x_paded.permute(0,3,1,2)# 进行卷积conv_layer nn.Conv2d(in_channelschannels, out_channelsnum_filters, kernel_sizefilters_size,stridestride, **kwargs)return conv_layer(x_paded)
# Example usage
x torch.randn(8, 64, 64, 3)
num_filters 16
output down_shifted_conv2d(x, num_filters)
print(output.shape)执行结果