成人午夜视频全免费观看高清-秋霞福利视频一区二区三区-国产精品久久久久电影小说-亚洲不卡区三一区三区一区

C語言擴(kuò)展怎么實(shí)現(xiàn)

本篇內(nèi)容介紹了“C語言擴(kuò)展怎么實(shí)現(xiàn)”的有關(guān)知識(shí),在實(shí)際案例的操作過程中,不少人都會(huì)遇到這樣的困境,接下來就讓小編帶領(lǐng)大家學(xué)習(xí)一下如何處理這些情況吧!希望大家仔細(xì)閱讀,能夠?qū)W有所成!

北林網(wǎng)站制作公司哪家好,找創(chuàng)新互聯(lián)建站!從網(wǎng)頁設(shè)計(jì)、網(wǎng)站建設(shè)、微信開發(fā)、APP開發(fā)、響應(yīng)式網(wǎng)站建設(shè)等網(wǎng)站項(xiàng)目制作,到程序開發(fā),運(yùn)營維護(hù)。創(chuàng)新互聯(lián)建站2013年至今到現(xiàn)在10年的時(shí)間,我們擁有了豐富的建站經(jīng)驗(yàn)和運(yùn)維經(jīng)驗(yàn),來保證我們的工作的順利進(jìn)行。專注于網(wǎng)站建設(shè)就選創(chuàng)新互聯(lián)建站

Extending torch.autograd 擴(kuò)展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自動(dòng)梯度中添加操作需要我們?yōu)槊總€(gè)操作實(shí)現(xiàn)一個(gè)新的Function子類.
我們知道,autograd使用Function來計(jì)算結(jié)果和計(jì)算梯度,并且編碼操作歷史.
每一個(gè)新的function需要我們實(shí)現(xiàn)兩個(gè)方法:
	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()方法 - 執(zhí)行操作的代碼.它可以接收你需要的任意數(shù)量的參數(shù),如果你指定默認(rèn)
	值,可以將其中部分設(shè)置成可選參數(shù).這里可以接收任意Python對(duì)象.
	跟蹤歷史的(即requires_grad=True) 張量Tensor參數(shù)在該函數(shù)調(diào)用之前將會(huì)被轉(zhuǎn)換成
	不跟蹤歷史的張量,并且他們的使用會(huì)被登記注冊(cè)到計(jì)算圖中.注意這個(gè)邏輯不會(huì)遍歷
	列表/字典/以及其他任何數(shù)據(jù)結(jié)構(gòu),并且只會(huì)作用于作為參數(shù)直接傳遞給該函數(shù)調(diào)用的
	張量. 你可以返回單個(gè)張量Tensor作為函數(shù)輸出,或者返回張量構(gòu)成的元組作為函數(shù)的
	多個(gè)輸出.同時(shí)你可以參閱Function文檔, 在文檔中你可以找到更多信息,它們介紹了一
	些只能在forward()函數(shù)中使用的好用的方法.
	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() - 梯度公式. 這個(gè)方法接收一定數(shù)量的Tensor張量參數(shù),參數(shù)的數(shù)量,就是這
	個(gè)運(yùn)算操作的輸出數(shù)據(jù)數(shù)量(即前向傳遞函數(shù)輸出數(shù)據(jù)的數(shù)量),并且這個(gè)函數(shù)接收的參
	數(shù)就是相對(duì)于輸出數(shù)據(jù)(前向傳遞的輸出數(shù)據(jù))的梯度. 該方法也返回一定數(shù)量的
	Tensor張量參數(shù),參數(shù)的數(shù)量就是輸入數(shù)據(jù)(前向傳遞的輸入數(shù)據(jù),也就是forward函數(shù)接
	收參數(shù)的數(shù)量)的數(shù)量,并且它的值是相對(duì)于輸入數(shù)據(jù)的梯度.如果你的數(shù)據(jù)不需要梯度
	(needs_input_grad是一個(gè)布爾類型構(gòu)成的元組,他表示輸入的每個(gè)數(shù)據(jù)是否需要計(jì)算梯
	度),	或者是非張量的對(duì)象,你可以返回None. 同樣,如果有可選參數(shù)傳遞到forward(),那
	么你可以返回比輸入數(shù)據(jù)更多數(shù)量的梯度,只要把他們?cè)O(shè)置成None即可.

Below you can find code for a Linear function from torch.nn, with additional comments:
以下內(nèi)容你可以看到torch.nn庫中Linear 函數(shù)的代碼:
# Inherit from Function# 繼承Functionclass LinearFunction(Function):# Note that both forward and backward are @staticmethods# 注意forward方法和backward方法都需要用@staticmethod來裝飾@staticmethod# bias is an optional argument# bias 是可選參數(shù)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# 該函數(shù)只有單個(gè)輸出,因此他只會(huì)接收一個(gè)梯度@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.# 這是一個(gè)非常方便的模式,在backward函數(shù)開頭解包saved_tensors# 然后初始化相對(duì)于輸入的梯度,將他們?cè)O(shè)置成None# 由于尾部多余的None值會(huì)被忽略,因此盡管函數(shù)有可選參數(shù),# 返回語句依然很簡單.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語言擴(kuò)展怎么實(shí)現(xiàn)”的內(nèi)容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業(yè)相關(guān)的知識(shí)可以關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編將為大家輸出更多高質(zhì)量的實(shí)用文章!

新聞名稱:C語言擴(kuò)展怎么實(shí)現(xiàn)
文章位置:http://jinyejixie.com/article38/gpsgpp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供自適應(yīng)網(wǎng)站、云服務(wù)器、ChatGPT、電子商務(wù)、微信公眾號(hào)標(biāo)簽優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

綿陽服務(wù)器托管
孟村| 新丰县| 信阳市| 鄂托克前旗| 达州市| 方山县| 龙海市| 平罗县| 崇信县| 乌恰县| 万盛区| 伊春市| 曲松县| 徐闻县| 盖州市| 高阳县| 阿勒泰市| 新田县| 北辰区| 新郑市| 忻州市| 布尔津县| 潮安县| 仲巴县| 建宁县| 淅川县| 皮山县| 福海县| 禄劝| 台南市| 恭城| 新晃| 贵州省| 昂仁县| 贵定县| 岳普湖县| 明溪县| 景谷| 措美县| 随州市| 天台县|