亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Pytorch 實現(xiàn)自定義參數(shù)層的例子

 更新時間:2019年08月17日 14:13:08   作者:青盞  
今天小編就為大家發(fā)信息一篇Pytorch 實現(xiàn)自定義參數(shù)層的例子,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

注意,一般官方接口都帶有可導功能,如果你實現(xiàn)的層不具有可導功能,就需要自己實現(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
    )

實現(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)

實現(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 實現(xiàn)自定義參數(shù)層的例子就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • 利用python將?Matplotlib?可視化插入到?Excel表格中

    利用python將?Matplotlib?可視化插入到?Excel表格中

    這篇文章主要介紹了利用python將?Matplotlib?可視化?插入到?Excel?表格中,通過使用xlwings模塊來控制Excel插入圖表,具體詳細需要的朋友可以參考下面文章內容
    2022-06-06
  • 如何利用pygame實現(xiàn)打飛機小游戲

    如何利用pygame實現(xiàn)打飛機小游戲

    pygame是python的一個做游戲的庫,非常適合做游戲開發(fā),這篇文章主要給大家介紹了關于如何利用pygame實現(xiàn)打飛機小游戲的相關資料,需要的朋友可以參考下
    2021-05-05
  • python 求兩個向量的順時針夾角操作

    python 求兩個向量的順時針夾角操作

    這篇文章主要介紹了python 求兩個向量的順時針夾角操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-03-03
  • Python的logging.config模塊操作步驟

    Python的logging.config模塊操作步驟

    這篇文章主要介紹了Python的logging.config模塊操作步驟,本文通過示例代碼給大家介紹的非常詳細對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-08-08
  • Python設計模式之建造者模式實例詳解

    Python設計模式之建造者模式實例詳解

    這篇文章主要介紹了Python設計模式之建造者模式,簡單說明了建造者模式的概念、原理,并結合實例形式分析了Python定義及使用建造者模式相關操作技巧,需要的朋友可以參考下
    2019-01-01
  • Qt調用Python詳細圖文過程記錄

    Qt調用Python詳細圖文過程記錄

    Qt調用python實際上就是c++調python,網(wǎng)上搜會出來很多,介紹得也比較全,這里做個記錄,下面這篇文章主要給大家介紹了關于Qt調用Python詳細圖文過程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-05-05
  • python淘寶準點秒殺搶單的實現(xiàn)示例

    python淘寶準點秒殺搶單的實現(xiàn)示例

    為了想要搶到想要的商品,想了個用Python實現(xiàn)python淘寶準點秒殺搶單方案,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • Anaconda安裝opencv庫詳細圖文教程

    Anaconda安裝opencv庫詳細圖文教程

    這篇文章主要給大家介紹了關于Anaconda安裝opencv庫詳細圖文教程的相關資料,安裝Anaconda后,你可以使用conda命令在Anaconda環(huán)境中安裝OpenCV,文中有詳細步驟,需要的朋友可以參考下
    2023-07-07
  • python中upper是做什么用的

    python中upper是做什么用的

    在本篇文章里小編給大家整理的是一篇關于python中upper的作用的相關文章,有需要的朋友們可以參考下。
    2020-07-07
  • python中多個裝飾器的調用順序詳解

    python中多個裝飾器的調用順序詳解

    這篇文章主要給大家介紹了關于python中多個裝飾器的調用順序,文中通過示例代碼介紹的非常詳細,對大家學習或者使用Python具有一定的參考學習價值,需要的朋友們下面來一起學習學習吧
    2019-07-07

最新評論