1. 程式人生 > 資料庫 >SQL中GROUP BY語句與HAVING語句的使用

SQL中GROUP BY語句與HAVING語句的使用

更多python教程請到: 菜鳥教程www.piaodoo.com

人人影視www.sfkyty.com

16影視www.591319.com

星辰影院www.591319.com


自定義Autograd函式

對於淺層的網路,我們可以手動的書寫前向傳播和反向傳播過程。但是當網路變得很大時,特別是在做深度學習時,網路結構變得複雜。前向傳播和反向傳播也隨之變得複雜,手動書寫這兩個過程就會存在很大的困難。幸運地是在pytorch中存在了自動微分的包,可以用來解決該問題。在使用自動求導的時候,網路的前向傳播會定義一個計算圖(computational graph),圖中的節點是張量(tensor),兩個節點之間的邊對應了兩個張量之間變換關係的函式。有了計算圖的存在,張量的梯度計算也變得容易了些。例如, x是一個張量,其屬性 x.requires_grad = True,那麼 x.grad就是一個儲存這個張量x的梯度的一些標量值。

最基礎的自動求導操作在底層就是作用在兩個張量上。前向傳播函式是從輸入張量到輸出張量的計算過程;反向傳播是輸入輸出張量的梯度(一些標量)並輸出輸入張量的梯度(一些標量)。在pytorch中我們可以很容易地定義自己的自動求導操作,通過繼承torch.autograd.Function並定義forward和backward函式。

forward(): 前向傳播操作。可以輸入任意多的引數,任意的python物件都可以。

backward():反向傳播(梯度公式)。輸出的梯度個數需要與所使用的張量個數保持一致,且返回的順序也要對應起來。

# Inherit from Function
class LinearFunction(Function):

Note that both forward and backward are @staticmethods

@staticmethod

bias is an optional argument

def forward(ctx, input, weight, bias=None):
# ctx在這裡類似self,ctx的屬性可以在backward中呼叫
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

@staticmethod
def 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.
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

呼叫自定義的自動求導函式

linear = LinearFunction.apply(args) #前向傳播
linear.backward()#反向傳播
linear.grad_fn.apply(
args)#反向傳播

對於非引數化的張量(權重是常量,不需要更新),此時可以定義為:

class MulConstant(Function):
  @staticmethod
  def forward(ctx, tensor, constant):
    # ctx is a context object that can be used to stash information
    # for backward computation
    ctx.constant = constant
    return tensor * constant

@staticmethod
def backward(ctx, grad_output):
# We return as many input gradients as there were arguments.
# Gradients of non-Tensor arguments to forward must be None.
return grad_output * ctx.constant, None

高階導數

grad_x =t.autograd.grad(y, x, create_graph=True)

grad_grad_x = t.autograd.grad(grad_x[0],x)

自定義Module

計算圖和自動求導在定義複雜網路和求梯度的時候非常好用,但對於大型的網路,這個還是有點偏底層。在我們構建網路的時候,經常希望將計算限制在每個層之內(引數更新分層更新)。而且在TensorFlow等其他深度學習框架中都提供了高階抽象結構。因此,在pytorch中也提供了類似的包nn,它定義了一組等價於層(layer)的模組(Modules)。一個Module接受輸入張量並得到輸出張量,同時也會包含可學習的引數。

有時候,我們希望運用一些新的且nn包中不存在的Module。此時就需要定義自己的Module了。自定義的Module需要繼承nn.Module且自定義forward函式。其中forward函式可以接受輸入張量並利用其它模型或者其他自動求導操作來產生輸出張量。但並不需要重寫backward函式,因此nn使用了autograd。這也就意味著,需要自定義Module, 都必須有對應的autograd函式以呼叫其中的backward。

class Linear(nn.Module):
  def __init__(self, input_features, output_features, bias=True):
    super(Linear, self).__init__()
    self.input_features = input_features
    self.output_features = output_features
# nn.Parameter is a special kind of Tensor, that will get
# automatically registered as Module's parameter once it's assigned
# as an attribute. Parameters and buffers need to be registered, or
# they won't appear in .parameters() (doesn't apply to buffers), and
# won't be converted when e.g. .cuda() is called. You can use
# .register_buffer() to register buffers.
# (很重要!!!引數一定需要梯度!)nn.Parameters require gradients by default.
self.weight = nn.Parameter(torch.Tensor(output_features, input_features))
if bias:
  self.bias = nn.Parameter(torch.Tensor(output_features))
else:
  # You should always register all possible parameters, but the
  # optional ones can be None if you want.
  self.register_parameter('bias', None)

# Not a very smart way to initialize weights
self.weight.data.uniform_(-0.1, 0.1)
if bias is not None:
  self.bias.data.uniform_(-0.1, 0.1)

def forward(self, input):
# See the autograd section for explanation of what happens here.
return LinearFunction.apply(input, self.weight, self.bias)

def extra_repr(self):
# (Optional)Set the extra information about this module. You can test
# it by printing an object of this class.
return 'in_features={}, out_features={}, bias={}'.format(
self.in_features, self.out_features, self.bias is not None

Function與Module的異同

Function與Module都可以對pytorch進行自定義拓展,使其滿足網路的需求,但這兩者還是有十分重要的不同:

Function一般只定義一個操作,因為其無法儲存引數,因此適用於啟用函式、pooling等操作;Module是儲存了引數,因此適合於定義一層,如線性層,卷積層,也適用於定義一個網路

Function需要定義三個方法:init, forward, backward(需要自己寫求導公式);Module:只需定義init和forward,而backward的計算由自動求導機制構成

可以不嚴謹的認為,Module是由一系列Function組成,因此其在forward的過程中,Function和Variable組成了計算圖,在backward時,只需呼叫Function的backward就得到結果,因此Module不需要再定義backward。

Module不僅包括了Function,還包括了對應的引數,以及其他函式與變數,這是Function所不具備的。

module 是 pytorch 組織神經網路的基本方式。Module 包含了模型的引數以及計算邏輯。Function 承載了實際的功能,定義了前向和後向的計算邏輯。

Module 是任何神經網路的基類,pytorch 中所有模型都必需是 Module 的子類。 Module 可以套嵌,構成樹狀結構。一個 Module 可以通過將其他 Module 做為屬性的方式,完成套嵌。

Function 是 pytorch 自動求導機制的核心類。Function 是無引數或者說無狀態的,它只負責接收輸入,返回相應的輸出;對於反向,它接收輸出相應的梯度,返回輸入相應的梯度。

在呼叫loss.backward()時,使用的是Function子類中定義的backward()函式。

以上這篇Pytorch: 自定義網路層例項就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援菜鳥教程www.piaodoo.com。