Replace layer with similar layer

Say I need to modify a model so that it takes a different number of input channels. I need to replace the first conv2d with an equivalent one, except with different in_channels. How do I do this and ensure I haven’t accidentally changed other parameters?

This issue is more general than just the input dimension question. For example, it also applies to changing the number of output nodes of a final fully connected layer.

Several threads address specific instances:

However, it’s hard to tell exactly how to re-create something that is exactly the same except for one intentional change. For instance, if I want to change the number of input channels of a Conv2d but keep everything else the same I end up with code like this:

#c is the existing Conv2d
new_c = torch.nn.Conv2d(
    in_channels=new_numer_of_channels,
    bias=c.bias,
    dilation=c.dilation,
    groups=c.groups,
    kernel_size=c.kernel_size,
    out_channels=c.out_channels,
    padding=c.padding,
    padding_mode=c.padding_mode,
    stride=c.stride,
    transposed=c.transposed,
    output_padding=c.output_padding,
)

… and I worry that I may have missed something, or the API will change to allow a different parameter.

Since we cannot simply modify the original like c.in_channels = 4, I don’t know a better way to do this.

Is there a simpler and less fragile way to achieve this common objective of replacing one element with a nearly-identical one?