模組¶
PyTorch 使用模組來表示神經網路。模組是
- 有狀態計算的建構模組。 PyTorch 提供了一個強大的模組庫,並且可以輕鬆定義新的自訂模組,從而可以輕鬆建構複雜的多層神經網路。 
- 與 PyTorch 的 autograd 系統緊密整合。 模組可以輕鬆指定可學習的參數,以便 PyTorch 的優化器進行更新。 
- 易於使用和轉換。 模組可以輕鬆地儲存和還原、在 CPU / GPU / TPU 設備之間傳送、修剪、量化等等。 
本說明描述了模組,適用於所有 PyTorch 使用者。由於模組是 PyTorch 的基礎,因此本說明中的許多主題在其他說明或教學中都有詳細說明,並且也提供了許多此類文件的連結。
一個簡單的自訂模組¶
首先,讓我們來看一個更簡單的自訂版本的 PyTorch Linear 模組。此模組對其輸入應用仿射變換。
import torch
from torch import nn
class MyLinear(nn.Module):
  def __init__(self, in_features, out_features):
    super().__init__()
    self.weight = nn.Parameter(torch.randn(in_features, out_features))
    self.bias = nn.Parameter(torch.randn(out_features))
  def forward(self, input):
    return (input @ self.weight) + self.bias
這個簡單的模組具有模組的以下基本特徵
- 它繼承自基礎 Module 類別。 所有模組都應該繼承 - Module,以便與其他模組組合。
- 它定義了一些用於計算的「狀態」。 在這裡,狀態包含隨機初始化的 - weight和- bias張量,它們定義了仿射變換。由於這些張量都被定義為- Parameter,因此它們會被註冊到模組中,並且會在呼叫- parameters()時自動被追蹤和返回。參數可以被視為模組計算的「可學習」方面(稍後會詳細介紹)。請注意,模組不需要具有狀態,也可以是無狀態的。
- 它定義了一個執行計算的 forward() 函數。 對於這個仿射變換模組,輸入會與 - weight參數進行矩陣乘法運算(使用- @簡寫表示法),並與- bias參數相加以產生輸出。更一般地說,模組的- forward()實作可以執行涉及任意數量輸入和輸出的計算。
這個簡單的模組展示了模組如何將狀態和計算打包在一起。可以建構和呼叫此模組的實例
m = MyLinear(4, 3)
sample_input = torch.randn(4)
m(sample_input)
: tensor([-0.3037, -1.0413, -4.2057], grad_fn=<AddBackward0>)
請注意,模組本身是可呼叫的,並且呼叫它會呼叫其 forward() 函數。這個名稱指的是「正向傳遞」和「反向傳遞」的概念,它們適用於每個模組。「正向傳遞」負責將模組表示的計算應用於給定的輸入(如上程式碼片段所示)。「反向傳遞」計算模組輸出相對於其輸入的梯度,這些梯度可以用於通過梯度下降方法「訓練」參數。PyTorch 的 autograd 系統會自動處理此反向傳遞計算,因此不需要為每個模組手動實作 backward() 函數。在 使用模組進行神經網路訓練 中詳細介紹了通過連續的正向/反向傳遞來訓練模組參數的過程。
可以通過呼叫 parameters() 或 named_parameters() 來迭代模組註冊的完整參數集,其中後者包含每個參數的名稱
for parameter in m.named_parameters():
  print(parameter)
: ('weight', Parameter containing:
tensor([[ 1.0597,  1.1796,  0.8247],
        [-0.5080, -1.2635, -1.1045],
        [ 0.0593,  0.2469, -1.4299],
        [-0.4926, -0.5457,  0.4793]], requires_grad=True))
('bias', Parameter containing:
tensor([ 0.3634,  0.2015, -0.8525], requires_grad=True))
一般來說,模組註冊的參數是模組計算中應該被「學習」的部分。本文稍後將展示如何使用 PyTorch 的優化器更新這些參數。不過,在我們開始之前,讓我們先來看看如何將模組彼此組合。
模組作為建構模組¶
模組可以包含其他模組,使其成為開發更精細功能的有用建構模組。最簡單的方法是使用 Sequential 模組。它允許我們將多個模組串在一起
net = nn.Sequential(
  MyLinear(4, 3),
  nn.ReLU(),
  MyLinear(3, 1)
)
sample_input = torch.randn(4)
net(sample_input)
: tensor([-0.6749], grad_fn=<AddBackward0>)
請注意,Sequential 會自動將第一個 MyLinear 模組的輸出作為輸入饋送到 ReLU 中,並將其輸出作為輸入饋送到第二個 MyLinear 模組中。如圖所示,它僅限於按順序鏈接具有單個輸入和輸出的模組。
一般來說,建議為任何超出最簡單用例的情況定義自訂模組,因為這為如何將子模組用於模組的計算提供了充分的靈活性。
例如,以下是一個作為自訂模組實作的簡單神經網路
import torch.nn.functional as F
class Net(nn.Module):
  def __init__(self):
    super().__init__()
    self.l0 = MyLinear(4, 3)
    self.l1 = MyLinear(3, 1)
  def forward(self, x):
    x = self.l0(x)
    x = F.relu(x)
    x = self.l1(x)
    return x
此模組由兩個「子模組」或「子模組」(l0 和 l1)組成,它們定義了神經網路的層,並用於模組的 forward() 方法中的計算。可以透過呼叫 children() 或 named_children() 來迭代模組的直接子模組
net = Net()
for child in net.named_children():
  print(child)
: ('l0', MyLinear())
('l1', MyLinear())
若要深入了解直接子模組以外的內容,modules() 和 named_modules() 會_遞迴地_迭代模組及其子模組
class BigNet(nn.Module):
  def __init__(self):
    super().__init__()
    self.l1 = MyLinear(5, 4)
    self.net = Net()
  def forward(self, x):
    return self.net(self.l1(x))
big_net = BigNet()
for module in big_net.named_modules():
  print(module)
: ('', BigNet(
  (l1): MyLinear()
  (net): Net(
    (l0): MyLinear()
    (l1): MyLinear()
  )
))
('l1', MyLinear())
('net', Net(
  (l0): MyLinear()
  (l1): MyLinear()
))
('net.l0', MyLinear())
('net.l1', MyLinear())
有時,模組需要動態定義子模組。此時,ModuleList 和 ModuleDict 模組就派上用場了;它們會從清單或字典中註冊子模組
class DynamicNet(nn.Module):
  def __init__(self, num_layers):
    super().__init__()
    self.linears = nn.ModuleList(
      [MyLinear(4, 4) for _ in range(num_layers)])
    self.activations = nn.ModuleDict({
      'relu': nn.ReLU(),
      'lrelu': nn.LeakyReLU()
    })
    self.final = MyLinear(4, 1)
  def forward(self, x, act):
    for linear in self.linears:
      x = linear(x)
    x = self.activations[act](x)
    x = self.final(x)
    return x
dynamic_net = DynamicNet(3)
sample_input = torch.randn(4)
output = dynamic_net(sample_input, 'relu')
對於任何給定的模組,其參數包括其直接參數以及所有子模組的參數。這意味著呼叫 parameters() 和 named_parameters() 將遞迴地包含子參數,從而可以方便地優化網路中的所有參數
for parameter in dynamic_net.named_parameters():
  print(parameter)
: ('linears.0.weight', Parameter containing:
tensor([[-1.2051,  0.7601,  1.1065,  0.1963],
        [ 3.0592,  0.4354,  1.6598,  0.9828],
        [-0.4446,  0.4628,  0.8774,  1.6848],
        [-0.1222,  1.5458,  1.1729,  1.4647]], requires_grad=True))
