samout 新设计参数共享解决初始loss 大的问题

python 复制代码
import torch


class MaxState(torch.nn.Module):
    def __init__(self, hidden_dim, heads):
        super(MaxState, self).__init__()

        assert hidden_dim % heads == 0, "Hidden size must be divisible by the number of heads."

        self.head_size = hidden_dim // heads
        self.head0 = torch.nn.Linear(hidden_dim, hidden_dim, bias=False)
        self.head1 = torch.nn.Linear(hidden_dim, hidden_dim, bias=False)
        self.head2 = torch.nn.Linear(hidden_dim, hidden_dim, bias=False)

        self.head_num = heads

        self.hidden = hidden_dim

    def forward(self, input_data, state=None):
        b, s, k, h = input_data.shape[0], input_data.shape[1], self.head_num, self.head_size

        out = self.head0(input_data)

        out1 = self.head1(input_data)

        out2 = self.head2(input_data)

        out = out.reshape([b, s, k, h]).permute([0, 2, 1, 3])
        out1 = out1.reshape([b, s, k, h]).permute([0, 2, 1, 3])

        out = torch.cummax((out + out1) / h ** 0.5, 2)[0]

        out = out.permute([0, 2, 1, 3])
        out1 = out1.permute([0, 2, 1, 3])

        out = out.reshape([b, s, -1])
        out1 = out1.reshape([b, s, -1])

        out = (out + out2) * out + out1

        return out, state


