Creating fixed weight nn.linear

Hi.
I would like to set up an nn.Linear layer in such a way that the data passing through the layer remains unchanged. I understand that I can achieve this by configuring the weight as an identity matrix. Is there a way to directly set the weight of the nn.Linear layer like this??

thanks always!!

Maybe you can use F.Linear instead of nn.Linear and specify the weight?

1 Like

Sure, you can use Identity — PyTorch 2.1 documentation

import torch.nn as nn
layer = nn.Identity()
1 Like