('linears.0.bias', Parameter containing:
tensor([ 1.5310,  1.0609, -2.0940,  1.1266], requires_grad=True))
('linears.1.weight', Parameter containing:
tensor([[ 2.1113, -0.0623, -1.0806,  0.3508],
        [-0.0550,  1.5317,  1.1064, -0.5562],
        [-0.4028, -0.6942,  1.5793, -1.0140],
        [-0.0329,  0.1160, -1.7183, -1.0434]], requires_grad=True))
('linears.1.bias', Parameter containing:
tensor([ 0.0361, -0.9768, -0.3889,  1.1613], requires_grad=True))
('linears.2.weight', Parameter containing:
tensor([[-2.6340, -0.3887, -0.9979,  0.0767],
        [-0.3526,  0.8756, -1.5847, -0.6016],
        [-0.3269, -0.1608,  0.2897, -2.0829],
        [ 2.6338,  0.9239,  0.6943, -1.5034]], requires_grad=True))
('linears.2.bias', Parameter containing:
tensor([ 1.0268,  0.4489, -0.9403,  0.1571], requires_grad=True))
('final.weight', Parameter containing:
tensor([[ 0.2509], [-0.5052], [ 0.3088], [-1.4951]], requires_grad=True))
('final.bias', Parameter containing:
tensor([0.3381], requires_grad=True))
使用 to() 將所有參數移動到不同的設備或更改其精度也很容易
# Move all parameters to a CUDA device
dynamic_net.to(device='cuda')
# Change precision of all parameters
dynamic_net.to(dtype=torch.float64)
dynamic_net(torch.randn(5, device='cuda', dtype=torch.float64))
: tensor([6.5166], device='cuda:0', dtype=torch.float64, grad_fn=<AddBackward0>)
更一般的情況下,可以使用 apply() 函數遞迴地將任意函數應用於模組及其子模組。例如,要將自訂初始化應用於模組及其子模組的參數
# Define a function to initialize Linear weights.
# Note that no_grad() is used here to avoid tracking this computation in the autograd graph.
@torch.no_grad()
def init_weights(m):
  if isinstance(m, nn.Linear):
    nn.init.xavier_normal_(m.weight)
    m.bias.fill_(0.0)
# Apply the function recursively on the module and its submodules.
dynamic_net.apply(init_weights)
這些範例展示了如何透過模組組合形成精細的神經網路,以及如何方便地操作它們。為了允許以最少的樣板程式碼快速輕鬆地建構神經網路,PyTorch 在 torch.nn 命名空間中提供了一個大型高效能模組庫,這些模組執行常見的神經網路操作,如池化、卷積、損失函數等。
在下一節中,我們將給出一個訓練神經網路的完整範例。
如要了解更多資訊,請查看
- PyTorch 提供的模組庫:torch.nn 
- 定義神經網路模組:https://pytorch.com.tw/tutorials/beginner/examples_nn/polynomial_module.html 
使用模組訓練神經網路¶
一旦建立了網路,就必須對其進行訓練,並且可以使用 PyTorch 的 torch.optim 中的一個優化器輕鬆優化其參數
# Create the network (from previous section) and optimizer
net = Net()
optimizer = torch.optim.SGD(net.parameters(), lr=1e-4, weight_decay=1e-2, momentum=0.9)
# Run a sample training loop that "teaches" the network
# to output the constant zero function
for _ in range(10000):
  input = torch.randn(4)
  output = net(input)
  loss = torch.abs(output)
  net.zero_grad()
  loss.backward()
  optimizer.step()
