-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraph.h
59 lines (49 loc) · 1.46 KB
/
graph.h
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef _GRAPH_H_
#define _GRAPH_H_
#include <vector>
#include "operation.h"
using namespace std;
class Operation;
class Variable;
// represents a computation graph
// this class abstracts a good interface for operations
// and runs the operations when given a output operation
// example:
/*
Graph g;
auto x = g.variable(2);
auto y = g.exp(x);
g.run(y);
cout << y->result << endl; // prints e^2
cout << x->grad << endl; // prints dy/dx
*/
class Graph {
private:
// operations in the graph
vector<Operation*> ops;
// clear all the grads in operations
// internal function dont use it outside
void zero_grads();
public:
// creates a operation of type Variable and returns a pointer to it
Variable* variable(double value = 0);
// all of the operation interface functions asks for Operation* parameter(s)
// then creates a new operation and returns its pointer
Operation* add(Operation* op1, Operation* op2);
Operation* sub(Operation* op1, Operation* op2);
Operation* mul(Operation* op1, Operation* op2);
Operation* div(Operation* op1, Operation* op2);
Operation* log(Operation* op);
Operation* exp(Operation* op);
Operation* pow(Operation* op1, Operation* op2);
Operation* sqrt(Operation* op);
Operation* sin(Operation* op);
Operation* cos(Operation* op);
Operation* tan(Operation* op);
Operation* asin(Operation* op);
Operation* acos(Operation* op);
Operation* atan(Operation* op);
// run the graph given a output operation
double run(Operation *output);
};
#endif