您好,登錄后才能下訂單哦!
本篇內容介紹了“C語言擴展怎么實現”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
Extending torch.autograd 擴展torch.autograd Adding operations to autograd requires implementing a new Function subclass for each operation. Recall that Function s are what autograd uses to compute the results and gradients, and encode the operation history. Every new function requires you to implement 2 methods: 向autograd自動梯度中添加操作需要我們為每個操作實現一個新的Function子類. 我們知道,autograd使用Function來計算結果和計算梯度,并且編碼操作歷史. 每一個新的function需要我們實現兩個方法: forward() - the code that performs the operation. It can take as many arguments as you want, with some of them being optional, if you specify the default values. All kinds of Python objects are accepted here. Tensor arguments that track history (i.e., with requires_grad=True) will be converted to ones that don’t track history before the call, and their use will be registered in the graph. Note that this logic won’t traverse lists/dicts/any other data structures and will only consider Tensor s that are direct arguments to the call. You can return either a single Tensor output, or a tuple of Tensor s if there are multiple outputs. Also, please refer to the docs of Function to find descriptions of useful methods that can be called only from forward(). forward()方法 - 執行操作的代碼.它可以接收你需要的任意數量的參數,如果你指定默認 值,可以將其中部分設置成可選參數.這里可以接收任意Python對象. 跟蹤歷史的(即requires_grad=True) 張量Tensor參數在該函數調用之前將會被轉換成 不跟蹤歷史的張量,并且他們的使用會被登記注冊到計算圖中.注意這個邏輯不會遍歷 列表/字典/以及其他任何數據結構,并且只會作用于作為參數直接傳遞給該函數調用的 張量. 你可以返回單個張量Tensor作為函數輸出,或者返回張量構成的元組作為函數的 多個輸出.同時你可以參閱Function文檔, 在文檔中你可以找到更多信息,它們介紹了一 些只能在forward()函數中使用的好用的方法. backward() - gradient formula. It will be given as many Tensor arguments as there were outputs, with each of them representing gradient w.r.t. that output. It should return as many Tensor s as there were inputs, with each of them containing the gradient w.r.t. its corresponding input. If your inputs didn’t require gradient (needs_input_grad is a tuple of booleans indicating whether each input needs gradient computation), or were non-Tensor objects, you can return None. Also, if you have optional arguments to forward() you can return more gradients than there were inputs, as long as they’re all None. backward() - 梯度公式. 這個方法接收一定數量的Tensor張量參數,參數的數量,就是這 個運算操作的輸出數據數量(即前向傳遞函數輸出數據的數量),并且這個函數接收的參 數就是相對于輸出數據(前向傳遞的輸出數據)的梯度. 該方法也返回一定數量的 Tensor張量參數,參數的數量就是輸入數據(前向傳遞的輸入數據,也就是forward函數接 收參數的數量)的數量,并且它的值是相對于輸入數據的梯度.如果你的數據不需要梯度 (needs_input_grad是一個布爾類型構成的元組,他表示輸入的每個數據是否需要計算梯 度), 或者是非張量的對象,你可以返回None. 同樣,如果有可選參數傳遞到forward(),那 么你可以返回比輸入數據更多數量的梯度,只要把他們設置成None即可. Below you can find code for a Linear function from torch.nn, with additional comments: 以下內容你可以看到torch.nn庫中Linear 函數的代碼:
# Inherit from Function# 繼承Functionclass LinearFunction(Function):# Note that both forward and backward are @staticmethods# 注意forward方法和backward方法都需要用@staticmethod來裝飾@staticmethod# bias is an optional argument# bias 是可選參數def forward(ctx, input, weight, bias=None):ctx.save_for_backward(input, weight, bias)output = input.mm(weight.t())if bias is not None:output += bias.unsqueeze(0).expand_as(output)return output# This function has only a single output, so it gets only one gradient# 該函數只有單個輸出,因此他只會接收一個梯度@staticmethoddef backward(ctx, grad_output):# This is a pattern that is very convenient - at the top of backward# unpack saved_tensors and initialize all gradients w.r.t. inputs to# None. Thanks to the fact that additional trailing Nones are# ignored, the return statement is simple even when the function has# optional inputs.# 這是一個非常方便的模式,在backward函數開頭解包saved_tensors# 然后初始化相對于輸入的梯度,將他們設置成None# 由于尾部多余的None值會被忽略,因此盡管函數有可選參數,# 返回語句依然很簡單.input, weight, bias = ctx.saved_tensors grad_input = grad_weight = grad_bias = None# These needs_input_grad checks are optional and there only to# improve efficiency. If you want to make your code simpler, you can# skip them. Returning gradients for inputs that don't require it is# not an error.# if ctx.needs_input_grad[0]:grad_input = grad_output.mm(weight)if ctx.needs_input_grad[1]:grad_weight = grad_output.t().mm(input)if bias is not None and ctx.needs_input_grad[2]:grad_bias = grad_output.sum(0).squeeze(0)return grad_input, grad_weight, grad_bias
“C語言擴展怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。