# After training, switch the module to eval mode to do inference, compute performance metrics, etc.
# (see discussion below for a description of training and evaluation modes)
...
net.eval()
...
在這個簡化的範例中,網路學習簡單地輸出零,因為任何非零輸出都會根據其絕對值被 torch.abs() 作為損失函數「懲罰」。雖然這不是一個非常有趣的任務,但訓練的關鍵部分都存在
- 建立一個網路。 
- 建立一個優化器(在本例中為隨機梯度下降優化器),並將網路的參數與其關聯。 
- 一個訓練迴圈...
- 取得輸入, 
- 執行網路, 
- 計算損失, 
- 將網路參數的梯度歸零, 
- 呼叫 loss.backward() 來更新參數的梯度, 
- 呼叫 optimizer.step() 將梯度應用於參數。 
 
 
執行上述程式碼片段後,請注意網路的參數已更改。特別是,檢查 l1 的 weight 參數的值會發現其值現在更接近 0(正如預期的那樣)
print(net.l1.weight)
: Parameter containing:
tensor([[-0.0013],
        [ 0.0030],
        [-0.0008]], requires_grad=True)
請注意,上述過程完全是在網路模組處於「訓練模式」時完成的。模組預設為訓練模式,可以使用 train() 和 eval() 在訓練和評估模式之間切換。它們的行為可能因其所處的模式而異。例如,BatchNorm 模組在訓練期間會維護一個執行中的均值和方差,這些均值和方差在模組處於評估模式時不會更新。一般來說,模組在訓練期間應處於訓練模式,並且僅在推理或評估時才切換到評估模式。以下是一個自訂模組的範例,它在兩種模式之間的行為不同
class ModalModule(nn.Module):
  def __init__(self):
    super().__init__()
  def forward(self, x):
    if self.training:
      # Add a constant only in training mode.
      return x + 1.
    else:
      return x
m = ModalModule()
x = torch.randn(4)
print('training mode output: {}'.format(m(x)))
: tensor([1.6614, 1.2669, 1.0617, 1.6213, 0.5481])
m.eval()
print('evaluation mode output: {}'.format(m(x)))
: tensor([ 0.6614,  0.2669,  0.0617,  0.6213, -0.4519])
訓練神經網路通常很棘手。如要了解更多資訊,請查看
模組狀態¶
在上一節中,我們演示了如何訓練模組的「參數」或計算的可學習方面。現在,如果我們想將訓練好的模型保存到磁碟,我們可以透過保存其 state_dict(即「狀態字典」)來實現
# Save the module
torch.save(net.state_dict(), 'net.pt')
...
# Load the module later on
new_net = Net()
new_net.load_state_dict(torch.load('net.pt'))
: <All keys matched successfully>
模組的 state_dict 包含影響其計算的狀態。這包括但不限於模組的參數。對於某些模組,除了參數之外,還有一些狀態會影響模組的計算,但這些狀態是不可學習的。對於這種情況,PyTorch 提供了「緩衝區」的概念,「緩衝區」分為「持久性」和「非持久性」。以下是模組可以擁有的各種狀態類型的概述
- **參數**:計算的可學習方面;包含在 - state_dict中
- **緩衝區**:計算的不可學習方面 - **持久性**緩衝區:包含在 - state_dict中(即在保存和載入時序列化)
- **非持久性**緩衝區:不包含在 - state_dict中(即在序列化中被忽略)
 
作為使用緩衝區的一個啟發性範例,請考慮一個維護執行中均值的簡單模組。我們希望將執行中均值的當前值視為模組的 state_dict 的一部分,以便在載入模組的序列化形式時可以恢復它,但我們不希望它是可學習的。以下程式碼片段展示了如何使用 register_buffer() 來實現此目的
class RunningMean(nn.Module):
  def __init__(self, num_features, momentum=0.9):
    super().__init__()
    self.momentum = momentum
    self.register_buffer('mean', torch.zeros(num_features))
  def forward(self, x):
    self.mean = self.momentum * self.mean + (1.0 - self.momentum) * x
    return self.mean
現在,執行中均值的當前值被視為模組的 state_dict 的一部分,並且在從磁碟載入模組時將被正確恢復
m = RunningMean(4)
for _ in range(10):
  input = torch.randn(4)
  m(input)
print(m.state_dict())
: OrderedDict([('mean', tensor([ 0.1041, -0.1113, -0.0647,  0.1515]))]))
# Serialized form will contain the 'mean' tensor
torch.save(m.state_dict(), 'mean.pt')
m_loaded = RunningMean(4)
m_loaded.load_state_dict(torch.load('mean.pt'))
assert(torch.all(m.mean == m_loaded.mean))
如前所述,可以透過將緩衝區標記為非持久性來將其排除在模組的 state_dict 之外
self.register_buffer('unserialized_thing', torch.randn(5), persistent=False)
持久性和非持久性緩衝區都會受到使用 to() 應用的模型範圍設備/dtype 更改的影響
# Moves all module parameters and buffers to the specified device / dtype
m.to(device='cuda', dtype=torch.float64)
可以使用 buffers() 或 named_buffers() 迭代模組的緩衝區。
for buffer in m.named_buffers():
  print(buffer)
以下類別演示了在模組中註冊參數和緩衝區的各種方法
class StatefulModule(nn.Module):
  def __init__(self):
    super().__init__()
    # Setting a nn.Parameter as an attribute of the module automatically registers the tensor
    # as a parameter of the module.
    self.param1 = nn.Parameter(torch.randn(2))
    # Alternative string-based way to register a parameter.
    self.register_parameter('param2', nn.Parameter(torch.randn(3)))
    # Reserves the "param3" attribute as a parameter, preventing it from being set to anything
    # except a parameter. "None" entries like this will not be present in the module's state_dict.
    self.register_parameter('param3', None)
    # Registers a list of parameters.
    self.param_list = nn.ParameterList([nn.Parameter(torch.randn(2)) for i in range(3)])
    # Registers a dictionary of parameters.
    self.param_dict = nn.ParameterDict({
      'foo': nn.Parameter(torch.randn(3)),
      'bar': nn.Parameter(torch.randn(4))
    })
    # Registers a persistent buffer (one that appears in the module's state_dict).
    self.register_buffer('buffer1', torch.randn(4), persistent=True)
    # Registers a non-persistent buffer (one that does not appear in the module's state_dict).
    self.register_buffer('buffer2', torch.randn(5), persistent=False)
    # Reserves the "buffer3" attribute as a buffer, preventing it from being set to anything
    # except a buffer. "None" entries like this will not be present in the module's state_dict.
    self.register_buffer('buffer3', None)
    # Adding a submodule registers its parameters as parameters of the module.
    self.linear = nn.Linear(2, 3)
m = StatefulModule()
# Save and load state_dict.
torch.save(m.state_dict(), 'state.pt')
m_loaded = StatefulModule()
m_loaded.load_state_dict(torch.load('state.pt'))
# Note that non-persistent buffer "buffer2" and reserved attributes "param3" and "buffer3" do
# not appear in the state_dict.
print(m_loaded.state_dict())
: OrderedDict([('param1', tensor([-0.0322,  0.9066])),
               ('param2', tensor([-0.4472,  0.1409,  0.4852])),
               ('buffer1', tensor([ 0.6949, -0.1944,  1.2911, -2.1044])),
               ('param_list.0', tensor([ 0.4202, -0.1953])),
               ('param_list.1', tensor([ 1.5299, -0.8747])),
               ('param_list.2', tensor([-1.6289,  1.4898])),
               ('param_dict.bar', tensor([-0.6434,  1.5187,  0.0346, -0.4077])),
               ('param_dict.foo', tensor([-0.0845, -1.4324,  0.7022])),
               ('linear.weight', tensor([[-0.3915, -0.6176],
                                         [ 0.6062, -0.5992],
                                         [ 0.4452, -0.2843]])),
               ('linear.bias', tensor([-0.3710, -0.0795, -0.3947]))])
