【笔记】torch.nn.identity()方法详解:放到最后一层后面显得没有那么空虚,因为前面的层后面都有个激活函数,就最后一层后面啥都没有所以放个Identity占位

eg:

        print(x[0,0,3:10])
        x = self.norm(x)
        print(x[0,0,3:10])
tensor([ 0.0023,  0.0320,  0.7124, -0.0062, -0.5222, -0.6546, -0.1880],
       grad_fn=<SliceBackward>)
tensor([ 0.0023,  0.0320,  0.7124, -0.0062, -0.5222, -0.6546, -0.1880],
       grad_fn=<SliceBackward>)

正文:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
好的,下面分别给出基于`torch.nn.Module`和`torch.autograd.Function`的实现示例。 基于`torch.nn.Module`的实现示例: ```python import torch import torch.nn as nn class LinearLayer(nn.Module): def __init__(self, input_size, output_size): super(LinearLayer, self).__init__() self.weight = nn.Parameter(torch.Tensor(output_size, input_size)) self.bias = nn.Parameter(torch.Tensor(output_size)) self.reset_parameters() def reset_parameters(self): nn.init.kaiming_uniform_(self.weight, a=math.sqrt(5)) if self.bias is not None: fan_in, _ = nn.init._calculate_fan_in_and_fan_out(self.weight) bound = 1 / math.sqrt(fan_in) nn.init.uniform_(self.bias, -bound, bound) def forward(self, input): return torch.matmul(input, self.weight.t()) + self.bias ``` 基于`torch.autograd.Function`的实现示例: ```python import torch from torch.autograd import Function class LinearFunction(Function): @staticmethod def forward(ctx, input, weight, bias=None): ctx.save_for_backward(input, weight, bias) output = torch.matmul(input, weight.t()) if bias is not None: output += bias.unsqueeze(0).expand_as(output) return output @staticmethod def backward(ctx, grad_output): input, weight, bias = ctx.saved_tensors grad_input = grad_weight = grad_bias = None if ctx.needs_input_grad[0]: grad_input = torch.matmul(grad_output, weight) if ctx.needs_input_grad[1]: grad_weight = torch.matmul(grad_output.t(), input) if bias is not None and ctx.needs_input_grad[2]: grad_bias = grad_output.sum(0) return grad_input, grad_weight, grad_bias class LinearLayer(Function): @staticmethod def forward(ctx, input, weight, bias=None): ctx.save_for_backward(input, weight, bias) output = torch.matmul(input, weight.t()) if bias is not None: output += bias.unsqueeze(0).expand_as(output) return output @staticmethod def backward(ctx, grad_output): input, weight, bias = ctx.saved_tensors grad_input = grad_weight = grad_bias = None if ctx.needs_input_grad[0]: grad_input = torch.matmul(grad_output, weight) if ctx.needs_input_grad[1]: grad_weight = torch.matmul(grad_output.t(), input) if bias is not None and ctx.needs_input_grad[2]: grad_bias = grad_output.sum(0) return grad_input, grad_weight, grad_bias ``` 这两个示例分别基于`torch.nn.Module`和`torch.autograd.Function`实现了一个Linear。你可以根据需要选择其中一种实现方式。希望对你有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序猿的探索之路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值