# Manually feeding trainable parameters to the optimizer

**URL:** <https://discuss.pytorch.org/t/manually-feeding-trainable-parameters-to-the-optimizer/1724>\
**Category:** Uncategorized\
**Created:** [April 7, 2017, 8:05am UTC](https://discuss.pytorch.org/t/manually-feeding-trainable-parameters-to-the-optimizer/1724 "2017-04-07T08:05:54Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![woofie56](https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/w/958977/32.png) [@woofie56](https://discuss.pytorch.org/u/woofie56)\
**Post date:** [April 7, 2017, 8:05am UTC](https://discuss.pytorch.org/t/manually-feeding-trainable-parameters-to-the-optimizer/1724/1 "2017-04-07T08:05:54Z")

</div>

Suppose I don’t use `torch.nn.Module` and I have some trainable weight matrices `w1` and `w2`. Then how do I feed these parameters into the optimiser? Would I do something like? :

`torch.optim.SGD({w1,w2}, lr=1e-4)`

---

<div class="post-metadata">

**Author:** ![Tudor\_Berariu](https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/tudor_berariu/32/419_2.png) [@Tudor\_Berariu](https://discuss.pytorch.org/u/Tudor_Berariu)\
**Post date:** [April 7, 2017, 8:20am UTC](https://discuss.pytorch.org/t/manually-feeding-trainable-parameters-to-the-optimizer/1724/2 "2017-04-07T08:20:01Z")

</div>

The first argument you feed when initializing any optimizer should be an iterable of `Variable`s or dictionaries containing `Variable`s. In your case a simple list containing `w1` and `w2` should be fine as long as those are `Variable`s that _require_ gradients.

```
import torch
import torch.optim as optim
from torch.autograd import Variable

w = Variable(torch.randn(3, 5), requires_grad=True)
b = Variable(torch.randn(3), requires_grad=True)
x = Variable(torch.randn(5))

optimizer = optim.SGD([w,b], lr=0.01)

optimizer.zero_grad()
y = torch.mv(w, x) + b
y.backward(torch.randn(3))
optimizer.step()
```
