Jyothi:

kn = torch.Tensor([[1 ,0, -1],[2, 0 ,-2], [1, 0 ,-1]]).unsqueeze(0).unsqueeze(0)


kn is initialized as a 2D tensor containing the specified values in the shape [3, 3] and gets two additional dimensions via the unsqueeze operations to have a shape of [1, 1, 3, 3].



 Jyothi:

self.conv1.weight = nn.Parameter(kn.expand(6, 3, -1, -1)) and what exactly this line is doing?


kn.expand expands the tensor in to have the shape [6, 3, 3, 3]. The docs explain its usage and which values can be used (e.g. in your use case the -1 would just keep the shape of this dimension).