update.rst 1.71 KB
Newer Older
1 2 3 4 5 6 7
.. update.rst

###########################
Make a stateful computation
###########################

In this section, we show how to make a stateful computation from
8
nGraph's stateless operations. The basic idea is that any computation
9 10 11
with side-effects can be factored into a stateless function that
transforms the old state into the new state.

12
An example from C++
13 14 15 16 17
===================

Let's start with a simple C++ example, a function ``count`` that
returns how many times it has already been called:

18
.. literalinclude:: ../../../../examples/update/update.cpp
19 20
   :language: cpp
   :lines: 20-24
21
   :caption: update.cpp
22 23 24 25

The static variable ``counter`` provides state for this function. The
state is initialized to 0. Every time ``count`` is called, the current
value of ``counter`` is returned and ``counter`` is incremented. To
26
convert this to use a stateless function, define a function that
27 28 29
takes the current value of ``counter`` as an argument and returns the
updated value.

30
.. literalinclude:: ../../../../examples/update/update.cpp
31 32 33 34 35
   :language: cpp
   :lines: 26-29

To use this version of counting,

36
.. literalinclude:: ../../../../examples/update/update.cpp
37 38 39 40 41 42
   :language: cpp
   :lines: 36-48

Update in nGraph
================

43 44 45 46
In working with nGraph-based construction of graphs, updating takes 
the same approach. During training, we include all the weights as 
arguments to the training function and return the updated weights 
along with any other results. For more complex forms of training, 
47
such as those using momentum, we would add the momentum tensors 
48 49 50
as additional arguments and include their updated values as additional 
results. A simple case is illustrated in the documentation for how 
to :doc:`derive-for-training`.