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,
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值