How to manipulate the weight of a conv2d layer?

BarCodeReader: <p>I want to create my own conv2d layer, say myConv2d, the only change is the weight matrix, all other things will be exactily the same as pytorch defines.</p>
<p>I noticed that Conv2d inherits _ConvNd, as stated in the document, so i just write a similar thing like below:</p>
<pre><code class="lang-auto">#import all other needed libraries
from torch.nn.modules.conv import _ConvNd

class myConv2d(_ConvNd):
    
    def __init__(...):
        ...

    def conv2d_forward(self, input, weight):
        #here i want to modify the behaviour of weight, say, weight*10
        weight = weight * 10
        ...
        return F.conv2d(input, weight, self.bias, self.stride,
                        self.padding, self.dilation, self.groups)

    def forward(self, input):
        return self.conv2d_forward(input, self.weight)
</code></pre>
<p>I saved above code in a file and import it in my network file:</p>
<pre><code class="lang-auto">from .myConv2d import myConv2d

class Net(nn.Module):

    def __init__(...):
        ...
        super(Net, self).__init__()
        
        self.conv1 = nn.Conv2d(...) #normal conv
        self.conv2 = myConv2d(...) #my conv
</code></pre>
<p>when i run the network, error pops:</p>
<blockquote>
<p>RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same</p>
</blockquote>
<p>means the weight in myConv2d is not passed to cuda…and actually I think this is because myConv2d not even belongs to my network graph(because it is not passed to cuda but the other part of the network are on the cuda)</p>
<p>So if I want to customize a conv layer’s behavior and make it part of the graph, what should I do or what did I missed?</p>
<p>Thanks for your help.</p>
<p><strong>Update</strong>: by manually set the weight to cuda I manage to run the code, but I still need to confirm with someone that this approach is valid and working.</p>
<pre><code class="lang-auto">def conv2d_forward(self, input, weight):
    device = weight.device
    new_weight = weight * 10
    new_weight = new_weight.to(device)
    ...
    return F.conv2d(input, new_weight, self.bias, self.stride,
                        self.padding, self.dilation, self.groups)
</code></pre>
<p>I also print out the weight and it is updating</p>

G.M: <ol>
<li>I think u don’t have to manually set <code>weight</code> to cuda if the input of the argument <code>weight</code>  is something already in cuda.</li>
<li>U don’t have to inherit <code>_ConvNd</code>, u can just inherit <code>nn.Module</code> or just write it as an ordinary Python function.</li>
<li>I think ur code should work, and I’m not from  Pytorch team <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> .</li>
</ol>

BarCodeReader: <p>Thanks for your reply.<br>
I am a bit confused that how you can interact with the weight by inherit from nn.module?<br>
can you share some simple code as hint?</p>
<p>thanks</p>

G.M: <pre><code class="lang-auto">class MyConv(nn.Conv1d):
    def __init__(self, *args):
        super().__init__(*args)
        self.weight.zeros_()
        self.bias.normal_(0, 1)
        #both "weight" and "bias" are Parameters from "nn.Conv1d"
        #and they all requires_grad
</code></pre>

BarCodeReader: <p>thanks, I will give a try on this and test it. Thank you!</p>