# How to manipulate the weight of a conv2d layer?

**URL:** <https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584>\
**Category:** autograd\
**Created:** [March 1, 2020, 4:08am UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584 "2020-03-01T04:08:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![BarCodeReader](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barcodereader/32/18717_2.png) [@BarCodeReader](https://discuss.pytorch.org/u/BarCodeReader)\
**Post date:** [March 1, 2020, 4:08am UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584/1 "2020-03-01T04:08:50Z")

</div>

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.

I noticed that Conv2d inherits \_ConvNd, as stated in the document, so i just write a similar thing like below:

```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)

```

I saved above code in a file and import it in my network file:

```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

```

when i run the network, error pops:

> RuntimeError: Input type (torch.cuda.FloatTensor) and weight type (torch.FloatTensor) should be the same

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)

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?

Thanks for your help.

**Update** : 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.

```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)

```

I also print out the weight and it is updating

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [March 1, 2020, 11:08am UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584/2 "2020-03-01T11:08:58Z")

</div>

1. I think u don’t have to manually set `weight` to cuda if the input of the argument `weight` is something already in cuda.
2. U don’t have to inherit `_ConvNd`, u can just inherit `nn.Module` or just write it as an ordinary Python function.
3. I think ur code should work, and I’m not from Pytorch team 🙂 .

---

<div class="post-metadata">

**Author:** ![BarCodeReader](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barcodereader/32/18717_2.png) [@BarCodeReader](https://discuss.pytorch.org/u/BarCodeReader)\
**Post date:** [March 2, 2020, 3:01pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584/3 "2020-03-02T15:01:10Z")

</div>

Thanks for your reply.  
I am a bit confused that how you can interact with the weight by inherit from nn.module?  
can you share some simple code as hint?

thanks

---

<div class="post-metadata">

**Author:** ![G.M](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/g.m/32/19266_2.png) [@G.M](https://discuss.pytorch.org/u/G.M)\
**Post date:** [March 3, 2020, 1:46am UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584/4 "2020-03-03T01:46:38Z")

</div>

```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

```

---

<div class="post-metadata">

**Author:** ![BarCodeReader](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/barcodereader/32/18717_2.png) [@BarCodeReader](https://discuss.pytorch.org/u/BarCodeReader)\
**Post date:** [March 3, 2020, 1:45pm UTC](https://discuss.pytorch.org/t/how-to-manipulate-the-weight-of-a-conv2d-layer/71584/5 "2020-03-03T13:45:08Z")

</div>

thanks, I will give a try on this and test it. Thank you!
