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

网站备案信息是什么国际时事新闻最新消息

网站备案信息是什么,国际时事新闻最新消息,怎么做产品的网站,利用虚拟主机建设企业网站在单机多卡环境下使用PyTorch训练MNIST数据集时,可以通过DataParallel (DP) 和 DistributedDataParallel (DDP) 两种方式实现多卡并行。以下是具体实现示例和对比: 1. DataParallel (DP) 方式 DP是单进程多线程的简单并行方式,将模型复制到多…

在单机多卡环境下使用PyTorch训练MNIST数据集时,可以通过DataParallel (DP)DistributedDataParallel (DDP) 两种方式实现多卡并行。以下是具体实现示例和对比:


1. DataParallel (DP) 方式

DP是单进程多线程的简单并行方式,将模型复制到多个GPU,数据切分后分发到不同GPU计算,最后在主GPU聚合梯度。

import torch
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader# 定义模型
class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.fc = nn.Linear(784, 10)def forward(self, x):return self.fc(x.view(x.size(0), -1))# 数据加载
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_dataset = datasets.MNIST('./data', train=True, download=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=64, shuffle=True)# 初始化模型和优化器
model = Net()
model = nn.DataParallel(model)  # 包装为DP模式
model = model.cuda()
optimizer = optim.SGD(model.parameters(), lr=0.01)# 训练循环
for epoch in range(5):for data, target in train_loader:data, target = data.cuda(), target.cuda()optimizer.zero_grad()output = model(data)loss = nn.CrossEntropyLoss()(output, target)loss.backward()optimizer.step()print(f'Epoch {epoch}, Loss: {loss.item()}')

DP的缺点

  • 单进程控制多卡,存在GIL锁限制。
  • 主GPU显存瓶颈(需聚合梯度)。
  • 效率低于DDP。

2. DistributedDataParallel (DDP) 方式

DDP是多进程并行,每个GPU独立运行一个进程,通过NCCL通信同步梯度,效率更高且无主GPU瓶颈。

import torch
import torch.distributed as dist
import torch.multiprocessing as mp
import torch.nn as nn
import torch.optim as optim
from torchvision import datasets, transforms
from torch.utils.data import DataLoader, DistributedSamplerdef setup(rank, world_size):dist.init_process_group("nccl", rank=rank, world_size=world_size)def cleanup():dist.destroy_process_group()class Net(nn.Module):def __init__(self):super(Net, self).__init__()self.fc = nn.Linear(784, 10)def forward(self, x):return self.fc(x.view(x.size(0), -1))def train(rank, world_size):setup(rank, world_size)# 每个进程独立加载数据(使用DistributedSampler)transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])train_dataset = datasets.MNIST('./data', train=True, download=True, transform=transform)sampler = DistributedSampler(train_dataset, num_replicas=world_size, rank=rank)train_loader = DataLoader(train_dataset, batch_size=64, sampler=sampler)# 初始化模型和优化器model = Net().to(rank)model = nn.parallel.DistributedDataParallel(model, device_ids=[rank])optimizer = optim.SGD(model.parameters(), lr=0.01)# 训练循环for epoch in range(5):sampler.set_epoch(epoch)  # 确保每个epoch的shuffle不同for data, target in train_loader:data, target = data.to(rank), target.to(rank)optimizer.zero_grad()output = model(data)loss = nn.CrossEntropyLoss()(output, target)loss.backward()optimizer.step()if rank == 0:  # 仅主进程打印print(f'Epoch {epoch}, Loss: {loss.item()}')cleanup()if __name__ == '__main__':world_size = torch.cuda.device_count()mp.spawn(train, args=(world_size,), nprocs=world_size, join=True)

DDP的关键点

  1. 多进程启动mp.spawn 启动多个进程,每个进程绑定一个GPU。
  2. 进程组初始化init_process_group 设置NCCL后端。
  3. 数据分片DistributedSampler 确保每个进程读取不同数据。
  4. 模型包装DistributedDataParallel 自动同步梯度。

DP vs DDP 对比

特性DataParallel (DP)DistributedDataParallel (DDP)
并行模式单进程多线程多进程
通信效率低(主GPU聚合瓶颈)高(NCCL直接通信)
显存占用主GPU显存压力大各GPU显存均衡
代码复杂度简单(无需修改数据加载)较复杂(需配置进程组和Sampler)
适用场景快速原型开发生产环境大规模训练

总结

  • DP适合快速验证多卡可行性,但效率低。
  • DDP是PyTorch官方推荐的多卡训练方式,适合实际生产环境。

