机器学习使用GPU

使用GPU

使用下面的命令来查看GPU的状态

shell 复制代码
!nvidia-smi
代码 含义
Memory-Usage 内存使用量/总量
GPU-Util (运行时)GPU使用量
CUDA Version CUDA的版本, 需要对应版本的框架

使用GPU运算

张量
python 复制代码
import torch

torch.device('cpu')
torch.device('cuda') # 使用GPU
torch.device('cuda:1')  # 访问第一个GPU

查看有多少GPU

python 复制代码
torch.cuda.device_count()

测试GPU环境

python 复制代码
def try_gpu(i=0):
	if torch.cuda.device_count() >= i + 1:
		return torch.device(f'cuda:{i}')
	return torch.device('cpu')

def try_all_gpu():
	devices = [torch.device(f'cuda:{i}') for i in range(torch.cuda.device_count())]
	return devices if devices else [torch.device('cpu')]

查询张量所在的设备

python 复制代码
x = torch.tensor([1, 2, 3])
x.device # device(type='cpu')

存储在gpu

python 复制代码
X = torch.ones(2, 3, device=try_gpu())
X # tensor(..., device='cuda:0')

第二个GPU创建张量

python 复制代码
X = torch.ones(2, 3, device=try_gpu(1))
X # tensor(..., device='cuda:1')

计算X, Y, 需要确定在同一个GPU执行计算操作

python 复制代码
Z = X.cuda(1)
Z # tensor(..., device='cuda:1')

Z.cuda(1) is Z  # True, 如果已经在对应的GPU, 不会做任何改变和开销

如果将不同的层分散放在CPU和GPU, 计算时会造成很大开销和性能问题, 并且不易排查, 所以最开始初始化就建议使用一个环境, 不要来回COPY切换

神经网络

神经网络在GPU

python 复制代码
net = nn.Sequential(nn.Linear(3, 1))
net = net.to(device=try_gpu())

net(X)

确认模型参数存储在同一个GPU

python 复制代码
net[0].weight.data.device # device(type='cuda',index=0)
相关推荐
dagouaofei2 分钟前
AI 生成 2026 年工作计划 PPT,内容质量差异在哪里
人工智能·python·powerpoint
ai_top_trends3 分钟前
2026 年工作计划汇报 PPT:AI 生成方案实测对比
人工智能·python·powerpoint
JicasdC123asd5 分钟前
基于YOLO11-seg的MultiSEAMHead驾驶员疲劳检测系统_计算机视觉实时监测_眼睛嘴巴状态识别
人工智能·计算机视觉
极新6 分钟前
飞书产品营销&增长经理王屹煊:飞书多维表格驱动业务新增长|2025极新AIGC峰会演讲实录
人工智能
寻星探路7 分钟前
【Python 全栈测开之路】Python 基础语法精讲(三):函数、容器类型与文件处理
java·开发语言·c++·人工智能·python·ai·c#
且去填词7 分钟前
构建基于 DeepEval 的 LLM 自动化评估流水线
运维·人工智能·python·自动化·llm·deepseek·deepeval
dagouaofei10 分钟前
不同 AI 生成 2026 年工作计划 PPT 的使用门槛对比
人工智能·python·powerpoint
IRevers12 分钟前
【目标检测】深度学习目标检测损失函数总结
人工智能·pytorch·深度学习·目标检测·计算机视觉
RockHopper202515 分钟前
为什么具身智能系统需要能“自我闭环”的认知机制
人工智能·具身智能·具身认知
itwangyang52016 分钟前
Windows + Conda + OpenMM GPU(CUDA)完整安装教程-50显卡系列
人工智能·windows·python·conda