1. 程式人生 > >PyTorch官方中文文件:torch.nn.functional

PyTorch官方中文文件:torch.nn.functional

torch.nn.functional

Convolution 函式

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

對幾個輸入平面組成的輸入訊號應用1D卷積。

有關詳細資訊和輸出形狀,請參見Conv1d

引數:
input – 輸入張量的形狀 (minibatch x in_channels x iW)
weight – 過濾器的形狀 (out_channels, in_channels, kW)
bias – 可選偏置的形狀 (out_channels)
stride

 – 卷積核的步長,預設為1

例子:

>>> filters = autograd.Variable(torch.randn(33, 16, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50))
>>> F.conv1d(inputs, filters)
torch.nn.functional.conv2d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

對幾個輸入平面組成的輸入訊號應用2D卷積。

有關詳細資訊和輸出形狀,請參見Conv2d

引數:
input – 輸入張量 (minibatch x in_channels x iH x iW)
weight – 過濾器張量 (out_channels, in_channels/groups, kH, kW)
bias – 可選偏置張量 (out_channels)
stride – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。預設為1
padding – 輸入上隱含零填充。可以是單個數字或元組。 預設值:0
groups – 將輸入分成組,in_channels應該被組數除盡

例子:

>>> # With square kernels and equal stride
>>> filters = autograd.Variable(torch.randn(8,4,3,3))
>>> inputs = autograd.Variable(torch.randn(1,4,5,5))
>>> F.conv2d(inputs, filters, padding=1)
torch.nn.functional.conv3d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1)

對幾個輸入平面組成的輸入訊號應用3D卷積。

有關詳細資訊和輸出形狀,請參見Conv3d

引數:
input – 輸入張量的形狀 (minibatch x in_channels x iT x iH x iW)
weight – 過濾器張量的形狀 (out_channels, in_channels, kT, kH, kW)
bias – 可選偏置張量的形狀 (out_channels)
stride – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。預設為1
padding – 輸入上隱含零填充。可以是單個數字或元組。 預設值:0

例子:

>>> filters = autograd.Variable(torch.randn(33, 16, 3, 3, 3))
>>> inputs = autograd.Variable(torch.randn(20, 16, 50, 10, 20))
>>> F.conv3d(inputs, filters)
torch.nn.functional.conv_transpose1d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)
torch.nn.functional.conv_transpose2d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)

在由幾個輸入平面組成的輸入影象上應用二維轉置卷積,有時也稱為“去卷積”。

有關詳細資訊和輸出形狀,請參閱ConvTranspose2d

引數:
input – 輸入張量的形狀 (minibatch x in_channels x iH x iW)
weight – 過濾器的形狀 (in_channels x out_channels x kH x kW)
bias – 可選偏置的形狀 (out_channels)
stride – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。預設: 1
padding – 輸入上隱含零填充。可以是單個數字或元組。 (padh x padw)。預設: 0
groups – 將輸入分成組,in_channels應該被組數除盡
output_padding – 0 <= padding <stride的零填充,應該新增到輸出。可以是單個數字或元組。預設值:0

torch.nn.functional.conv_transpose3d(input, weight, bias=None, stride=1, padding=0, output_padding=0, groups=1)

在由幾個輸入平面組成的輸入影象上應用三維轉置卷積,有時也稱為“去卷積”。

有關詳細資訊和輸出形狀,請參閱ConvTranspose3d

引數:
input – 輸入張量的形狀 (minibatch x in_channels x iT x iH x iW)
weight – 過濾器的形狀 (in_channels x out_channels x kH x kW)
bias – 可選偏置的形狀 (out_channels)
stride – 卷積核的步長,可以是單個數字或一個元組 (sh x sw)。預設: 1
padding – 輸入上隱含零填充。可以是單個數字或元組。 (padh x padw)。預設: 0

Pooling 函式

torch.nn.functional.avg_pool1d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)

對由幾個輸入平面組成的輸入訊號進行一維平均池化。

有關詳細資訊和輸出形狀,請參閱AvgPool1d

引數:
kernel_size – 視窗的大小
stride – 視窗的步長。預設值為kernel_size
padding – 在兩邊新增隱式零填充
ceil_mode – 當為True時,將使用ceil代替floor來計算輸出形狀
count_include_pad – 當為True時,這將在平均計算時包括補零

例子:

>>> # pool of square window of size=3, stride=2
>>> input = Variable(torch.Tensor([[[1,2,3,4,5,6,7]]]))
>>> F.avg_pool1d(input, kernel_size=3, stride=2)
Variable containing:
(0 ,.,.) =
  2  4  6
[torch.FloatTensor of size 1x1x3]
torch.nn.functional.avg_pool2d(input, kernel_size, stride=None, padding=0, ceil_mode=False, count_include_pad=True)