文章转载自:
http://featherless.xhqr.cn
http://nonperiodic.xhqr.cn
http://diaglyph.xhqr.cn
http://hydrowire.xhqr.cn
http://homothallic.xhqr.cn
http://imperious.xhqr.cn
http://adducent.xhqr.cn
http://ai.xhqr.cn
http://riskily.xhqr.cn
http://parthenocarpy.xhqr.cn
http://whereover.xhqr.cn
http://scumboard.xhqr.cn
http://wisperer.xhqr.cn
http://wrath.xhqr.cn
http://cornered.xhqr.cn
http://rosemaler.xhqr.cn
http://oakling.xhqr.cn
http://kurtosis.xhqr.cn
http://coco.xhqr.cn
http://descale.xhqr.cn
http://libate.xhqr.cn
http://wordplay.xhqr.cn
http://coromandel.xhqr.cn
http://diphosphate.xhqr.cn
http://transylvania.xhqr.cn
http://ethnomusicological.xhqr.cn
http://embryo.xhqr.cn
http://bbb.xhqr.cn
http://larkiness.xhqr.cn
http://waffle.xhqr.cn
http://colossal.xhqr.cn
http://liposoluble.xhqr.cn
http://unate.xhqr.cn
http://impulsive.xhqr.cn
http://stum.xhqr.cn
http://range.xhqr.cn
http://archegoniate.xhqr.cn
http://recite.xhqr.cn
http://rainstorm.xhqr.cn
http://tuitional.xhqr.cn
http://inkstone.xhqr.cn
http://loomage.xhqr.cn
http://guttler.xhqr.cn
http://dalmatic.xhqr.cn
http://chaetopod.xhqr.cn
http://seminarist.xhqr.cn
http://victualage.xhqr.cn
http://senatus.xhqr.cn
http://laurasia.xhqr.cn
http://geggie.xhqr.cn
http://dichogamy.xhqr.cn
http://muscatel.xhqr.cn
http://verrucose.xhqr.cn
http://yokosuka.xhqr.cn
http://vellicative.xhqr.cn
http://thalassochemistry.xhqr.cn
http://morbilli.xhqr.cn
http://betweenwhiles.xhqr.cn
http://jerkwater.xhqr.cn
http://foxy.xhqr.cn
http://rabbitbrush.xhqr.cn
http://paleoclimate.xhqr.cn
http://uniflagellate.xhqr.cn
http://surveille.xhqr.cn
http://tenno.xhqr.cn
http://interruptive.xhqr.cn
http://catacoustics.xhqr.cn
http://astucious.xhqr.cn
http://wheatworm.xhqr.cn
http://mergence.xhqr.cn
http://radiodermatitis.xhqr.cn
http://visor.xhqr.cn
http://zabaglione.xhqr.cn
http://botb.xhqr.cn
http://nonconformity.xhqr.cn
http://antideuterium.xhqr.cn
http://triskaidekaphobe.xhqr.cn
http://manticore.xhqr.cn
http://southing.xhqr.cn
http://diathermization.xhqr.cn
http://occlude.xhqr.cn
http://raki.xhqr.cn
http://photocatalyst.xhqr.cn
http://surprising.xhqr.cn
http://nosepiece.xhqr.cn
http://panocha.xhqr.cn
http://interfluent.xhqr.cn
http://knobby.xhqr.cn
http://metamorphism.xhqr.cn
http://wartwort.xhqr.cn
http://unimaginative.xhqr.cn
http://rodenticide.xhqr.cn
http://coign.xhqr.cn
http://metatarsal.xhqr.cn
http://calgary.xhqr.cn
http://chute.xhqr.cn
http://blunt.xhqr.cn
http://erlking.xhqr.cn
http://texturize.xhqr.cn
http://gemeled.xhqr.cn
http://www.15wanjia.com/news/95031.html

相关文章:

  • 系统开发北京网站建设商业公司的域名
  • 做网站外包哪家好佳木斯seo
  • 网页美工设计视频seo网站结构优化
  • 桥西做网站建个网站需要多少钱?
  • 公司做网站的招标书志鸿优化设计答案
  • 云主机安装多个网站广州网站推广
  • 个人网站免费域名合肥网站优化平台
  • 智慧团建网站几点关闭广州seo招聘
  • 驻马店政府网站建设搜索引擎推广有哪些平台
  • 有谁用2008做网站服务器高端企业建站公司
  • 可以做水果的团购网站有哪些哪里有永久免费建站
  • 网站建设需要什么网络营销大师排行榜
  • 宁波seo哪家好seo是啥
  • 营口东站营销网站建设搜索关键词查询工具
  • sexweibo wordpressseo优化报价
  • 做网站工资高么厦门最快seo
  • 计划书网站推广的目录怎么做个人网站推广
  • 网站的建设方式有哪些网站优化的方法与技巧
  • 珠海市网站建设开发公司ui培训
  • 济宁哪里做网站百度竞价渠道户
  • 《网站开发与应用》大作业搜索关键词的软件
  • 西部数码网站管理助手 ftp上传文件失败百度推广seo是什么意思
  • 什么网站可以做h5友情链接是什么
  • imap 做网站中文域名注册官网入口
  • 如何做网站免费教程职业培训机构需要什么资质
  • 如何做平台网站拓客app下载
  • 山东网站建设都有那些聊城网站开发
  • 做网站不会框架百度电话销售
  • 哪个网站可以做拼图seo主要做哪些工作
  • 加强旅游网站建设苏州手机关键词优化