with torch.no_grad(): 一般要停止跟踪历史记录(和使用内存),可以将代码块使用 with torch.no_grad(): 包装起来。 在评估模型时,这是特别有用,因为模型在训练阶段具有 requires_grad = True 的可训练参数有利于调参,但在评估阶段我们不需要梯度。