在kh x kw區域中應用步長為dh x dw的二維平均池化操作。輸出特徵的數量等於輸入平面的數量。

有關詳細資訊和輸出形狀,請參閱AvgPool2d

引數:
input – 輸入的張量 (minibatch x in_channels x iH x iW)
kernel_size – 池化區域的大小,可以是單個數字或者元組 (kh x kw)
stride – 池化操作的步長,可以是單個數字或者元組 (sh x sw)。預設等於核的大小
padding – 在輸入上隱式的零填充,可以是單個數字或者一個元組 (padh x padw),預設: 0
ceil_mode – 定義空間輸出形狀的操作
count_include_pad – 除以原始非填充影象內的元素數量或kh * kw

torch.nn.functional.avg_pool3d(input, kernel_size, stride=None)

在kt x kh x kw區域中應用步長為dt x dh x dw的二維平均池化操作。輸出特徵的數量等於 input planes / dt。

torch.nn.functional.max_pool1d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
torch.nn.functional.max_pool2d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
torch.nn.functional.max_pool3d(input, kernel_size, stride=None, padding=0, dilation=1, ceil_mode=False, return_indices=False)
torch.nn.functional.max_unpool1d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
torch.nn.functional.max_unpool2d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
torch.nn.functional.max_unpool3d(input, indices, kernel_size, stride=None, padding=0, output_size=None)
torch.nn.functional.lp_pool2d(input, norm_type, kernel_size, stride=None, ceil_mode=False)
torch.nn.functional.adaptive_max_pool1d(input, output_size, return_indices=False)

在由幾個輸入平面組成的輸入訊號上應用1D自適應最大池化。

有關詳細資訊和輸出形狀,請參閱AdaptiveMaxPool1d

引數:
output_size – 目標輸出大小(單個整數)
return_indices – 是否返回池化的指數

torch.nn.functional.adaptive_max_pool2d(input, output_size, return_indices=False)

在由幾個輸入平面組成的輸入訊號上應用2D自適應最大池化。

有關詳細資訊和輸出形狀,請參閱AdaptiveMaxPool2d

引數:
output_size – 目標輸出大小(單整數或雙整數元組)
return_indices – 是否返回池化的指數

torch.nn.functional.adaptive_avg_pool1d(input, output_size)

在由幾個輸入平面組成的輸入訊號上應用1D自適應平均池化。

有關詳細資訊和輸出形狀,請參閱AdaptiveAvgPool1d

引數:
output_size – 目標輸出大小(單整數或雙整數元組)

torch.nn.functional.adaptive_avg_pool2d(input, output_size)

在由幾個輸入平面組成的輸入訊號上應用2D自適應平均池化。

有關詳細資訊和輸出形狀,請參閱AdaptiveAvgPool2d

引數:
output_size – 目標輸出大小(單整數或雙整數元組)

非線性啟用函式

torch.nn.functional.threshold(input, threshold, value, inplace=False)
torch.nn.functional.relu(input, inplace=False)
torch.nn.functional.hardtanh(input, min_val=-1.0, max_val=1.0, inplace=False)
torch.nn.functional.relu6(input, inplace=False)
torch.nn.functional.elu(input, alpha=1.0, inplace=False)
torch.nn.functional.leaky_relu(input, negative_slope=0.01, inplace=False)
torch.nn.functional.prelu(input, weight)
torch.nn.functional.rrelu(input, lower=0.125, upper=0.3333333333333333, training=False, inplace=False)
torch.nn.functional.logsigmoid(input)
torch.nn.functional.hardshrink(input, lambd=0.5)
torch.nn.functional.tanhshrink(input)
torch.nn.functional.softsign(input)
torch.nn.functional.softplus(input, beta=1, threshold=20)
torch.nn.functional.softmin(input)
torch.nn.functional.softmax(input)
torch.nn.functional.softshrink(input, lambd=0.5)
torch.nn.functional.log_softmax(input)
torch.nn.functional.tanh(input)
torch.nn.functional.sigmoid(input)

Normalization 函式

torch.nn.functional.batch_norm(input, running_mean, running_var, weight=None, bias=None, training=False, momentum=0.1, eps=1e-05)

線性函式

torch.nn.functional.linear(input, weight, bias=None)

Dropout 函式

torch.nn.functional.dropout(input, p=0.5, training=False, inplace=False)

距離函式(Distance functions)

torch.nn.functional.pairwise_distance(x1, x2, p=2, eps=1e-06)

計算向量v1、v2之間的距離(成次或者成對,意思是可以計算多個,可以參看後面的引數)
$$
\left | x \right |{p}:=\left ( \sum{i=1}^{N}\left | x_{i}^{p} \right | \right )^{1/p}
$$
引數:

  • x1:第一個輸入的張量
  • x2:第二個輸入的張量
  • p:矩陣範數的維度。預設值是2,即二範數。

