Pytorch 實(shí)現(xiàn)自定義參數(shù)層的例子
注意,一般官方接口都帶有可導(dǎo)功能,如果你實(shí)現(xiàn)的層不具有可導(dǎo)功能,就需要自己實(shí)現(xiàn)梯度的反向傳遞。
官方Linear層:
class Linear(Module):
def __init__(self, in_features, out_features, bias=True):
super(Linear, self).__init__()
self.in_features = in_features
self.out_features = out_features
self.weight = Parameter(torch.Tensor(out_features, in_features))
if bias:
self.bias = Parameter(torch.Tensor(out_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(1))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input):
return F.linear(input, self.weight, self.bias)
def extra_repr(self):
return 'in_features={}, out_features={}, bias={}'.format(
self.in_features, self.out_features, self.bias is not None
)
實(shí)現(xiàn)view層
class Reshape(nn.Module):
def __init__(self, *args):
super(Reshape, self).__init__()
self.shape = args
def forward(self, x):
return x.view((x.size(0),)+self.shape)
實(shí)現(xiàn)LinearWise層
class LinearWise(nn.Module):
def __init__(self, in_features, bias=True):
super(LinearWise, self).__init__()
self.in_features = in_features
self.weight = nn.Parameter(torch.Tensor(self.in_features))
if bias:
self.bias = nn.Parameter(torch.Tensor(self.in_features))
else:
self.register_parameter('bias', None)
self.reset_parameters()
def reset_parameters(self):
stdv = 1. / math.sqrt(self.weight.size(0))
self.weight.data.uniform_(-stdv, stdv)
if self.bias is not None:
self.bias.data.uniform_(-stdv, stdv)
def forward(self, input):
x = input * self.weight
if self.bias is not None:
x = x + self.bias
return x
以上這篇Pytorch 實(shí)現(xiàn)自定義參數(shù)層的例子就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解python中DRF框架的數(shù)據(jù)校驗(yàn)方式
這篇文章主要為大家詳細(xì)介紹了python中DRF框架的數(shù)據(jù)校驗(yàn)方式,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以跟隨小編一起了解一下2023-10-10
python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘
這篇文章主要為大家詳細(xì)介紹了python+PyQT實(shí)現(xiàn)系統(tǒng)桌面時(shí)鐘,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-11-11
django開發(fā)post接口簡(jiǎn)單案例,獲取參數(shù)值的方法
今天小編就為大家分享一篇django開發(fā)post接口簡(jiǎn)單案例,獲取參數(shù)值的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2018-12-12
Python彩色化Linux的命令行終端界面的代碼實(shí)例分享
美化Linux的terminal終端顯示的方法多種多樣,這里我們給出一個(gè)利用Python彩色化Linux的命令行終端界面的代碼實(shí)例分享,包括一個(gè)Linux下簡(jiǎn)便執(zhí)行Python程序的方法,需要的朋友可以參考下2016-07-07
Python使用asyncio包處理并發(fā)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Python使用asyncio包處理并發(fā),asyncio包使用事件循環(huán)驅(qū)動(dòng)的協(xié)程實(shí)現(xiàn)并發(fā),本文通過實(shí)例代碼給大家介紹的非常詳細(xì)對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12
Matplotlib實(shí)戰(zhàn)之平行坐標(biāo)系繪制詳解
平行坐標(biāo)系是一種統(tǒng)計(jì)圖表,它包含多個(gè)垂直平行的坐標(biāo)軸,每個(gè)軸表示一個(gè)字段,并用刻度標(biāo)明范圍,下面我們就來看看如何繪制平行坐標(biāo)系吧2023-08-08
python pyinstaller打包exe報(bào)錯(cuò)的解決方法
這篇文章主要給大家介紹了關(guān)于python pyinstaller打包exe報(bào)錯(cuò)的解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
利用Python產(chǎn)生加密表和解密表的實(shí)現(xiàn)方法
這篇文章主要介紹了利用Python產(chǎn)生加密表和解密表的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10

