学习笔记:动手学深度学习 22 自定义层

不带参数的层

Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)]
Type 'copyright', 'credits' or 'license' for more information
IPython 7.22.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 7.22.0
Python 3.8.8 (default, Apr 13 2021, 15:08:03) [MSC v.1916 64 bit (AMD64)] on win32
import torch
import torch.nn.functional as F
from torch import nn
class CenteredLayer(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(self, X):
        return X - X.mean()#把均值减掉
    
layer = CenteredLayer()
layer(torch.FloatTensor([1, 2, 3, 4, 5]))
Out[4]: tensor([-2., -1.,  0.,  1.,  2.])
net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())
Y = net(torch.rand(4, 8))
Y.mean()
Out[7]: tensor(6.9849e-10, grad_fn=<MeanBackward0>)

带参数的层

class MyLinear(nn.Module):
    def __init__(self, in_units, units):
        super().__init__()
        self.weight = nn.Parameter(torch.randn(in_units, units))
        self.bias = nn.Parameter(torch.randn(units,))
    def forward(self, X):
        linear = torch.matmul(X, self.weight.data) + self.bias.data
        return F.relu(linear)
    
linear = MyLinear(5, 3)
linear.weight
Out[9]: 
Parameter containing:
tensor([[ 0.6766, -0.1331, -0.1324],
        [ 0.1136,  1.8299,  0.5271],
        [ 0.4939, -1.7403, -0.9208],
        [ 0.2670,  0.4728, -0.4918],
        [ 0.1707, -1.2526, -1.1154]], requires_grad=True)

自定义层

class MyLinear(nn.Module):
    def __init__(self, in_units, units):
        super().__init__()
        self.weight = nn.Parameter(torch.randn(in_units, units))
        self.bias = nn.Parameter(torch.randn(units,))
    def forward(self, X):
        linear = torch.matmul(X, self.weight.data) + self.bias.data
        return F.relu(linear)
    
linear = MyLinear(5, 3)
linear.weight
Out[9]: 
Parameter containing:
tensor([[ 0.6766, -0.1331, -0.1324],
        [ 0.1136,  1.8299,  0.5271],
        [ 0.4939, -1.7403, -0.9208],
        [ 0.2670,  0.4728, -0.4918],
        [ 0.1707, -1.2526, -1.1154]], requires_grad=True)

使用自定义层进行正向传播计算

linear(torch.rand(2, 5))
Out[10]: 
tensor([[2.7365, 2.7208, 0.0000],
        [2.6007, 4.7692, 0.2059]])

使用自定义层构建模型

net = nn.Sequential(MyLinear(64, 8), MyLinear(8, 1))
net(torch.rand(2, 64))
Out[11]: 
tensor([[6.2113],
        [4.6728]])

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值