溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

如何進行Function函數(shù)的分析

發(fā)布時間:2021-11-23 10:49:55 來源:億速云 閱讀:133 作者:柒染 欄目:大數(shù)據(jù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)如何進行Function函數(shù)的分析,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

原文以及翻譯:

Function 函數(shù)
torch.autograd.Function
Records operation history and defines formulas for differentiating ops.
記錄操作歷史,并且定義求導(dǎo)操作的公式.
Every operation performed on Tensor s creates a new function object, 
that performs the computation, and records that it happened. 
The history is retained in the form of a DAG of functions, 
with edges denoting data dependencies (input <- output). 
Then, when backward is called, the graph is processed in the topological ordering, 
by calling backward() methods of each Function object, 
and passing returned gradients on to next Function s.
作用在每個Tensor上的操作都會新創(chuàng)建一個新的function對象,
這些function對象執(zhí)行計算,并且記錄計算的發(fā)生.
這些歷史以函數(shù)functions的有向無環(huán)圖的形式保留下來.
有向無環(huán)圖的邊表示數(shù)據(jù)的依賴關(guān)系(輸入 <- 輸出)(input <- output).
之后,當(dāng)反向傳播backward被調(diào)用時,計算圖會以拓?fù)漤樞虮惶幚韴?zhí)行.
這個處理過程是通過調(diào)用每個Function對象的backward()方法來完成的,
并且依次將返回得到的梯度傳遞到下一個Function對象.
Normally, the only way users interact with functions is by creating 
subclasses and defining new operations. This is a recommended 
way of extending torch.autograd.
一般而言,用戶和functions交互的唯一方式是創(chuàng)建一個子類,并定義新的操作.
這也是擴展torch.autograd推薦使用的方式.
Each function object is meant to be used only once (in the forward pass).
每個function對象只會被使用一次(在前向傳播過程中).

Examples:例子

>>> class Exp(Function):
>>>
>>>     @staticmethod
>>>     def forward(ctx, i):
>>>         result = i.exp()
>>>         ctx.save_for_backward(result)
>>>         return result
>>>
>>>     @staticmethod
>>>     def backward(ctx, grad_output):
>>>         result, = ctx.saved_tensors
>>>         return grad_output * result
static backward(ctx, *grad_outputs)
	Defines a formula for differentiating the operation.
	定義求導(dǎo)操作的公式.
	This function is to be overridden by all subclasses.
	這個函數(shù)將會被所有子類所重寫.
	It must accept a context ctx as the first argument, 
	followed by as many outputs did forward() return, 
	and it should return as many tensors, as there were inputs to forward(). 
	Each argument is the gradient w.r.t the given output, 
	and each returned value should be the gradient w.r.t. the corresponding input.
	它必須接收一個上下文ctx作為第一個參數(shù),
	然后接收forward()函數(shù)返回的所有參數(shù),
	而且它必須返回forward()函數(shù)接收的所有張量tensor.
	每個參數(shù)是相對于給定輸出的梯度.
	并且每個返回的值都應(yīng)該是相應(yīng)輸入的梯度.
	The context can be used to retrieve tensors saved during the forward pass. 
	It also has an attribute ctx.needs_input_grad as a tuple of booleans 
	representing whether each input needs gradient. E.g., 
	backward() will have ctx.needs_input_grad[0] = True if the first 
	input to forward() needs gradient computated w.r.t. the output.
	我們可以使用上下文context來獲取在前向傳遞過程中保存的張量.
	它同時具有屬性ctx.needs_input_grad,他是一個元素為布爾類型的元組,
	布爾值表示每個輸入數(shù)據(jù)是否需要梯度.舉個例子,
	如果forward()函數(shù)的第一個輸入數(shù)據(jù)需要根據(jù)輸出計算梯度,
	那么backward()中的屬性ctx.needs_input_grad[0] = True.
	

static forward(ctx, *args, **kwargs)
	Performs the operation.
	執(zhí)行操作.
	This function is to be overridden by all subclasses.
	該函數(shù)將會被所有子類所重寫.
	It must accept a context ctx as the first argument, 
	followed by any number of arguments (tensors or other types).
	它必需接收一個上下文ctx作為第一個參數(shù),
	然后可以接著接收任意數(shù)量的參數(shù)(張量或者其他類型)
	The context can be used to store tensors that can be then retrieved during the backward pass.
	上下文可以被用來保存張量,這樣就可以在后向傳遞的過程中獲取這些張量.

上述就是小編為大家分享的如何進行Function函數(shù)的分析了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI