PyTorch入门例程

这是一个两层权连接网络的实现。迭代500次。其中定义了自己的MyReLU函数,通过编写forward backward函数,可以直接调用该函数。

#PyTorch:Defineing new autograd functions
# -*- coding: utf-8 -*-
import torch

class MyReLU(torch.autograd.Function):
    @staticmethod
    def forward(ctx,input):
        
        ctx.save_for_backward(input)
        return input.clamp(min=0)
        
    @staticmethod
    def backward(ctx,grad_output):
        input,=ctx.saved_tensors
        grad_input=grad_output.clone()
        grad_input[input<0]=0
        return grad_input
dtype=torch.float
device=torch.device('cpu')

# 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 input and outputs.
x = torch.randn(N, D_in, device=device, dtype=dtype)
y = torch.randn(N, D_out, device=device, dtype=dtype)

# Create random Tensors for weights.
w1 = torch.randn(D_in, H, device=device, dtype=dtype, requires_grad=True)
w2 = torch.randn(H, D_out, device=device, dtype=dtype, requires_grad=True)

lr=1e-6
for t in range(500):
    relu=MyReLU.apply
    
    y_pred=relu(x.mm(w1)).mm(w2)
    
    loss=(y_pred-y).pow(2).sum()
    if(t%100==99):
        print(t,loss.item())
        
    #backward:计算带有require_grad=true的tensor将其梯度保存至  .grad中
    loss.backward()
    
    #upgrade:此阶段不必再计算w1w2的梯度,所以需要在no_grad中计算
    with torch.no_grad():
        w1-=lr*w1.grad
        w2-=lr*w2.grad
        
        #更新完将gradients手动置零
        w1.grad.zero_()
        w2.grad.zero_()



output:
99 1060.0052490234375
199 7.554439067840576
299 0.08785037696361542
399 0.001566882710903883
499 0.00013583201507572085

通过编写这种简单的程序,加深了对于层内前向,后向操作的理解。

你可能感兴趣的:(PyTorch)