如要了解更多資訊,請查看
模組初始化¶
默認情況下,torch.nn 提供的模組之參數和浮點數緩衝區會在模組實例化期間初始化為 CPU 上的 32 位元浮點數值,並使用經歷史證明對該模組類型有效的初始化方案。對於某些使用案例,可能需要使用不同的 dtype、裝置(例如 GPU)或初始化技術進行初始化。
範例
# Initialize module directly onto GPU.
m = nn.Linear(5, 3, device='cuda')
# Initialize module with 16-bit floating point parameters.
m = nn.Linear(5, 3, dtype=torch.half)
# Skip default parameter initialization and perform custom (e.g. orthogonal) initialization.
m = torch.nn.utils.skip_init(nn.Linear, 5, 3)
nn.init.orthogonal_(m.weight)
請注意,上述所示範的裝置和 dtype 選項也適用於為模組註冊的任何浮點數緩衝區
m = nn.BatchNorm2d(3, dtype=torch.half)
print(m.running_mean)
: tensor([0., 0., 0.], dtype=torch.float16)
雖然模組編寫者可以使用任何裝置或 dtype 來初始化其自訂模組中的參數,但良好的做法是默認也使用 dtype=torch.float 和 device='cpu'。或者,您可以通過遵循上述所有 torch.nn 模組都遵循的慣例,為您的自訂模組在這些方面提供完全的靈活性
- 提供一個適用於模組註冊的任何參數/緩衝區的 - device建構函數關鍵字參數。
- 提供一個適用於模組註冊的任何參數/浮點數緩衝區的 - dtype建構函數關鍵字參數。
- 僅在模組的建構函數中對參數和緩衝區使用初始化函數(即來自 - torch.nn.init的函數)。請注意,這僅是使用- skip_init()所必需的;有關說明,請參閱此頁面。
如要了解更多資訊,請查看
模組鉤子¶
在使用模組進行神經網路訓練中,我們演示了模組的訓練過程,該過程迭代地執行正向和反向傳遞,每次迭代都更新模組參數。為了更好地控制這個過程,PyTorch 提供了「鉤子」,可以在正向或反向傳遞期間執行任意計算,甚至可以根據需要修改傳遞的方式。此功能的一些有用示例包括除錯、視覺化激活、深入檢查梯度等。鉤子可以添加到您自己沒有編寫的模組中,這意味著此功能可以應用於第三方或 PyTorch 提供的模組。
PyTorch 為模組提供了兩種鉤子
- **正向鉤子**在正向傳遞期間被調用。它們可以使用 - register_forward_pre_hook()和- register_forward_hook()為給定的模組安裝。這些鉤子將分別在正向函數被調用之前和之後被調用。或者,可以使用類似的- register_module_forward_pre_hook()和- register_module_forward_hook()函數為所有模組全局安裝這些鉤子。
- **反向鉤子**在反向傳遞期間被調用。它們可以使用 - register_full_backward_pre_hook()和- register_full_backward_hook()安裝。當計算完此模組的反向傳遞時,將調用這些鉤子。- register_full_backward_pre_hook()將允許用戶訪問輸出的梯度,而- register_full_backward_hook()將允許用戶訪問輸入和輸出的梯度。或者,可以使用- register_module_full_backward_hook()和- register_module_full_backward_pre_hook()為所有模組全局安裝它們。
所有鉤子都允許用戶返回一個更新值,該值將在剩餘的計算過程中使用。因此,這些鉤子可以用於沿著常規模組正向/反向傳遞執行任意代碼,或者修改一些輸入/輸出,而無需更改模組的 forward() 函數。
下面是一個演示正向和反向鉤子用法的示例
torch.manual_seed(1)
def forward_pre_hook(m, inputs):
  # Allows for examination and modification of the input before the forward pass.
  # Note that inputs are always wrapped in a tuple.
  input = inputs[0]
  return input + 1.
def forward_hook(m, inputs, output):
  # Allows for examination of inputs / outputs and modification of the outputs
  # after the forward pass. Note that inputs are always wrapped in a tuple while outputs
  # are passed as-is.
  # Residual computation a la ResNet.
  return output + inputs[0]
