本文簡要介紹python語言中 torch.autograd.inference_mode
的用法。
用法:
class torch.autograd.inference_mode(mode=True)
mode(bool) -標記是否啟用或禁用推理模式
Context-manager 啟用或禁用推理模式
InferenceMode 是一個新的上下文管理器,類似於
no_grad
,當您確定您的操作不會與 autograd 交互時使用(例如,模型訓練)。在此模式下運行的代碼通過禁用視圖跟蹤和版本計數器碰撞來獲得更好的性能。這個上下文管理器是線程本地的;它不會影響其他線程中的計算。
也起到裝飾器的作用。 (確保用括號實例化。)
注意
推理模式是可以在本地啟用或禁用梯度的幾種機製之一,請參閱本地禁用梯度計算以獲取有關它們如何比較的更多信息。
>>> import torch >>> x = torch.ones(1, 2, 3, requires_grad=True) >>> with torch.inference_mode(): ... y = x * x >>> y.requires_grad False >>> y._version Traceback (most recent call last): File "<stdin>", line 1, in <module> RuntimeError: Inference tensors do not track version counter. >>> @torch.inference_mode() ... def func(x): ... return x * x >>> out = func(x) >>> out.requires_grad False
例子:
參數:
相關用法
- Python PyTorch index_select用法及代碼示例
- Python PyTorch invoke_on_rank_and_broadcast_result用法及代碼示例
- Python PyTorch inv_ex用法及代碼示例
- Python PyTorch inv用法及代碼示例
- Python PyTorch inner用法及代碼示例
- Python PyTorch ignore用法及代碼示例
- Python PyTorch ihfft用法及代碼示例
- Python PyTorch identity用法及代碼示例
- Python PyTorch import_huggingface_model用法及代碼示例
- Python PyTorch is_tensor_like用法及代碼示例
- Python PyTorch i0用法及代碼示例
- Python PyTorch irfft用法及代碼示例
- Python PyTorch is_nonzero用法及代碼示例
- Python PyTorch isneginf用法及代碼示例
- Python PyTorch ifft2用法及代碼示例
- Python PyTorch ifftn用法及代碼示例
- Python PyTorch i1e用法及代碼示例
- Python PyTorch is_scripting用法及代碼示例
- Python PyTorch isclose用法及代碼示例
- Python PyTorch ifftshift用法及代碼示例
- Python PyTorch isnan用法及代碼示例
- Python PyTorch imag用法及代碼示例
- Python PyTorch is_tensor_method_or_property用法及代碼示例
- Python PyTorch isreal用法及代碼示例
- Python PyTorch import_fairseq_model用法及代碼示例
注:本文由純淨天空篩選整理自pytorch.org大神的英文原創作品 torch.autograd.inference_mode。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。