Replace layer with similar layer

sammlapp: <p>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?</p>
<p>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.</p>
<p>Several threads address specific instances:</p><aside class="quote quote-modified" data-post="1" data-topic="163339">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/xiang-zhe/48/44315_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/modified-a-input-channel-using-replacement-with-new-conv2d-working-but-not-with-using-model-layer-in-channel-12/163339">Modified a input_channel using replacement with new Conv2d working but not with using model.layer.in_channel=12</a> 
  </div>
  <blockquote>
    I have a model network and its parameters, i modified the parameters, then go to change a channel of a conv2d layer, at first, i used model.layer.in_channels = 12, and then check with model.modules, it showed that input_channel(is 12 as assigned) of the conv2d was modified, but when using model.load_state_dict(parameters), it showed errors with parameters’ shape is (16,12,3,3) but model’s shape is (16,3,3,3); 
but i using model.layer = conv2d() to replace original layer works fine. 
why model.la…
  </blockquote>
</aside>

<aside class="quote quote-modified" data-post="7" data-topic="43586">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/brando_miranda/48/14355_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/how-to-replace-a-layer-with-own-custom-variant/43586/7">How to replace a layer with own custom variant?</a> 
  </div>
  <blockquote>
    I think this will work for you, just change it to your custom layer. Let us know if did work: 
def replace_bn(module, name):
    '''
    Recursively put desired batch norm in nn.module module.

    set module = net to start code.
    '''
    # go through all attributes of module nn.module (e.g. network or layer) and put batch norms if present
    for attr_str in dir(module):
        target_attr = getattr(m, attr_str)
        if type(target_attr) == torch.nn.BatchNorm2d:
            print('replac…
  </blockquote>
</aside>

<aside class="quote" data-post="3" data-topic="17736">
  <div class="title">
    <div class="quote-controls"></div>
    <img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/i/bc8723/48.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/replacing-convs-modules-with-custom-convs-then-notimplementederror/17736/3">Replacing convs modules with custom convs, then NotImplementedError</a> <a class="badge-category__wrapper " href="/c/autograd/7"><span data-category-id="7" data-drop-close="true" class="badge-category " title="A category of posts relating to the autograd engine itself."><span class="badge-category__name">autograd</span></span></a>
  </div>
  <blockquote>
    For those who seek a solution and not snobbish mentoring: 
def replace_bn(m, name):
    for attr_str in dir(m):
        target_attr = getattr(m, attr_str)
        if type(target_attr) == torch.nn.BatchNorm2d:
            print('replaced: ', name, attr_str)
            setattr(m, attr_str, SynchronizedBatchNorm2d(target_attr.num_features, target_attr.eps, target_attr.momentum, target_attr.affine))
    for n, ch in m.named_children():
        replace_bn(ch, n)
        
replace_bn(net, "net")
  </blockquote>
</aside>

<p>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:</p>
<pre><code class="lang-auto">#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,
)
</code></pre>
<p>… and I worry that I may have missed something, or the API will change to allow a different parameter.</p>
<p>Since we cannot simply modify the original like c.in_channels = 4, I don’t know a better way to do this.</p>
<p>Is there a simpler and less fragile way to achieve this common objective of replacing one element with a nearly-identical one?</p>