# How to set nn.conv2d weights

**URL:** <https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407>\
**Category:** Uncategorized\
**Created:** [January 22, 2020, 7:59pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407 "2020-01-22T19:59:36Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![ramin272](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@ramin272](https://discuss.pytorch.org/u/ramin272)\
**Post date:** [January 22, 2020, 7:59pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/1 "2020-01-22T19:59:36Z")

</div>

For some predefined operation i need to fix the parameters of a convolution layer.  
when I have this layer

```auto
self.conv1 = nn.Conv2d(1, 5, kernel_size=1, stride=1, padding=0, bias=False)

```

the code works fine, however when I set the weights of constitutional filter as

```auto
self.conv1 = nn.Conv2d(1, 5, kernel_size=1, stride=1, padding=0, bias=False)
self.conv1.weight = torch.nn.Parameter(torch.ones((1, 1, 5)))

```

I received following error

> RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]

Any idea how can i solve this problem?

---

<div class="post-metadata">

**Author:** ![LeviViana](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/leviviana/32/9548_2.png) [@LeviViana](https://discuss.pytorch.org/u/LeviViana)\
**Post date:** [January 22, 2020, 10:20pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/2 "2020-01-22T22:20:28Z")

</div>

The dimensions are not correct: you are assigning a `[1, 1, 5]` tensor to the weights, whereas `self.conv1.weight.size()` is `torch.Size([5, 1, 1, 1])`.

Try:

```python
self.conv1.weight = torch.nn.Parameter(torch.ones_like(self.conv1.weight))

```

and it will work !

---

<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:** [January 23, 2020, 5:24am UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/3 "2020-01-23T05:24:23Z")

</div>

A great way to know what the shapes of the weight should be is to print the shape of the weights.

---

<div class="post-metadata">

**Author:** ![fn246.c](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/839c29/32.png) [@fn246.c](https://discuss.pytorch.org/u/fn246.c)\
**Post date:** [May 13, 2021, 2:24pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/4 "2021-05-13T14:24:35Z")

</div>

I want to **set and fix** weights of nn.conv1d, so this layer in network has fixed parameters and is **NOT learnable**.  
Is self.conv1.weight = torch.nn.Parameter(torch.ones\_like(self.conv1.weight))  
makes the weights fixed?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [May 14, 2021, 7:04am UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/5 "2021-05-14T07:04:23Z")

</div>

No, `nn.Parameter`s require gradients by default, so you would need to set its `requires_grad` attribute to `False`.

---

<div class="post-metadata">

**Author:** ![fn246.c](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/f/839c29/32.png) [@fn246.c](https://discuss.pytorch.org/u/fn246.c)\
**Post date:** [July 23, 2021, 7:32pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/6 "2021-07-23T19:32:58Z")

</div>

I want to set **initial weights** and they start learning from that initial weights, so is it true to use bellow script:  
self.conv1.weight = torch.nn.Parameter(torch.ones\_like(self.conv1.weight),requires\_grad= **True** )  
?

---

<div class="post-metadata">

**Author:** ![afsaneh\_ebrahimi](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/afsaneh_ebrahimi/32/39738_2.png) [@afsaneh\_ebrahimi](https://discuss.pytorch.org/u/afsaneh_ebrahimi)\
**Post date:** [November 1, 2021, 6:18pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/7 "2021-11-01T18:18:15Z")

</div>

Is there any way to set requires\_grad=False just for one element of weight?

---

<div class="post-metadata">

**Author:** ![ptrblck](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/32/1823_2.png) [@ptrblck](https://discuss.pytorch.org/u/ptrblck)\
**Post date:** [November 1, 2021, 8:46pm UTC](https://discuss.pytorch.org/t/how-to-set-nn-conv2d-weights/67407/8 "2021-11-01T20:46:07Z")

</div>

No, that’s not possible as you can change the `requires_grad` attribute for an entire tensor only.  
An alternative approach would be to either set the gradients to zero for the desired elements after the `backward()` operation and before the `step()` call or to recreate the parameter from different tensors (which use different `requires_grad` attributes) via `torch.cat` or `torch.stack`. However, the latter approach might be cumbersome depending how complicated the tensor creation would be.
