实用指南:Day34 GPU训练及类的call方法

查看CPU性能:架构代际、核心数、线程数

提前安装一个库 

# pip install wmi -i https://pypi.tuna.tsinghua.edu.cn/simple
# 这是Windows专用的库,Linux和MacOS不支持,其他系统自行询问大模型
# 我想查看一下CPU的型号和核心数
import wmi
c = wmi.WMI()
processors = c.Win32_Processor()
for processor in processors:
print(f"CPU 型号: {processor.Name}")
print(f"核心数: {processor.NumberOfCores}")
print(f"线程数: {processor.NumberOfLogicalProcessors}")

GPU训练对比CPU训练

相较于cpu,使用GPU计算多了3个时间上的开销

1. 数据传输开销 (CPU 内存 <-> GPU 显存)

2. 核心启动开销 (GPU 核心启动时间)

3. 性能浪费:计算量和数据批次

类的call方法:为什么定义前向传播时可以直接写作self.fc1(x)

__call__ 方法是一个特殊的魔术方法(双下划线方法),它允许类的实例像函数一样被调用。

# 不带参数的call方法
class Counter:
def __init__(self):
self.count = 0
def __call__(self):
self.count += 1
return self.count
# 使用示例
counter = Counter()
print(counter())  # 输出: 1
print(counter())  # 输出: 2
print(counter.count)  # 输出: 2

@浙大疏锦行

posted @ 2025-08-17 20:55  yjbjingcha  阅读(7)  评论(0)    收藏  举报