-
Notifications
You must be signed in to change notification settings - Fork 0
/
simplest-ann-cost-function.js
168 lines (135 loc) · 3.77 KB
/
simplest-ann-cost-function.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
//adapted from: http://matt.might.net/articles/rendering-mathematical-functions-in-javascript-with-canvas-html/
let simplestCostFunctionGraph = (canvasId, etaId) => {
let canvas = document.getElementById(canvasId)
let ctx = canvas.getContext('2d')
let width = canvas.width
let height = canvas.height
let fps = 5
let drawCounter = 0
let sig = z => 1 / (1 + Math.exp(-z))
let cost = (activation, desiredValue) => 0.5 * (desiredValue - activation) ** 2
let sigPrime = z => sig(z)*(1-sig(z))
// get logical viewport
let vp = {
min: {
x: -15,
y: -5
},
max: {
x: 15,
y: 5
}
}
let marks = {
x: 1,
y: 1
}
// Returns the physical x-coordinate of a logical x-coordinate
let xPhys = x => (x - vp.min.x) / (vp.max.x - vp.min.x) * width
// Returns the physical y-coordinate of a logical y-coordinate
let yPhys = y => height - (y - vp.min.y) / (vp.max.y - vp.min.y) * height
function drawAxes() {
ctx.save()
ctx.linewidth = 2
ctx.strokeStyle = "black"
createAxisPart(0, vp.min.y)
createAxisPart(0, vp.max.y)
createAxisPart(vp.min.x, 0)
createAxisPart(vp.max.x, 0)
createAxisMarkPartX(vp.min.x, -1)
createAxisMarkPartX(vp.max.x, 1)
createAxisMarkPartY(vp.min.y, -1)
createAxisMarkPartY(vp.max.y, 1)
ctx.restore()
}
function createAxisPart(x, y){
ctx.beginPath()
ctx.moveTo(xPhys(0), yPhys(0))
ctx.lineTo(xPhys(x), yPhys(y)) //e.g. (vp.min.x, 0) or (0, vp.max.y)
ctx.stroke()
}
function createAxisMarkPartX(amountOfMarks, sign){
for (let i = 1; i < amountOfMarks * sign; i++) {
ctx.beginPath()
ctx.moveTo(xPhys(i * sign), yPhys(0) - 5)
ctx.lineTo(xPhys(i * sign), yPhys(0) + 5)
ctx.stroke()
}
}
function createAxisMarkPartY(amountOfMarks, sign){
for (let i = 1; i < amountOfMarks * sign; i++) {
ctx.beginPath()
ctx.moveTo(xPhys(0) - 5, yPhys(i * sign))
ctx.lineTo(xPhys(0) + 5, yPhys(i * sign))
ctx.stroke()
}
}
function drawCostFunction(x, desiredValue, color) {
let xStep = 0.01
ctx.beginPath()
ctx.strokeStyle = color
let wx = vp.min.x
let z = wx * x
let y = cost(z, desiredValue)
ctx.moveTo(xPhys(wx), yPhys(y))
for (wx = wx + xStep; wx <= vp.max.x; wx += xStep) {
z = wx * x
y = cost(z, desiredValue)
ctx.lineTo(xPhys(wx), yPhys(y))
}
ctx.stroke()
}
function drawGraph(etaId) {
ctx.clearRect(0, 0, width, height)
drawAxes()
drawCostFunction(1, -5, "red")
let drawGradientDescent = startDrawGradientDescent()
drawGradientDescent()
}
function startDrawGradientDescent(){
let eta = document.getElementById(etaId).value
let desiredValue = -5
let x = 1 //training example
let wx = -2 //starting point
let activation = x * wx
let y = cost(activation, desiredValue)
// console.log(`wx, y, desiredValue, eta`)
// console.log(wx, y, desiredValue, eta)
return function drawGradientDescent(){
ctx.beginPath()
ctx.strokeStyle = "blue"
ctx.moveTo(xPhys(wx), yPhys(y))
y = cost(activation, desiredValue)
if(y <= 0.001) {
ctx.closePath()
}
activation = x * wx
let dcost_da = activation - desiredValue
let da_dw = x
let error = dcost_da * da_dw //dcost/da * da/dw
wx = wx - eta * error
console.log(drawCounter++)
console.log(`wx, y, desiredValue, error, eta`)
console.log(wx, y, desiredValue, error, eta)
ctx.lineTo(xPhys(wx), yPhys(y))
ctx.stroke()
setTimeout(() => {
requestAnimationFrame(drawGradientDescent)
}, 1000 / fps);
}
}
function drawGraph() {
ctx.clearRect(0, 0, width, height)
drawAxes()
drawCostFunction(1, -5, "red")
let drawGradientDescent = startDrawGradientDescent()
drawGradientDescent()
}
if(etaId){
document.getElementById('simplest-ann-cost-function-simplest-gd-play').addEventListener('click', () => {
drawGraph(etaId)
})
}
drawAxes()
drawCostFunction(1, -5, "red")
}