欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

pytorch動態(tài)網(wǎng)絡(luò)以及權(quán)重共享實例

 更新時間:2020年01月06日 15:41:29   作者:bob_chen_csdn  
今天小編就為大家分享一篇pytorch動態(tài)網(wǎng)絡(luò)以及權(quán)重共享實例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

pytorch 動態(tài)網(wǎng)絡(luò)+權(quán)值共享

pytorch以動態(tài)圖著稱,下面以一個栗子來實現(xiàn)動態(tài)網(wǎng)絡(luò)和權(quán)值共享技術(shù):

# -*- coding: utf-8 -*-
import random
import torch


class DynamicNet(torch.nn.Module):
  def __init__(self, D_in, H, D_out):
    """
    這里構(gòu)造了幾個向前傳播過程中用到的線性函數(shù)
    """
    super(DynamicNet, self).__init__()
    self.input_linear = torch.nn.Linear(D_in, H)
    self.middle_linear = torch.nn.Linear(H, H)
    self.output_linear = torch.nn.Linear(H, D_out)

  def forward(self, x):
    """
    For the forward pass of the model, we randomly choose either 0, 1, 2, or 3
    and reuse the middle_linear Module that many times to compute hidden layer
    representations.

    Since each forward pass builds a dynamic computation graph, we can use normal
    Python control-flow operators like loops or conditional statements when
    defining the forward pass of the model.

    Here we also see that it is perfectly safe to reuse the same Module many
    times when defining a computational graph. This is a big improvement from Lua
    Torch, where each Module could be used only once.
    這里中間層每次向前過程中都是隨機添加0-3層,而且中間層都是使用的同一個線性層,這樣計算時,權(quán)值也是用的同一個。
    """
    h_relu = self.input_linear(x).clamp(min=0)
    for _ in range(random.randint(0, 3)):
      h_relu = self.middle_linear(h_relu).clamp(min=0)
    y_pred = self.output_linear(h_relu)
    return y_pred


    # N is batch size; D_in is input dimension;
    # H is hidden dimension; D_out is output dimension.
    N, D_in, H, D_out = 64, 1000, 100, 10

    # Create random Tensors to hold inputs and outputs
    x = torch.randn(N, D_in)
    y = torch.randn(N, D_out)

    # Construct our model by instantiating the class defined above
    model = DynamicNet(D_in, H, D_out)

    # Construct our loss function and an Optimizer. Training this strange model with
    # vanilla stochastic gradient descent is tough, so we use momentum
    criterion = torch.nn.MSELoss(reduction='sum')
    optimizer = torch.optim.SGD(model.parameters(), lr=1e-4, momentum=0.9)
    for t in range(500):
      # Forward pass: Compute predicted y by passing x to the model
      y_pred = model(x)

      # Compute and print loss
      loss = criterion(y_pred, y)
      print(t, loss.item())

      # Zero gradients, perform a backward pass, and update the weights.
      optimizer.zero_grad()
      loss.backward()
      optimizer.step()

這個程序?qū)嶋H上是一種RNN結(jié)構(gòu),在執(zhí)行過程中動態(tài)的構(gòu)建計算圖

References: Pytorch Documentations.

以上這篇pytorch動態(tài)網(wǎng)絡(luò)以及權(quán)重共享實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Python?matplotlib之折線圖的各種樣式與畫法總結(jié)

    Python?matplotlib之折線圖的各種樣式與畫法總結(jié)

    matplotlib是Python中的一個第三方庫,主要用于開發(fā)2D圖表,以漸進式、交互式的方式實現(xiàn)數(shù)據(jù)可視化,可以更直觀的呈現(xiàn)數(shù)據(jù),使數(shù)據(jù)更具說服力,下面這篇文章主要給大家介紹了關(guān)于Python?matplotlib之折線圖的各種樣式與畫法的相關(guān)資料,需要的朋友可以參考下
    2022-12-12
  • Python實現(xiàn)識別圖片為文字的示例代碼

    Python實現(xiàn)識別圖片為文字的示例代碼

    這篇文章主要為大家詳細介紹了Python如何不調(diào)用三方收費接口,照樣實現(xiàn)識別圖片為文字的功能。文中的示例代碼講解詳細,感興趣的可以了解一下
    2022-08-08
  • Restful_framework視圖組件代碼實例解析

    Restful_framework視圖組件代碼實例解析

    這篇文章主要介紹了Restful_framework視圖組件代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2020-11-11
  • Python中 map()函數(shù)的用法詳解

    Python中 map()函數(shù)的用法詳解

    map( )函數(shù)在算法題目里面經(jīng)常出現(xiàn),map( )會根據(jù)提供的函數(shù)對指定序列做映射,在寫返回值等需要轉(zhuǎn)換的時候比較常用。這篇文章主要介紹了Python中 map()的用法,需要的朋友可以參考下
    2018-07-07
  • python Dejavu庫快速識別音頻指紋實例探究

    python Dejavu庫快速識別音頻指紋實例探究

    這篇文章主要為大家介紹了python Dejavu庫快速識別音頻指紋實例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2024-01-01
  • 解決python虛擬環(huán)境切換無效的問題

    解決python虛擬環(huán)境切換無效的問題

    這篇文章主要介紹了解決python虛擬環(huán)境切換無效的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-04-04
  • opencv python 基于KNN的手寫體識別的實例

    opencv python 基于KNN的手寫體識別的實例

    這篇文章主要介紹了opencv python 基于KNN的手寫體識別的實例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-08-08
  • Python實現(xiàn)提高運行速度的技巧分享

    Python實現(xiàn)提高運行速度的技巧分享

    這篇文章主要為大家詳細介紹了Python實現(xiàn)提高運行速度的相關(guān)技巧,文中的示例代碼講解詳細,具有一定的參考價值,感興趣的小伙伴可以跟隨小編一起了解一下
    2023-06-06
  • 親手教你實現(xiàn)pynq-z2條形碼識別功能

    親手教你實現(xiàn)pynq-z2條形碼識別功能

    這篇文章主要介紹了pynq-z2條形碼識別功能,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-07-07
  • python安裝自己的代碼到site-packages問題

    python安裝自己的代碼到site-packages問題

    這篇文章主要介紹了python安裝自己的代碼到site-packages問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-11-11

最新評論