在Ubuntu上使用PyTorch调试模型时,可以遵循以下步骤:
安装Python和PyTorch:
sudo apt update
sudo apt install python3 python3-pip
pip3 install torch torchvision torchaudio
安装调试工具:
pdb:Python的内置调试器。ipdb:基于IPython的增强版调试器。torch.autograd.set_detect_anomaly(True):启用PyTorch的自动异常检测。pdb在代码中插入pdb.set_trace()来设置断点。
import torch
import torch.nn as nn
class SimpleModel(nn.Module):
def __init__(self):
super(SimpleModel, self).__init__()
self.fc = nn.Linear(10, 5)
def forward(self, x):
x = self.fc(x)
pdb.set_trace() # 设置断点
return x
model = SimpleModel()
input_tensor = torch.randn(1, 10)
output = model(input_tensor)
ipdb同样插入ipdb.set_trace(),但会提供更好的交互体验。
import ipdb
# ... 同上 ...
def forward(self, x):
x = self.fc(x)
ipdb.set_trace() # 设置断点
return x
在终端中运行你的Python脚本:
python3 your_script.py
程序会在断点处暂停,你可以使用以下命令进行调试:
n:下一步s:进入函数c:继续执行l:显示当前代码位置p <variable>:打印变量值q:退出调试在关键步骤添加日志输出,帮助理解程序的执行流程和数据状态。
import logging
logging.basicConfig(level=logging.DEBUG)
# ... 同上 ...
def forward(self, x):
logging.debug(f"Input shape: {x.shape}")
x = self.fc(x)
logging.debug(f"Output shape: {x.shape}")
return x
使用TensorBoard或其他可视化工具来监控模型的训练过程和性能指标。
from torch.utils.tensorboard import SummaryWriter
writer = SummaryWriter('runs/simple_experiment')
# ... 在训练循环中使用writer记录数据 ...
编写单元测试来验证模型的各个部分是否按预期工作。
import unittest
class TestSimpleModel(unittest.TestCase):
def test_forward(self):
model = SimpleModel()
input_tensor = torch.randn(1, 10)
output = model(input_tensor)
self.assertEqual(output.shape, (1, 5))
if __name__ == '__main__':
unittest.main()
分析模型的性能瓶颈,优化计算图。
from torch.profiler import profile, record_function, ProfilerActivity
with profile(activities=[ProfilerActivity.CPU], record_shapes=True) as prof:
with record_function("model_inference"):
output = model(input_tensor)
print(prof.key_averages().table(sort_by="cpu_time_total", row_limit=10))
通过以上步骤,你应该能够在Ubuntu上有效地调试PyTorch模型。