pytorch中的unsqueez_和keras中的epxand_dim有什么区别,使用它后输出的形状是什么?

nad*_*dia 1 python keras pytorch

我是 keras 的初学者,我有一个 pytorch 代码,我需要将其更改为 keras,但我无法理解其中的某些部分。特别是我在输出形状的大小方面遇到问题。的形状image是 (:, 3,32,32) ,第一个维度image是批次的大小。现在,我的问题是:这条线的作用是什么以及输出形状是什么:

    image_yuv_ch = image[:, channel, :, :].unsqueeze_(1)
Run Code Online (Sandbox Code Playgroud)

它在位置 1 处添加了一个维度?输出形状是什么?:( 过滤器的大小是 (64,8,8) 然后我们有,这是否意味着isfilters.unsqueez_(1)的新形状?这条线的作用是什么? 它与 keras 中的 conv2d 相同吗?它的输出张量的形状?我也无法理解视图的作用?我知道它试图以新的形状显示张量,但在下面的代码中我无法理解每个 或 后的输出形状。您能告诉我什么吗是每条线的输出形状吗?先谢谢你了。filters(64,1,8,8)image_conv = F.conv2d(image_yuv_ch, filters, stride=8)unsqueez_permuteview

import torch.nn.functional as F
def apply_conv(self, image, filter_type: str):



        if filter_type == 'dct':
            filters = self.dct_conv_weights
        elif filter_type == 'idct':
            filters = self.idct_conv_weights
        else:
            raise('Unknown filter_type value.')

        image_conv_channels = []
        for channel in range(image.shape[1]):
            image_yuv_ch = image[:, channel, :, :].unsqueeze_(1)
            image_conv = F.conv2d(image_yuv_ch, filters, stride=8)
            image_conv = image_conv.permute(0, 2, 3, 1)
            image_conv = image_conv.view(image_conv.shape[0], image_conv.shape[1], image_conv.shape[2], 8, 8)
            image_conv = image_conv.permute(0, 1, 3, 2, 4)
            image_conv = image_conv.contiguous().view(image_conv.shape[0],
                                                  image_conv.shape[1]*image_conv.shape[2],
                                                  image_conv.shape[3]*image_conv.shape[4])

            image_conv.unsqueeze_(1)

            # image_conv = F.conv2d()
            image_conv_channels.append(image_conv)

        image_conv_stacked = torch.cat(image_conv_channels, dim=1)

        return image_conv_stacked
Run Code Online (Sandbox Code Playgroud)

Dav*_* Ng 5

看来您是 Keras 用户或 Tensorflow 用户,并且正在尝试学习 Pytorch。您应该访问Pytorch 文档的网站来了解有关每个操作的更多信息。

  • unsqueeze是将张量的dim扩大1倍。中的下划线unsqueeze_()表示这是in-place函数。
  • view()可以像keras中那样理解.reshape()
  • permute()就是切换张量的多个维度。例如:
x = torch.randn(1,2,3) # shape [1,2,3]
x = torch.permute(2,0,1) # shape [3,1,2]
Run Code Online (Sandbox Code Playgroud)

为了知道每次运算后张量的形状,只需简单地添加print(x.size())。例如:

image_conv = image_conv.permute(0, 2, 3, 1)
print(image_conv.size())

image_conv = image_conv.view(image_conv.shape[0], image_conv.shape[1], 
print(image_conv.size())

image_conv.shape[2], 8, 8)
print(image_conv.size())

image_conv = image_conv.permute(0, 1, 3, 2, 4)
print(image_conv.size())

Run Code Online (Sandbox Code Playgroud)

Pytorch 和 Tensorflow(Keras 后端)之间的最大区别在于 Pytorch 将生成动态图,而不是像 Tensorflow 那样生成静态图。您定义模型的方式在 Pytorch 中无法正常工作,因为在反向传播期间无法优化的权重conv不会被保存。model.parameters()

还有一条评论,请查看此链接以了解如何使用 Pytorch 定义正确的模型:

image_conv = image_conv.permute(0, 2, 3, 1)
print(image_conv.size())

image_conv = image_conv.view(image_conv.shape[0], image_conv.shape[1], 
print(image_conv.size())

image_conv.shape[2], 8, 8)
print(image_conv.size())

image_conv = image_conv.permute(0, 1, 3, 2, 4)
print(image_conv.size())

Run Code Online (Sandbox Code Playgroud)

评论的代码:

import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5)
        self.conv2 = nn.Conv2d(20, 20, 5)

    def forward(self, x):
       x = F.relu(self.conv1(x))
       return F.relu(self.conv2(x))

Run Code Online (Sandbox Code Playgroud)

希望这对您有所帮助并祝您学习愉快!