《动手学深度学习 Pytorch版》 5.4 自定义层

5.4.1 不带参数的层

python 复制代码
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]))  # 可以正常运行
复制代码
tensor([-2., -1.,  0.,  1.,  2.])
python 复制代码
net = nn.Sequential(nn.Linear(8, 128), CenteredLayer())  # 可以将其组合进更复杂的模型中

Y = net(torch.rand(4, 8))
Y.mean()  # 均值为0,可以说明正常(由于浮点数特性,无法严格为0)
复制代码
tensor(2.3283e-09, grad_fn=<MeanBackward0>)

5.4.2 带参数的层

python 复制代码
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, linear(torch.rand(2, 5))  # 测试正常
复制代码
(Parameter containing:
 tensor([[ 0.1349, -1.0820, -0.8273],
         [-0.2127, -1.0748, -0.5577],
         [-0.9080,  0.5133, -0.4401],
         [-0.4881, -0.8850, -0.0289],
         [-0.0330, -1.4289, -0.6820]], requires_grad=True),
 tensor([[2.6406, 0.0000, 0.0000],
         [1.7090, 0.0000, 0.0000]]))
python 复制代码
net = nn.Sequential(MyLinear(64, 8), MyLinear(8, 1))  # 可正常组合使用
net(torch.rand(2, 64))
复制代码
tensor([[0.0000],
        [2.0065]])

练习

(1)设计一个接收输入并计算张量降维的层,它返回 y k = ∑ i , j W i j k x i x j y_k=\sum_{i,j}W_{ijk}x_ix_j yk=∑i,jWijkxixj。

python 复制代码
class DimensionalityReductionTensors(nn.Module):
    def __init__(self, in_units):
        super().__init__()
        self.weight = nn.Parameter(torch.randn(in_units, in_units, in_units))  # 初始化权重
    def forward(self, X):
        y = torch.zeros_like(X)
        for k in range(0, X.shape[0]):
            for i in range(0, X.shape[0]):
                for j in range(0, X.shape[0]):
                    y[k] += self.weight[i][j][k] * X[i] * X[j]
        return y
    
layer = DimensionalityReductionTensors(3)
layer.weight, layer(torch.rand(3, 1))  # 测试正常
复制代码
(Parameter containing:
 tensor([[[-0.3133,  0.6214,  0.6835],
          [-0.0720,  0.8630,  1.1317],
          [-0.5848, -0.9351, -0.5103]],
 
         [[ 0.2724,  0.3710, -0.6909],
          [ 1.1569,  0.8924, -0.2694],
          [-0.2803, -1.0708,  0.5445]],
 
         [[-0.4135,  1.2961, -0.2054],
          [-0.5572,  1.0026,  0.7997],
          [ 0.3076,  0.4108,  0.4654]]], requires_grad=True),
 tensor([[0.3252],
         [0.6745],
         [0.1474]], grad_fn=<CopySlices>))

(2)设计一个返回输入数据的傅里叶系数前半部分的层。

python 复制代码
class FirstHalfFouriercoefficient(nn.Module):
    def __init__(self):
        super().__init__()
    def forward(self, X):
        FFT = torch.fft.fft(X)
        return FFT[:, :round(X.shape[1]/2)]
    
layer = FirstHalfFouriercoefficient()
layer(torch.rand(3, 7))  # 测试正常
复制代码
tensor([[ 3.9421+0.0000j, -0.2037+0.1165j, -0.6413-1.0152j, -0.4512-0.3608j],
        [ 3.5853+0.0000j, -0.4951-0.8863j, -0.0675+0.4883j,  0.6614+0.0268j],
        [ 4.0120+0.0000j,  0.9124+0.1392j, -0.5101+0.0387j,  0.9813+0.3515j]])
相关推荐
Patrick在香港5 分钟前
同一行 strftime,两台机器跑出两种日期
python·datetime·时区·locale·日期处理
2601_9679873715 分钟前
用Python+OpenCV+YOLO写个游戏脚本:从自动瞄准到自动采集的保姆级教程
python·opencv·yolo·目标检测·游戏自动化
meiying883815 分钟前
快递单号批量查询Python脚本:从手动到自动化的完整实现
开发语言·python·自动化
在所不辞兄18 分钟前
【零基础学智能仿真-63】ABACUS作业工具与MCP接口
人工智能·深度学习·神经网络·机器学习·智能仿真
人工智能导论实践课28 分钟前
【GUI桌面开发3】PyCharm配置外部工具
ide·python·pycharm
一千柯橘35 分钟前
LangChain 的实时输出处理: stream_events 和 Streaming
python·langchain·agent
贾公子39 分钟前
图片 AIGC 检测接入实战——URL 与 Base64 的取舍、体积校验与「你送过去的可能不是那张图」
python·aigc
SEO_juper1 小时前
Node.js 重定向链检测工具:301 跳了几跳、有没有成环,一次看清
xml·开发语言·python·node.js·json·bash
曲径海盗1 小时前
【Python 学习第 37 天】for 循环
python·学习笔记·编程入门
心静财富之门1 小时前
Python 数据共享四种方案完整实例(同进程 / JSON / Redis / HTTP)
数据库·python·oracle