nn.Identity() 是个函数,不是矩阵!
恒等函数 f ( x ) = x f(x)=x f(x)=x
罢了

说明

nn.Identity will just return its input, but doesn’t show that it’s a view.
If you thus manipulate the input inplace, the output of nn.Identity will also be changed:

a = torch.arange(4.)
m = nn.Identity()
input_identity = m(a)

print(a)
> tensor([0., 1., 2., 3.])

print(input_identity)
> tensor([0., 1., 2., 3.])

manipulate inplace

a[0] = 2.
print(a)
> tensor([2., 1., 2., 3.])

print(input_identity)
> tensor([2., 1., 2., 3.])

As mentioned before, nn.Identity will just return the input without any clone usage or manipulation of the input. The input and output would thus be the same.

Yes, this “pass-through” layer can easily be written manually, which was also the reason feature requests were declined in the past.
However, since a lot of users were rewriting the same layer to e.g. replace specific modules inside a larger model, this layer was introduced.

You might not want to use this layer as it’s not “doing anything” besides just returning the input.
However, there are use cases where users needed exactly this (e.g. to replace another layer) and were manually creating custom modules to do so and asked for the nn.Identity layer in the PyTorch nn backend. Since more and more users were depending on it, it was created.
However, as already said, this layer might not be interesting for you.

skip connection

Skip connections would usually a add activations instead of concatenating them as seen in the resnet example 12.

An often used use case for nn.Identity would be to get the “features” of a pretrained model instead of the class logits.
最后一层本来是用作 分类的,用 softmax函数或者 fully connected 函数,但是用 nn.identtiy() 函数把最后一层替换掉,相当于得到分类之前的特征!
在迁移学习里有用。

Here is an example:

model = models.resnet18()
# replace last linar layer with nn.Identity
model.fc = nn.Identity()

# get features for input
x = torch.randn(1, 3, 224, 224)
out = model(x)
print(out.shape)
> torch.Size([1, 512])

来自


What is nn.Identity() used for?

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