規格:

  • 輸入:(N,D)其中D等於向量的維度
  • 輸出:(N,1)

例子:

>>> input1 = autograd.Variable(torch.randn(100, 128))
>>> input2 = autograd.Variable(torch.randn(100, 128))
>>> output = F.pairwise_distance(input1, input2, p=2)
>>> output.backward()

損失函式(Loss functions)

torch.nn.functional.nll_loss(input, target, weight=None, size_average=True)

負的log likelihood損失函式. 詳細請看NLLLoss.

引數:
input - (N,C) C 是類別的個數
target - (N) 其大小是 0 <= targets[i] <= C-1
weight (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
size_average (bool, optional) – 預設情況下,是mini-batchloss的平均值,然而,如果size_average=False,則是mini-batchloss的總和。

Variables:
weight – 對於constructor而言,每一類的權重作為輸入

torch.nn.functional.kl_div(input, target, size_average=True)

引數:
input – 任意形狀的 Variable
target – 與輸入相同形狀的 Variable
size_average – 如果為TRUE,loss則是平均值,需要除以輸入 tensor 中 element 的數目

torch.nn.functional.cross_entropy(input, target, weight=None, size_average=True)

該函式使用了 log_softmax 和 nll_loss,詳細請看CrossEntropyLoss

引數:
input - (N,C) 其中,C 是類別的個數
target - (N) 其大小是 0 <= targets[i] <= C-1
weight (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
size_average (bool, optional) – 預設情況下,是mini-batchloss的平均值,然而,如果size_average=False,則是mini-batchloss的總和。

torch.nn.functional.binary_cross_entropy(input, target, weight=None, size_average=True)

該函式計算了輸出與target之間的二進位制交叉熵,詳細請看BCELoss

引數:
input – 任意形狀的 Variable
target – 與輸入相同形狀的 Variable
weight (Variable, optional) – 一個可手動指定每個類別的權重。如果給定的話,必須是大小為nclasses的Variable
size_average (bool, optional) – 預設情況下,是mini-batchloss的平均值,然而,如果size_average=False,則是mini-batchloss的總和。

torch.nn.functional.smooth_l1_loss(input, target, size_average=True)

Vision functions

torch.nn.functional.pixel_shuffle(input, upscale_factor)[source]

將形狀為[*, C*r^2, H, W]Tensor重新排列成形狀為[C, H*r, W*r]的Tensor.

形參說明:
- input (Variable) – 輸入
- upscale_factor (int) – 增加空間解析度的因子.

例子:

ps = nn.PixelShuffle(3)
input = autograd.Variable(torch.Tensor(1, 9, 4, 4))
output = ps(input)
print(output.size())
torch.Size([1, 1, 12, 12])

torch.nn.functional.pad(input, pad, mode='constant', value=0)[source]

填充Tensor.

目前為止,只支援2D3D填充.
Currently only 2D and 3D padding supported.
當輸入為4D Tensor的時候,pad應該是一個4元素的tuple (pad_l, pad_r, pad_t, pad_b ) ,當輸入為5D Tensor的時候,pad應該是一個6元素的tuple (pleft, pright, ptop, pbottom, pfront, pback).

形參說明:

  • input (Variable) – 4D 或 5D tensor

  • pad (tuple) – 4元素 或 6-元素 tuple

  • mode – ‘constant’, ‘reflect’ or ‘replicate’

  • value – 用於constant padding 的值.

艾伯特(http://www.aibbt.com/)國內第一家人工智慧門戶

相關推薦

PyTorch官方中文torch.nn.functional

torch.nn.functionalConvolution 函式torch.nn.functional.conv1d(input, weight, bias=None, stride=1, padding=0, dilation=1, groups=1) 對幾個輸入平面組成

PyTorch官方中文torchvision.transforms

正則 slam dal get bsp ops compose python val pytorch torchvision transform 對PIL.Image進行變換 class torchvision.transforms.Compose(tran

Keras官方中文包裝器Wrapper

程序 mod 大小 add med str lstm softmax 國內 包裝器Wrapper TimeDistributed包裝器 keras.layers.wrappers.TimeDistributed(layer) 該包裝器可以把一個層應用到輸入的每一個時間步上

Keras官方中文優化器Optimizer

loss param compile 方法 clas xiv pos 後者 deep 優化器optimizers 優化器是編譯Keras模型必要的兩個參數之一 from keras import optimizers model = Sequential() model.

Keras官方中文融合層Merge

put 函數式 首字母 人工智能 余弦相似性 bat 調用 cti pytho Merge層 Merge層提供了一系列用於融合兩個層或兩個張量的層對象和方法。以大寫首字母開頭的是Layer類,以小寫字母開頭的是張量的函數。小寫字母開頭的張量函數在內部實際上是調用了大寫字母開

Keras官方中文循環層Recurrent

reset mask list long cell 矩陣 逆向 返回 窗口 循環層Recurrent Recurrent層 keras.layers.recurrent.Recurrent(return_sequences=False, go_backwards=False

Keras官方中文局部連接層Locally

model 參數 mat 字符 same 寫敏感 HR input 不同 局部連接層LocallyConnceted LocallyConnected1D層 keras.layers.local.LocallyConnected1D(filters, kernel_size

Chrome 開發者工具官方中文

傳送門 Chrome開發者工具官方中文文件 Chrome開發者工具詳解 前端學習front-end-study系列 Elements面板(https://zhuanlan.zhihu.com/p/24535735?refer=thinkingInFE) Elements

Airflow 中文寫日誌

在本地編寫日誌 使用者可以使用base_log_folder設定在airflow.cfg指定日誌資料夾。 預設情況下,它位於AIRFLOW_HOME目錄中。 此外,使用者可以提供遠端位置,以便在雲端儲存中儲存日誌和日誌備份。 在Airflow Web UI中,本地日誌優先於遠端日誌。 如果找不到或訪問本

Airflow 中文用Celery擴大規模

CeleryExecutor是您擴充套件工人數量的方法之一。 為此,您需要設定Celery後端( RabbitMQ , Redis ,...)並更改airflow.cfg以將執行程式引數指向CeleryExecutor並提供相關的Celery設定。 有關設定Celer

Airflow 中文用Dask擴充套件

DaskExecutor允許您在Dask分散式群集中執行Airflow任務。 Dask叢集可以在單個機器上執行,也可以在遠端網路上執行。 有關完整詳細資訊,請參閱分散式文件 。 要建立叢集,首先啟動排程程式: # default settings for a local cluster DA

Airflow 中文UI /截圖

通過Airflow UI,您可以輕鬆監控資料管道並對其進行故障排除。 以下是您可以在Airflow UI中找到的一些功能和視覺化的快速概述。 DAGs檢視 您環境中的DAG列表,以及一組有用頁面的快捷方式。 您可以一目瞭然地檢視成功,失敗或當前正在執行的任務數量。  

Airflow 中文保護連線

預設情況下,Airflow將在元資料資料庫中以純文字格式儲存連線的密碼。 在安裝過程中強烈建議使用crypto包。 crypto包確實要求您的作業系統安裝了libffi-dev。 如果最初未安裝crypto軟體包,您仍可以通過以下步驟為連線啟用加密: 安裝crypto包pip

Airflow 中文管理連線

Airflow需要知道如何連線到您的環境。 其他系統和服務的主機名,埠,登入名和密碼等資訊在UI的Admin-&gt;Connection部分中處理。 您將創作的管道程式碼將引用Connection物件的“conn_id”。 可以使用UI或環境變數建立和管理連線。 有關

Airflow 中文使用操作器

操作器代表一個理想情況下是冪等的任務。 操作員確定DAG執行時實際執行的內容。 有關更多資訊,請參閱Operators Concepts文件和Operators API Reference 。 BashOperator 模板 故障排除

Airflow 中文初始化資料庫後端

如果您想對Airflow進行真正的試駕,您應該考慮設定一個真正的資料庫後端並切換到LocalExecutor。 由於Airflow是使用優秀的SqlAlchemy庫與其元資料進行互動而構建的,因此您應該能夠使用任何支援作為SqlAlchemy後端的資料庫後端。 我們建議使用MySQL或P

Airflow 中文設定配置選項

第一次執行Airflow時,它會在$AIRFLOW_HOME目錄中建立一個名為airflow.cfg的檔案(預設情況下為~/airflow $AIRFLOW_HOME )。 此檔案包含Airflow的配置,您可以對其進行編輯以更改任何設定。 您還可以使用以下格式設定帶有環境變數的選項: $A

Airflow 中文教程

本教程將向您介紹一些基本的Airflow概念,物件及其在編寫第一個管道時的用法。 示例管道定義 以下是基本管道定義的示例。 如果這看起來很複雜,請不要擔心,下面將逐行說明。 """ Code that goes along with the Airflow tutorial l

Airflow 中文安裝

獲得氣流 安裝最新穩定版Airflow的最簡單方法是使用pip : pip install apache-airflow 您還可以安裝Airflow,支援s3或postgres等額外功能: pip install apache-airflow [ postgres,s3

Airflow 中文快速開始

安裝快速而直接。 # airflow needs a home, ~/airflow is the default, # but you can lay foundation somewhere else if you prefer # (optional) export AIRFLO