溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

pytorch中的torch.nn.Conv2d()函數(shù)怎么用

發(fā)布時間:2022-03-01 09:09:31 來源:億速云 閱讀:360 作者:小新 欄目:開發(fā)技術(shù)

這篇文章主要為大家展示了“pytorch中的torch.nn.Conv2d()函數(shù)怎么用”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學習一下“pytorch中的torch.nn.Conv2d()函數(shù)怎么用”這篇文章吧。

    一、官方文檔介紹

    官網(wǎng)

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    nn.Conv2d:對由多個輸入平面組成的輸入信號進行二維卷積

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    二、torch.nn.Conv2d()函數(shù)詳解

    參數(shù)詳解

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

    參數(shù)參數(shù)類型  
    in_channelsintNumber of channels in the input image輸入圖像通道數(shù)
    out_channelsintNumber of channels produced by the convolution卷積產(chǎn)生的通道數(shù)
    kernel_size(int or tuple)Size of the convolving kernel卷積核尺寸,可以設(shè)為1個int型數(shù)或者一個(int, int)型的元組。例如(2,3)是高2寬3卷積核
    stride(int or tuple, optional)Stride of the convolution. Default: 1卷積步長,默認為1??梢栽O(shè)為1個int型數(shù)或者一個(int, int)型的元組。
    padding(int or tuple, optional)Zero-padding added to both sides of the input. Default: 0填充操作,控制padding_mode的數(shù)目。
    padding_mode(string, optional)‘zeros’, ‘reflect’, ‘replicate’ or ‘circular’. Default: ‘zeros’padding模式,默認為Zero-padding 。
    dilation(int or tuple, optional)Spacing between kernel elements. Default: 1擴張操作:控制kernel點(卷積核點)的間距,默認值:1。
    groups(int, optional)Number of blocked connections from input channels to output channels. Default: 1group參數(shù)的作用是控制分組卷積,默認不分組,為1組。
    bias(bool, optional)If True, adds a learnable bias to the output. Default: True為真,則在輸出中添加一個可學習的偏差。默認:True。

    參數(shù)dilation——擴張卷積(也叫空洞卷積)

    dilation操作動圖演示如下:

    Dilated Convolution with a 3 x 3 kernel and dilation rate 2

    擴張卷積核為3×3,擴張率為2

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    參數(shù)groups——分組卷積

    Group Convolution顧名思義,則是對輸入feature map進行分組,然后每組分別卷積。

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    三、代碼實例

    import torch
    
    x = torch.randn(3,1,5,4)
    print(x)
    
    conv = torch.nn.Conv2d(1,4,(2,3))
    res = conv(x)
    
    print(res.shape)    # torch.Size([3, 4, 4, 2])

    輸入:x[ batch_size, channels, height_1, width_1 ]

    • batch_size,一個batch中樣本的個數(shù) 3

    • channels,通道數(shù),也就是當前層的深度 1

    • height_1, 圖片的高 5

    • width_1, 圖片的寬 4

    卷積操作:Conv2d[ channels, output, height_2, width_2 ]

    • channels,通道數(shù),和上面保持一致,也就是當前層的深度 1

    • output ,輸出的深度 4【需要4個filter】

    • height_2,卷積核的高 2

    • width_2,卷積核的寬 3

    輸出:res[ batch_size,output, height_3, width_3 ]

    • batch_size,,一個batch中樣例的個數(shù),同上 3

    • output, 輸出的深度 4

    • height_3, 卷積結(jié)果的高度 4

    • width_3,卷積結(jié)果的寬度 2

    一個樣本卷積示例:

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    pytorch中的torch.nn.Conv2d()函數(shù)怎么用

    以上是“pytorch中的torch.nn.Conv2d()函數(shù)怎么用”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道!

    向AI問一下細節(jié)

    免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI