-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplayground.py
44 lines (31 loc) · 910 Bytes
/
playground.py
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
import chained_operations as op
import optimizers
import plotting
import numpy as np
import itertools as it
y_ = np.array([1, 0])
x = op.Placeholder()
g = op.Gradient(x)
y = op.Mul(-1, op.Log(op.Mul(op.Sum(op.Mul(y_, op.Exp(x))), op.Reciprocal(op.Sum(op.Exp(x))))))
y2 = op.Mul(-1, op.Log(op.Mul(op.Dot(y_, op.Exp(x)), op.Reciprocal(op.Sum(op.Exp(x))))))
#y = op.Sum(op.Mul(-3, x))
def f(inp):
return op.run(y2, {x: inp})
def grad(inp, _):
op.run(y2, {x: inp})
return op.run(g)
data1 = np.random.rand(*y_.shape)
data2 = np.copy(data1)
data2[1] += 1
graph1 = plotting.Graph('numeric')
graph2 = plotting.Graph('backprop')
opt1 = optimizers.NEG(f)
opt2 = optimizers.NEG(f, gradient_func=grad)
for i in it.count():
evl = f(data1)
graph1.maybe_add(i, i, evl)
opt1.optimize(data1)
evl = f(data2)
graph2.maybe_add(i, i, evl)
opt2.optimize(data2)
plotting.replot()