example for its use?
I am not sure I subscribe to the use-cases, but there is some discussion in the feature request for it.
Best regards
Thomas
CLASS torch.nn.
Identity
( *args , **kwargs )[SOURCE]
A placeholder identity operator that is argument-insensitive.
Parameters
- 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])
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
return
class Network(nn.Module):
def __init__(self, ...):
self.Input = nn.Identity()
...
for name, layer in self.named_children():
layer.register_forward_hook(foo)
def forward(self, x):
y = self.Input(x)
# do stuff with 'y'
...