如何在 PyTorch 中使用常數值填充輸入張量的邊界?
**torch.nn.ConstantPad2D()** 使用常數值填充輸入張量的邊界。輸入張量的尺寸必須為 3D 或 4D,分別為 **(C,H,W) 或 (N,C,H,W)** 格式。其中 **N、C、H、W** 分別代表小批次大小、通道數、高度和寬度。填充沿輸入張量的高度和寬度進行。
它接受填充大小 (**padding**) 和常數值 (**value**) 作為引數。填充大小可以是整數或元組。**padding** 對所有邊界可以相同,也可以對每個邊界不同。
填充可以是整數或 (**左,右,上,下**) 格式的元組。如果 padding 是整數,則所有邊界的填充相同。
填充後的張量**高度**增加 **上+下**,而填充後的張量**寬度**增加 **左+右**。它不會改變通道大小或批大小。填充通常用於卷積神經網路 (CNN) 的池化層之後,以保持輸入大小。
語法
torch.nn.ConstantPad2D(padding, value)
引數
**padding** – 所需的填充大小。一個整數或 (**左,右,上,下**) 格式的元組。
**value** – 常數值。輸入張量將使用此值進行填充。
步驟
我們可以使用以下步驟來使用常數值填充輸入張量的邊界:
匯入所需的庫。在以下所有示例中,所需的 Python 庫是 torch。確保你已經安裝了它。
import torch
定義輸入張量。我們定義一個 4D 張量如下所示。
input = torch.randn(2, 1, 3, 3)
定義填充大小 **padding** 和常數值 **value**,並將它們傳遞給 **torch.nn.ConstantPad2D()** 並建立一個例項 **pad** 以使用常數值 value 對張量進行填充。填充大小可以相同或不同。
padding = (2,1) value = 3 pad = nn.ConstantPad2d(padding, value)
使用上面建立的例項 **pad** 使用常數值 **value** 對輸入張量進行填充。
output = pad(input)
列印最終填充後的張量。
print("Padded Ternsor:
", output)示例 1
在下面的 Python 示例中,我們使用常數值 **value=3** 和整數填充大小 2,即 **padding=2** 來填充 3D 和 4D 張量。
# Import the required library
import torch
import torch.nn as nn
# define 3D tensor (C,H,W)
input = torch.tensor([[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
",input)
# define padding same for all sides (left, right, top, bottom)
# nn.ConstantPad(padding, value)
pad = nn.ConstantPad2d(2, 3)
# pad the input tensor
output = pad(input)
print("Padded Ternsor:
", output)
# define same padding only for left and right sides
pad = nn.ConstantPad2d((2,2), 3)
# pad the input tensor
output = pad(input)
print("Padded Tensor:
", output)輸出
Input Tensor: tensor([[[1, 2], [3, 4]]]) Padded Tensor: tensor([[[3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 1, 2, 3, 3], [3, 3, 3, 4, 3, 3], [3, 3, 3, 3, 3, 3], [3, 3, 3, 3, 3, 3]]]) Padded Tensor: tensor([[[3, 3, 1, 2, 3, 3], [3, 3, 3, 4, 3, 3]]])
示例 2
在下面的 Python 示例中,我們使用對輸入張量所有邊界不同的填充大小來使用常數值進行填充。
# Import the required library
import torch
import torch.nn as nn
# define 3D tensor (C,H,W)
input = torch.tensor([[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
", input)
# define padding different for different sides
padding = (2,1,2,1)
value = 3
pad = nn.ConstantPad2d(padding,value)
# pad the input tensor
output = pad(input)
print("Padded Ternsor:
", output)
input = torch.tensor([[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
",input)
# define padding different for left and right sides
padding = (2,1)
value = 3
pad = nn.ConstantPad2d(padding,value)
# pad the input tensor
output = pad(input)
print("Padded Tensor:
", output)
# define 4D tensor (N,C,H,W)->for a batch of N tensors
input = torch.tensor([[[ 1, 2],[ 3, 4]],
[[ 1, 2],[ 3, 4]]])
print("Input Tensor:
",input)
# define padding different for different sides
padding = (2,2,1,1)
value = 7
pad = nn.ConstantPad2d(padding, value)
# pad the input tensor
output = pad(input)
print("Padded Tensor:
", output)輸出
Input Tensor: tensor([[[1, 2], [3, 4]]]) Padded Tensor: tensor([[[3, 3, 3, 3, 3], [3, 3, 3, 3, 3], [3, 3, 1, 2, 3], [3, 3, 3, 4, 3], [3, 3, 3, 3, 3]]]) Input Tensor: tensor([[[1, 2], [3, 4]]]) Padded Tensor: tensor([[[3, 3, 1, 2, 3], [3, 3, 3, 4, 3]]]) Input Tensor: tensor([[[1, 2], [3, 4]], [[1, 2], [3, 4]]]) Padded Ternsor: tensor([[[7, 7, 7, 7, 7, 7], [7, 7, 1, 2, 7, 7], [7, 7, 3, 4, 7, 7], [7, 7, 7, 7, 7, 7]], [[7, 7, 7, 7, 7, 7], [7, 7, 1, 2, 7, 7], [7, 7, 3, 4, 7, 7], [7, 7, 7, 7, 7, 7]]])
資料結構
網路
關係型資料庫管理系統 (RDBMS)
作業系統
Java
iOS
HTML
CSS
Android
Python
C語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP