Skip to content

Commit

Permalink
Example/simple optim (#266)
Browse files Browse the repository at this point in the history
* Add simple optimisation example

* Update docs

* Update docs

* Update docs

* Update docs

* Update basic optimisation example
  • Loading branch information
MattPainter01 authored and ethanwharris committed Jul 30, 2018
1 parent f928a07 commit 3e46052
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
6 changes: 3 additions & 3 deletions docs/_static/examples/basic_opt.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ def process(self, state):
return state['est'].data


steps = torch.tensor(list(range(50000)))
p = torch.tensor([2.0, 1.0, 10.0])
training_steps = 50000

model = Net(p)
optim = torch.optim.SGD(model.parameters(), lr=0.0001)

tbmodel = tb.Model(model, optim, loss, [est(), 'loss'])
tbmodel.fit(steps, steps, 1, pass_state=True)
tbmodel = tb.Model(model, optim, loss, [est(), 'loss']).to('cuda')
tbmodel.fit_generator(None, pass_state=True, train_steps=training_steps)
print(list(model.parameters())[0].data)
12 changes: 10 additions & 2 deletions docs/examples/basic_opt.rst
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,14 @@ We define the model and optimiser in the standard way.
:language: python
:lines: 46-47

Finally we start the optimising (giving as "data" and "targets" the number of steps desired) and print the final minimum estimate.
Finally we start the optimising on the GPU and print the final minimum estimate.

.. literalinclude:: /_static/examples/basic_opt.py
:language: python
:lines: 49-51

Note that we could use targets that are meaningful as they are given to the loss function, however this is not done for this example.
Usually torchbearer will infer the number of training steps from the data generator.
Since for this example we have no data to give the model (which will be passed `None`), we need to tell torchbearer how many steps to run by setting the ``training_steps`` argument.


Viewing Progress
Expand All @@ -68,3 +69,10 @@ This simple metric is used to display the estimate throughout the optimisation p
The final estimate is very close to our desired minimum at [5, 0, 1]:

tensor([ 4.9988e+00, 4.5355e-05, 1.0004e+00])

Source Code
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The source code for the example is given below:

:download:`Download Python source code: basic_opt.py </_static/examples/basic_opt.py>`

0 comments on commit 3e46052

Please sign in to comment.