快速鍵

ZeroPad1d

class torch.nn.ZeroPad1d(padding)[source][source]

使用零填充輸入張量的邊界。

對於 N 維填充,請使用 torch.nn.functional.pad()

參數

padding(填充) ( int, tuple ) – 填充的大小。如果是 int,則在兩個邊界使用相同的填充。如果是 2-tuple,則使用 (padding_left\text{padding\_left}, padding_right\text{padding\_right})

形狀 (Shape)
  • 輸入 (Input): (C,Win)(C, W_{in})(N,C,Win)(N, C, W_{in}).

  • 輸出 (Output): (C,Wout)(C, W_{out})(N,C,Wout)(N, C, W_{out}), 其中

    Wout=Win+padding_left+padding_rightW_{out} = W_{in} + \text{padding\_left} + \text{padding\_right}

範例 (Examples)

>>> m = nn.ZeroPad1d(2)
>>> input = torch.randn(1, 2, 4)
>>> input
tensor([[[-1.0491, -0.7152, -0.0749,  0.8530],
         [-1.3287,  1.8966,  0.1466, -0.2771]]])
>>> m(input)
tensor([[[ 0.0000,  0.0000, -1.0491, -0.7152, -0.0749,  0.8530,  0.0000,
           0.0000],
         [ 0.0000,  0.0000, -1.3287,  1.8966,  0.1466, -0.2771,  0.0000,
           0.0000]]])
>>> m = nn.ZeroPad1d(2)
>>> input = torch.randn(1, 2, 3)
>>> input
tensor([[[ 1.6616,  1.4523, -1.1255],
         [-3.6372,  0.1182, -1.8652]]])
>>> m(input)
tensor([[[ 0.0000,  0.0000,  1.6616,  1.4523, -1.1255,  0.0000,  0.0000],
         [ 0.0000,  0.0000, -3.6372,  0.1182, -1.8652,  0.0000,  0.0000]]])
>>> # using different paddings for different sides
>>> m = nn.ZeroPad1d((3, 1))
>>> m(input)
tensor([[[ 0.0000,  0.0000,  0.0000,  1.6616,  1.4523, -1.1255,  0.0000],
         [ 0.0000,  0.0000,  0.0000, -3.6372,  0.1182, -1.8652,  0.0000]]])

文件

獲取 PyTorch 的完整開發者文件

檢視文件 (View Docs)

教學 (Tutorials)

獲取針對初學者和進階開發者的深度教學

檢視教學 (View Tutorials)

資源 (Resources)

尋找開發資源並獲得問題解答

檢視資源 (View Resources)