summaryrefslogtreecommitdiff
path: root/caffe2/observers/README.md
blob: 86ef3aed9f837eb704724ce1335cfbbe8797e092 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# Observers

## Usage

Observers are a small framework that allow users to attach code to the execution of SimpleNets and Operators.

An example of an Observer is the `TimeObserver`, used as follows:

### C++

```
unique_ptr<TimeObserver<NetBase>> net_ob =
    make_unique<TimeObserver<NetBase>>(net.get());
auto* ob = net->AddObserver(std::move(net_ob));
net->Run();
LOG(INFO) << "av time children: " << ob->average_time_children();
LOG(INFO) << "av time: " << ob->average_time();
```
  
### Python

```
model.net.AddTimeObserver()
ws.RunNet(model.net)
ob = model.net.GetObserver()

print("av time children:", ob.average_time_children())
print("av time:", ob.average_time())
```


## Implementing An Observer

To implement an observer you must inherit from `ObserverBase` and implement the `Start` and `Stop` functions.

Observers are instantiated with a `subject` of a generic type, such as a `Net` or `Operator`.  The observer framework is built to be generic enough to "observe" various other types, however.