当前位置: 首页 > news >正文

网站的建设与规划wordpress无辜跳出广告

网站的建设与规划,wordpress无辜跳出广告,百度网盘怎么用,像做网站平台取什么名字好参考: MOE原理解释及从零实现一个MOE(专家混合模型)_moe代码-CSDN博客 MoE环游记:1、从几何意义出发 - 科学空间|Scientific Spaces 深度学习之图像分类(二十八)-- Sparse-MLP(MoE)网络详解_sparse moe…

参考:

MOE原理解释及从零实现一个MOE(专家混合模型)_moe代码-CSDN博客

MoE环游记:1、从几何意义出发 - 科学空间|Scientific Spaces 

深度学习之图像分类(二十八)-- Sparse-MLP(MoE)网络详解_sparse moe-CSDN博客

深度学习之图像分类(二十九)-- Sparse-MLP网络详解_sparse mlp-CSDN博客 

 

代码如下:

import torch
import torch.nn as nn
import torch.nn.functional as F
from torchvision import datasets, transforms
from torch.utils.data import DataLoader# 超参数设置
num_experts = 4      # 专家数量
top_k = 2            # 激活专家数
# input_dim = 3072     # CIFAR-10图像展平后维度(32x32x3)
input_dim = 64 * 8 * 8
hidden_dim = 512     # 专家网络隐藏层维度
num_classes = 10     # 分类类别数# MoE层实现(文献[5][7])
class SparseMoE(nn.Module):def __init__(self):super().__init__()self.experts = nn.ModuleList([nn.Sequential(nn.Linear(input_dim, hidden_dim),nn.ReLU(),nn.Linear(hidden_dim, hidden_dim)) for _ in range(num_experts)])self.gate = nn.Sequential(nn.Linear(input_dim, num_experts),nn.Softmax(dim=1))# 负载均衡参数(文献[4][7])self.balance_loss_weight = 0.01self.register_buffer('expert_counts', torch.zeros(num_experts))def forward(self, x):# 门控计算gate_scores = self.gate(x)  # [B, num_experts]# Top-k选择(文献[5])topk_scores, topk_indices = torch.topk(gate_scores, top_k, dim=1)mask = F.one_hot(topk_indices, num_experts).float().sum(dim=1)# 专家输出聚合expert_outputs = torch.stack([expert(x) for expert in self.experts], dim=1)selected_experts = expert_outputs.gather(1, topk_indices.unsqueeze(-1).expand(-1, -1, hidden_dim))  # [B, 2, H]# print(f"专家输出维度: {expert_outputs.shape}")# print(f"选择索引维度: {topk_indices.shape}")# print(f"选择专家维度: {selected_experts.shape}")weighted_outputs = (selected_experts  * topk_scores.unsqueeze(-1)).sum(dim=1)# 更新专家使用统计self.expert_counts += mask.sum(dim=0)return weighted_outputsdef balance_loss(self):# 计算负载均衡损失(文献[4][7])expert_probs = self.expert_counts / self.expert_counts.sum()balance_loss = torch.std(expert_probs) * self.balance_loss_weightself.expert_counts.zero_()  # 重置计数器return balance_loss# 完整模型架构(文献[2][6])
class MoEImageClassifier(nn.Module):def __init__(self):super().__init__()self.feature_extractor = nn.Sequential(nn.Conv2d(3, 32, 3, padding=1),nn.ReLU(),nn.MaxPool2d(2),nn.Conv2d(32, 64, 3, padding=1),nn.ReLU(),nn.MaxPool2d(2))self.moe_layer = SparseMoE()self.classifier = nn.Linear(hidden_dim, num_classes)def forward(self, x):x = self.feature_extractor(x)x = x.view(x.size(0), -1)  # 展平特征x = self.moe_layer(x)return self.classifier(x)# 数据预处理(文献[2])
transform = transforms.Compose([transforms.ToTensor(),transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])train_set = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_set, batch_size=64, shuffle=True)# 训练流程
model = MoEImageClassifier()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)for epoch in range(10):for images, labels in train_loader:optimizer.zero_grad()outputs = model(images)main_loss = criterion(outputs, labels)balance_loss = model.moe_layer.balance_loss()total_loss = main_loss + balance_losstotal_loss.backward()optimizer.step()print(f'Epoch [{epoch+1}/10], Loss: {total_loss.item():.4f}')

http://www.dtcms.com/a/411548.html

相关文章:

  • 个人做论坛网站有哪些常用的网站推广
  • 【读书笔记】架构整洁之道 P6 实现细节
  • 古籍版面分析新SOTA:HisDoc-DETR如何助力AI赋能古籍数字化难题
  • 浙江省网站icp备案多久oa协同办公系统
  • 伊朗声称以色列核计划数据遭重大泄露
  • 自适应平台(Adaptive Platform)标准 ——Specification of Sensor Interfaces
  • LeetCode热题--200. 岛屿数量--中等
  • 营销型网站试运营调忧北京海淀房管局网站
  • 网站建设与制作与维护ppt网站百度排名怎么做快
  • SSM飞机售票管理系统63z52(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面
  • [论文阅读] 人工智能 + 软件工程 | 当传统调试遇上LLM:CodeHinter为新手程序员打造专属辅助工具
  • 亚马逊网站做外贸网站是可以做的吗
  • Kimi推出全新Agent模式OK Computer,基于K2模型的端到端任务执行,已开启灰度测试
  • 用vs2010做网站视频教程高端网站制作报价
  • react-native集成PDF预览组件react-native-pdf
  • Dify笔记 知识库
  • 模板建站服务器网页打不开的解决方法
  • 女生做网站前台设置自动删除的wordpress
  • 苏州市吴江太湖新城建设局网站微信手机网站设计6
  • 单片机开发中的队列数据结构详解,队列数据结构在单片机软件开发中的应用详解,C语言
  • 邯郸网站推广wordpress 页面生成
  • 搭建本地代理服务器
  • USB4接口防护,ESD管与TVS管怎么选?-ASIM阿赛姆
  • LazyLLM部署日志
  • 祝贺职业教育网站上线网站的前端和后台
  • 第三人称:角色攻击
  • 怎么理解GO中的context
  • 国内永久免费建站哈尔滨网站设计有哪些步骤
  • 运动控制教学——5分钟学会样条曲线算法!(三次样条曲线,B样条曲线)
  • HTTP 错误 403.14 - Forbidden Web 服务器被配置为不列出此目录的内容——错误代码:0x00000000