手机网站大全农村小学校园网站建设方案
2026/1/8 21:47:15 网站建设 项目流程
手机网站大全,农村小学校园网站建设方案,微商货源类网站源码,做网站贵么浙大疏锦行 一、CPU 性能查看 系统级查看#xff08;Windows/Linux/Mac 通用#xff09; #xff08;1#xff09;Windows 系统 任务管理器#xff1a;按下 CtrlShiftEsc → 切换到「性能」选项卡 → 查看 CPU 使用率、核心数、频率、缓存等#xff1b;资源监视器浙大疏锦行一、CPU 性能查看系统级查看Windows/Linux/Mac 通用1Windows 系统任务管理器按下CtrlShiftEsc→ 切换到「性能」选项卡 → 查看 CPU 使用率、核心数、频率、缓存等资源监视器任务管理器「性能」→ 左下角「打开资源监视器」→ 可查看每个进程的 CPU 占用、线程数。2Linux 系统命令行# 实时查看CPU整体使用率、核心数、负载最常用 top # 按1显示每个核心按q退出 # 查看CPU硬件信息核心数、架构、频率 lscpu # 更直观的CPU监控需安装 apt install htop htop # 查看进程CPU占用指定进程名/ID pidstat -p [进程ID] 1 # 每秒刷新1次3Mac 系统「活动监视器」启动台搜索「活动监视器」→ 「CPU」标签页 → 查看使用率、进程占用终端命令top # 实时监控 sysctl -n hw.ncpu # 查看CPU核心数二、GPU 性能查看Python 代码中查看 GPU 性能import torch # 1. 检查GPU是否可用 gpu_available torch.cuda.is_available() print(fGPU是否可用{gpu_available}) # 2. 查看GPU数量、型号 if gpu_available: gpu_count torch.cuda.device_count() gpu_name torch.cuda.get_device_name(0) # 查看第0块GPU名称 print(fGPU数量{gpu_count}第0块GPU型号{gpu_name}) # 3. 查看当前GPU显存使用PyTorch视角 torch.cuda.empty_cache() # 清空缓存 allocated torch.cuda.memory_allocated(0) / 1024**3 # 已分配显存GB cached torch.cuda.memory_reserved(0) / 1024**3 # 缓存显存GB print(f已分配显存{allocated:.2f}GB缓存显存{cached:.2f}GB) # 4. 设置默认GPU torch.cuda.set_device(0)三、GPU 训练方法核心步骤设备配置 → 模型 / 数据迁移 → 训练逻辑 → 显存优化设备配置通过torch.cuda.is_available()判断 GPU 是否可用将模型 / 数据移到 GPU.to(device)数据 / 模型迁移张量 / 模型必须和设备匹配GPU 张量不能在 CPU 上计算反之亦然批量训练避免 GPU 显存溢出控制 batch_size混合精度可选加速训练减少显存占用。import torch import torch.nn as nn import torch.optim as optim from torch.utils.data import DataLoader, TensorDataset # 1. 设备配置核心 device torch.device(cuda:0 if torch.cuda.is_available() else cpu) print(f训练设备{device}) # 2. 定义模型并迁移到GPU class SimpleMLP(nn.Module): def __init__(self, input_dim10, hidden_dim64, output_dim2): super().__init__() self.mlp nn.Sequential( nn.Linear(input_dim, hidden_dim), nn.ReLU(), nn.Linear(hidden_dim, output_dim) ) def forward(self, x): return self.mlp(x) # 初始化模型并移到GPU model SimpleMLP(input_dim10, output_dim2).to(device) # 3. 准备数据并迁移到GPU批量 # 模拟数据集 batch_size 64 x torch.randn(1000, 10) # CPU张量 y torch.randint(0, 2, (1000,)) # CPU张量 dataset TensorDataset(x, y) dataloader DataLoader(dataset, batch_sizebatch_size, shuffleTrue) # 4. 定义优化器/损失函数 criterion nn.CrossEntropyLoss() optimizer optim.Adam(model.parameters(), lr1e-3) # 5. GPU训练逻辑 epochs 5 model.train() # 训练模式 for epoch in range(epochs): epoch_loss 0.0 for x_batch, y_batch in dataloader: # 关键将batch数据移到GPUDataLoader默认生成CPU张量 x_batch x_batch.to(device) y_batch y_batch.to(device) # 前向传播 outputs model(x_batch) loss criterion(outputs, y_batch) # 反向传播优化 optimizer.zero_grad() loss.backward() optimizer.step() epoch_loss loss.item() avg_loss epoch_loss / len(dataloader) print(fEpoch [{epoch1}/{epochs}], 平均损失{avg_loss:.4f}) # 6. GPU推理验证 model.eval() with torch.no_grad(): # 关闭梯度节省显存 test_x torch.randn(10, 10).to(device) # 测试数据移到GPU pred model(test_x) print(f\nGPU推理结果类别{pred.argmax(dim1).cpu().numpy()}) # 转回CPU便于打印四、类的call方法1. 语法规则class 类名: def __call__(self, *args, **kwargs): # 自定义逻辑可接收参数、返回结果 执行操作 return 结果当实例被调用实例()或实例(参数)时Python 会自动执行__call__方法self指向实例本身可访问实例的所有属性状态支持任意参数*args/**kwargs和返回值和普通函数完全一致。2. 本质可调用对象CallablePython 中「可调用对象」包括函数、方法、类、带__call__的实例。可通过callable()验证class Counter: def __call__(self): pass counter Counter() print(callable(counter)) # True实例可调用 print(callable(Counter)) # True类本身可调用调用后创建实例 print(callable(lambda x: x1)) # True函数可调用3.基础示例带状态的调用最典型的场景是「保存状态的函数」区别于普通函数的局部变量class Accumulator: 累加器每次调用传入一个数累加并返回总和 def __init__(self): self.total 0 # 实例属性保存累加状态 def __call__(self, num): self.total num return self.total # 使用示例 acc Accumulator() print(acc(10)) # 执行__call__total010 → 输出10 print(acc(20)) # total1020 → 输出30 print(acc(5)) # total305 → 输出35 print(acc.total)# 直接访问状态 → 输出35

需要专业的网站建设服务?

联系我们获取免费的网站建设咨询和方案报价,让我们帮助您实现业务目标

立即咨询