日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx

發布時間:2025/4/16 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx

在寫 PyTorch 代碼時,我們會發現在 torch.nn.xxx 和 torch.nn.functional.xxx 中有一些功能重復的操作,比如卷積、激活、池化。這些操作有什么不同?各有什么用處?

首先可以觀察源碼:

eg:torch.nn.Conv2d

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

eg:torch.nn.functional

torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) → Tensor

從中,我們可以發現,nn.Conv2d 是一個類,而 nn.functional.conv2d是一個函數。

換言之:

  • nn.Module 實現的 layer 是由 class Layer(nn.Module) 定義的特殊類
  • nn.functional 中的函數更像是純函數,由 def function(input) 定義

此外:

  • 兩者的調用方式不同:調用 nn.xxx 時要先在里面傳入超參數,然后再將數據以函數調用的方式傳入 nn.xxx

    # torch.nn inputs = torch.randn(64, 3, 244, 244) self.conv = nn.Conv2d(in_channels=3, out_channels=64, kernel_size=3, padding=1) outputs = self.conv(inputs)# torch.nn.functional 需要同時傳入數據和 weight,bias等參數 inputs = torch.randn(64, 3, 244, 244) weight = torch.randn(64, 3, 3, 3) bias = torch.randn(64) outputs = nn.functinoal.conv2d(inputs, weight, bias, padding=1)
  • nn.xxx 能夠放在 nn.Sequential里,而 nn.functional.xxx 就不行

  • nn.functional.xxx 需要自己定義 weight,每次調用時都需要手動傳入 weight,而 nn.xxx 則不用

    import torch import torch.nn as nn import torch.nn.functional as F# torch.nn 定義的CNN class CNN(nn.Module):def __init__(self):super(CNN, self).__init__()self.conv_1 = nn.Conv2d(1, 16, krenel_size=5, padding=0)self.relu_1 = nn.ReLU(inplace=True)self.maxpool_1 = nn.MaxPool2d(kernel_size=2)self.conv_2 = nn.Conv2d(16, 32, krenel_size=5, padding=0)self.relu_2 = nn.ReLU(inplace=True)self.maxpool_2 = nn.MaxPool2d(kernel_size=2) self.linear = nn.Linear(4*4*32, 10)def forward(self, x):x = x.view(x.size(0), -1)out = self.maxpool_1(self.relu_1(self.conv_1(x)))out = self.maxpool_2(self.relu_2(self.conv_2(out)))out = self.linear(out.view(x.size(0), -1))return out# torch.nn.functional 定義一個相同的CNN class CNN(nn.Module):def __init__(self):super(CNN, self).__init__()self.conv_1_weight = nn.Parameter(torch.randn(16, 1, 5, 5))self.bias_1_weight = nn.Parameter(torch.randn(16))self.conv_2_weight = nn.Parameter(torch.randn(32, 16, 5, 5))self.bias_2_weight = nn.Parameter(torch.randn(32))self.linear_weight = nn.Parameter(torch.randn(4 * 4 * 32, 10))self.bias_weight = nn.Parameter(torch.randn(10))def forward(self, x):x = x.view(x.size(0), -1)out = F.conv2d(x, self.conv_1_weight, self.bias_1_weight)out = F.conv2d(out, self.conv_2_weight, self.bias_2_weight)out = F.linear(out.view(x.size(0), -1), self.linear_weight, self.bias_weight)
  • 在使用Dropout時,推薦使用 nn.xxx。因為一般只有訓練時才使用 Dropout,在驗證或測試時不需要使用 Dropout。使用 nn.Dropout時,如果調用 model.eval() ,模型的 Dropout 層都會關閉;但如果使用 nn.functional.dropout,在調用 model.eval() 時,不會關閉 Dropout。

  • 當我們想要自定義卷積核時,是不能使用torch.nn.ConvNd 的,因為它里面的權重都是需要學習的參數,沒有辦法自行定義。但是,我們可以使用 torch.nn.functional.conv2d() 。

  • References:

  • pytorch:nn與nn.functional的區別——簡書
  • 轉載于:https://www.cnblogs.com/xxxxxxxxx/p/11475419.html

    總結

    以上是生活随笔為你收集整理的PyTorch : torch.nn.xxx 和 torch.nn.functional.xxx的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。