class FeedForward(torch.nn.Module):
    def __init__(self, hidden_size):
        super(FeedForward, self).__init__()

        self.ffn1 = torch.nn.Linear(hidden_size, hidden_size // 2)
        self.ffn2 = torch.nn.Linear(hidden_size // 2, hidden_size)
        self.gate = torch.nn.Linear(hidden_size, hidden_size // 2)

        self.relu = torch.nn.ReLU()

    def forward(self, x):
        x1 = self.ffn1(x)
        x2 = self.relu(self.gate(x))
        xx = x1 * x2
        x = self.ffn2(xx)
        return x


class DecoderLayer(torch.nn.Module):
    def __init__(self, hidden_size, num_heads):
        super(DecoderLayer, self).__init__()

        self.self_attention = MaxState(hidden_size, num_heads)

        self.ffn = FeedForward(hidden_size)
        self.layer_norm = torch.nn.LayerNorm(hidden_size)

        self.alpha = torch.nn.Parameter(torch.tensor(0.5))

    def forward(self, x, state=None, ):
        x1, state = self.self_attention(x, state)
        x = self.layer_norm(self.alpha * self.ffn(x1) + (1 - self.alpha) * x)

        return x, state


class SamOut(torch.nn.Module):
    def __init__(self, voc_size, hidden_size, num_heads, num_layers):
        super(SamOut, self).__init__()
        self.em = torch.nn.Embedding(voc_size, hidden_size, padding_idx=3)

        self.decoder_layers = torch.nn.ModuleList([DecoderLayer(hidden_size, num_heads) for _ in range(num_layers)])
        self.head = FeedForward(hidden_size)

    def state_forward(self, state, x):
        if state is None:
            state = [None] * len(self.decoder_layers)
        i = 0
        for ii, decoder_layer in enumerate(self.decoder_layers):
            x1, state[i] = decoder_layer(x, state[i])
            x = x1 + x
            i += 1
        return x, state

    def forward(self, x, state=None):
        x = self.em(x)

        x, state = self.state_forward(state, x)

        em = self.head(self.em.weight) / x.shape[-1]

        return x @ em.permute([1, 0]), state


if __name__ == '__main__':

    net = SamOut(235, 256, 16, 4)
    net(torch.randint(0, 200, [2, 8 * 13]))
   

这段代码定义了一个基于 PyTorch 的神经网络模型,它包括几个自定义的层和模块。让我们逐步解析每个部分:

MaxState 类

MaxState 是一个自定义的 PyTorch 模块,旨在实现某种形式的多头机制(multi-head mechanism)。这里假设它试图模仿 Transformer 架构中的多头注意力机制(Multi-Head Attention),但实际实现上有所不同。具体来说:

  • __init__ 方法初始化了三个线性变换层 (head0, head1, head2) 和一些必要的属性。
  • forward 方法实现了前向传播逻辑,其中使用了 torch.cummax 函数来计算累积最大值,并将结果与其他输出相加。最终输出还涉及到了 out2 的乘法和 out1 的加法。

FeedForward 类

FeedForward 实现了一个简单的前馈神经网络(feed-forward network),通常用于 Transformer 模型中作为残差连接的一部分。这个类包含两个线性层和一个门控机制,后者通过 ReLU 激活函数控制信息流。

DecoderLayer 类

DecoderLayer 结合了 MaxStateFeedForward,并添加了层归一化(layer normalization)以及一个可学习的参数 alpha 用来混合原始输入与经过前馈网络处理后的输出。

SamOut 类

这是整个模型的核心部分,它定义了一个带有嵌入层、多个解码器层和一个最终的前馈层的序列到序列模型。SamOut 包含以下功能:

  • Embedding Layer (em): 将词汇索引映射为高维稠密向量。
  • Decoder Layers (decoder_layers) : 使用 DecoderLayer 组成的列表,模拟了多层解码过程。
  • Final Feed Forward Layer (head): 最后一层用于生成输出预测。

此外,SamOut 还定义了两个前向传播方法:

  • state_forward: 处理状态传递,允许模型在时间步之间共享内部状态。
  • forward: 完整的前向传播路径,从输入嵌入开始直到产生最终输出。

主程序

在主程序部分,创建了一个 SamOut 实例,并对其进行了测试,传入随机整数张量作为输入。这表明该模型可能是一个文本生成或翻译任务的一部分,其中 voc_size=235 表示词汇表大小,hidden_size=256 是隐藏层维度,num_heads=16 是多头机制中的头数,而 num_layers=4 则指定了堆叠的解码层数量。

需要注意的是,MaxState 中的逻辑看起来有些不寻常,特别是 torch.cummax 的用法和后续的操作。通常情况下,Transformer 的注意力机制会使用 softmax 来计算权重,而不是 cummax。因此,这里的实现可能是为了特定目的设计的变体,或者是代码中的错误。

总的来说,这段代码展示了一个结合了多头机制和前馈网络的深度学习模型,但是其具体应用和某些细节(如 MaxState 的实现)需要进一步澄清或验证。

相关推荐
杨荧38 分钟前
基于Python的反诈知识科普平台 Python+Django+Vue.js
大数据·前端·vue.js·python·数据分析
钮钴禄·爱因斯晨39 分钟前
AIGC浪潮下,风靡全球的Mcp到底是什么?一文讲懂,技术小白都知道!!
开发语言·人工智能·深度学习·神经网络·生成对抗网络·aigc
大模型真好玩40 分钟前
深入浅出LangChain AI Agent智能体开发教程(九)—LangChain从0到1搭建知识库
人工智能·python·mcp
MZ_ZXD0016 小时前
springboot汽车租赁服务管理系统-计算机毕业设计源码58196
java·c++·spring boot·python·django·flask·php
A 计算机毕业设计-小途6 小时前
大四零基础用Vue+ElementUI一周做完化妆品推荐系统?
java·大数据·hadoop·python·spark·毕业设计·毕设
lxmyzzs9 小时前
基于深度学习CenterPoint的3D目标检测部署实战
人工智能·深度学习·目标检测·自动驾驶·ros·激光雷达·3d目标检测
念念010710 小时前
数学建模竞赛中评价类相关模型
python·数学建模·因子分析·topsis
云天徽上11 小时前
【数据可视化-94】2025 亚洲杯总决赛数据可视化分析:澳大利亚队 vs 中国队
python·信息可视化·数据挖掘·数据分析·数据可视化·pyecharts
☺����11 小时前
实现自己的AI视频监控系统-第一章-视频拉流与解码2
开发语言·人工智能·python·音视频
王者鳜錸11 小时前
PYTHON让繁琐的工作自动化-函数
开发语言·python·自动化