快捷键

torch.jit.ignore

torch.jit.ignore(drop=False, **kwargs)[source][source]

此装饰器指示编译器忽略函数或方法,并将其保留为 Python 函数。这允许您在模型中保留尚未与 TorchScript 兼容的代码。如果从 TorchScript 调用,则忽略的函数将调用 Python 解释器。带有忽略函数的模型无法导出;请使用 @torch.jit.unused 代替。

示例(在方法上使用 @torch.jit.ignore ):

import torch
import torch.nn as nn


class MyModule(nn.Module):
    @torch.jit.ignore
    def debugger(self, x):
        import pdb

        pdb.set_trace()

    def forward(self, x):
        x += 10
        # The compiler would normally try to compile `debugger`,
        # but since it is `@ignore`d, it will be left as a call
        # to Python
        self.debugger(x)
        return x


m = torch.jit.script(MyModule())

# Error! The call `debugger` cannot be saved since it calls into Python
m.save("m.pt")

示例(在方法上使用 @torch.jit.ignore(drop=True) ):

import torch
import torch.nn as nn

class MyModule(nn.Module):
    @torch.jit.ignore(drop=True)
    def training_method(self, x):
        import pdb
        pdb.set_trace()

    def forward(self, x):
        if self.training:
            self.training_method(x)
        return x

m = torch.jit.script(MyModule())

# This is OK since `training_method` is not saved, the call is replaced
# with a `raise`.
m.save("m.pt")

© 版权所有 PyTorch 贡献者。

使用 Sphinx 构建,并使用 Read the Docs 提供的主题。

文档

PyTorch 的全面开发者文档

查看文档

教程

深入了解初学者和高级开发者的教程

查看教程

资源

查找开发资源并获得您的疑问解答

查看资源