def backward_hook(m, grad_inputs, grad_outputs):
  # Allows for examination of grad_inputs / grad_outputs and modification of
  # grad_inputs used in the rest of the backwards pass. Note that grad_inputs and
  # grad_outputs are always wrapped in tuples.
  new_grad_inputs = [torch.ones_like(gi) * 42. for gi in grad_inputs]
  return new_grad_inputs
# Create sample module & input.
m = nn.Linear(3, 3)
x = torch.randn(2, 3, requires_grad=True)
# ==== Demonstrate forward hooks. ====
# Run input through module before and after adding hooks.
print('output with no forward hooks: {}'.format(m(x)))
: output with no forward hooks: tensor([[-0.5059, -0.8158,  0.2390],
                                        [-0.0043,  0.4724, -0.1714]], grad_fn=<AddmmBackward>)
# Note that the modified input results in a different output.
forward_pre_hook_handle = m.register_forward_pre_hook(forward_pre_hook)
print('output with forward pre hook: {}'.format(m(x)))
: output with forward pre hook: tensor([[-0.5752, -0.7421,  0.4942],
                                        [-0.0736,  0.5461,  0.0838]], grad_fn=<AddmmBackward>)
# Note the modified output.
forward_hook_handle = m.register_forward_hook(forward_hook)
print('output with both forward hooks: {}'.format(m(x)))
: output with both forward hooks: tensor([[-1.0980,  0.6396,  0.4666],
                                          [ 0.3634,  0.6538,  1.0256]], grad_fn=<AddBackward0>)
# Remove hooks; note that the output here matches the output before adding hooks.
forward_pre_hook_handle.remove()
forward_hook_handle.remove()
print('output after removing forward hooks: {}'.format(m(x)))
: output after removing forward hooks: tensor([[-0.5059, -0.8158,  0.2390],
                                               [-0.0043,  0.4724, -0.1714]], grad_fn=<AddmmBackward>)
# ==== Demonstrate backward hooks. ====
m(x).sum().backward()
print('x.grad with no backwards hook: {}'.format(x.grad))
: x.grad with no backwards hook: tensor([[ 0.4497, -0.5046,  0.3146],
                                         [ 0.4497, -0.5046,  0.3146]])
# Clear gradients before running backward pass again.
m.zero_grad()
x.grad.zero_()
m.register_full_backward_hook(backward_hook)
m(x).sum().backward()
print('x.grad with backwards hook: {}'.format(x.grad))
: x.grad with backwards hook: tensor([[42., 42., 42.],
                                      [42., 42., 42.]])
進階功能¶
PyTorch 還提供了一些更進階的功能,旨在與模組配合使用。所有這些功能都可用於自訂編寫的模組,但需要注意的是,某些功能可能需要模組符合特定的約束才能得到支持。有關這些功能和相應要求的詳細討論,請參閱以下鏈接。
性能分析¶
PyTorch Profiler 可用於識別模型中的性能瓶頸。它可以測量並輸出內存使用情況和時間消耗的性能特徵。
通過剪枝提高內存使用率¶
大型深度學習模型通常參數過多,導致內存使用率很高。為了克服這個問題,PyTorch 提供了模型剪枝機制,可以在保持任務準確性的同時幫助減少內存使用。 剪枝教程描述了如何利用 PyTorch 提供的剪枝技術或根據需要定義自定義剪枝技術。
參數化¶
對於某些應用程序,在模型訓練期間約束參數空間可能是有益的。例如,強制學習到的參數的正交性可以提高 RNN 的收斂性。PyTorch 提供了一種應用參數化的機制,例如這種,並且允許定義自定義約束。
使用 FX 轉換模組¶
PyTorch 的FX 組件提供了一種通過直接操作模組計算圖來轉換模組的靈活方法。這可以用於以編程方式生成或操作模組,以滿足廣泛的用例。要探索 FX,請查看這些使用 FX 進行卷積+批歸一化融合和CPU 性能分析的示例。