pytorch torch.nn.Identity()


torch.nn.Identity()

今天看源碼時,遇到的這個恆等函數,就如同名字那樣
占位符,並沒有實際操作

源碼:

class Identity(Module):
    r"""A placeholder identity operator that is argument-insensitive.
    Args:
        args: any argument (unused)
        kwargs: any keyword argument (unused)
    Examples::
        >>> m = nn.Identity(54, unused_argument1=0.1, unused_argument2=False)
        >>> input = torch.randn(128, 20)
        >>> output = m(input)
        >>> print(output.size())
        torch.Size([128, 20])
    """
    def __init__(self, *args, **kwargs):
        super(Identity, self).__init__()
 
    def forward(self, input: Tensor) -> Tensor:
        return input

主要使用場景:
不區分參數的占位符標識運算符

  • if 某個操作 else Identity()
  • 在增減網絡過程中,可以使得整個網絡層數據不變,便於遷移權重數據


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM