-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathengine.js
113 lines (113 loc) · 3 KB
/
engine.js
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.tanh = exports.relu = exports.div = exports.neg = exports.pow = exports.mul = exports.sub = exports.add = exports.Value = void 0;
class Value {
data;
_children;
_op;
grad = 0;
_backward = () => { };
constructor(data, _children = [], _op = '') {
this.data = data;
this._children = _children;
this._op = _op;
}
backward() {
const topo = [];
const visited = new Set();
function buildTopo(v) {
if (!visited.has(v)) {
visited.add(v);
for (const child of v._children) {
buildTopo(child);
}
topo.push(v);
}
}
buildTopo(this);
this.grad = 1;
for (const v of topo.reverse()) {
v._backward();
}
}
}
exports.Value = Value;
const add = (...args) => {
const out = new Value(args.reduce((acc, cur) => acc + cur.data, 0), args, '+');
function _backward() {
for (const arg of args) {
arg.grad += 1 * out.grad;
}
}
out._backward = _backward;
return out;
};
exports.add = add;
const sub = (a, b) => {
const out = new Value(a.data - b.data, [a, b], '-');
function _backward() {
a.grad += 1 * out.grad;
b.grad += -1 * out.grad;
}
out._backward = _backward;
return out;
};
exports.sub = sub;
const mul = (...args) => {
const out = new Value(args.reduce((acc, cur) => acc * cur.data, 1), args, '*');
function _backward() {
for (const arg of args) {
arg.grad += (out.grad * out.data) / arg.data;
}
}
out._backward = _backward;
return out;
};
exports.mul = mul;
const pow = (a, b) => {
const out = new Value(a.data ** b.data, [a, b], '**');
function _backward() {
a.grad += b.data * a.data ** (b.data - 1) * out.grad;
b.grad += Math.log(a.data) * a.data ** b.data * out.grad;
}
out._backward = _backward;
return out;
};
exports.pow = pow;
const neg = (a) => {
const out = new Value(-a.data, [a], '-');
function _backward() {
a.grad += -1 * out.grad;
}
out._backward = _backward;
return out;
};
exports.neg = neg;
const div = (a, b) => {
const out = new Value(a.data / b.data, [a, b], '/');
function _backward() {
a.grad += (1 / b.data) * out.grad;
b.grad += (-a.data / b.data ** 2) * out.grad;
}
out._backward = _backward;
return out;
};
exports.div = div;
const relu = (a) => {
const out = new Value(a.data < 0 ? 0 : a.data, [a], "relu");
function _backward() {
a.grad += (out.data > 0 ? 1 : 0) * out.grad;
}
out._backward = _backward;
return out;
};
exports.relu = relu;
const tanh = (a) => {
const out = new Value(Math.tanh(a.data), [a], "tanh");
function _backward() {
a.grad += (1 - out.data ** 2) * out.grad;
}
out._backward = _backward;
return out;
};
exports.tanh = tanh;