What is the use of nn.Identity?

example for its use?

1 Like

I am not sure I subscribe to the use-cases, but there is some discussion in the feature request for it.

Best regards


CLASS torch.nn. Identity ( *args , **kwargs )[SOURCE]

A placeholder identity operator that is argument-insensitive.


  • args – any argument (unused)
  • kwargs – any keyword argument (unused)


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])

I’ve used it to make my code more compact:

self.layer = nn.Sequential(…,
nn.Dropout(0.5) if dropout else nn.Identity(),


One way I’ve used it: suppose you register a hook to track something about the output of every layer in a network. But if you also want track this statistic for the input to the network, but not the input to any other layer, you have some inconvenient if statements to write.

Instead, just create a dummy layer at the start of the network (or wherever is useful):

def foo(module, input, ouput):
    # do something with 'output' only

class Network(nn.Module):

    def __init__(self, ...):
        self.Input = nn.Identity()

        for name, layer in self.named_children():

    def forward(self, x):
        y = self.Input(x)
        # do stuff with 'y'