-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path2d-weights.html
62 lines (48 loc) · 1.28 KB
/
2d-weights.html
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>2d weights</title>
</head>
<body>
<script src="index.js"></script>
<script>
const canvas = document.createElement('canvas')
const ctx = canvas.getContext('2d')
canvas.width = canvas.height = 800
document.body.appendChild(canvas)
const som = new SOM({
mapSize: 5,
vectorSize: 2,
radiusShrinkRate: 500,
learningRate: 0.1
})
const getRandVec = () => [
Math.random(),
Math.random()
]
const points = []
setInterval(function(){
ctx.clearRect(0,0,800,800)
const input = getRandVec()
const { bmi } = som.compute(input)
som.train(input,bmi)
for(var i = 0; i < som.units.length; i++){
const { vector } = som.units[i]
const distance = som.getUnitDistance(bmi,som.units[i])
ctx.fillStyle = `rgba(0,0,0,${distance+0.08})`
ctx.fillRect(vector[0]*200,vector[1]*200,4,4)
}
ctx.fillStyle = `red`
ctx.fillRect(input[0]*200,input[1]*200,5,5)
for(var x = 0; x < 100; x++){
for(var y = 0; y < 100; y++){
const { bmiIndex } = som.compute([x/100,y/100])
ctx.fillStyle = `hsl(${parseInt((bmiIndex/som.units.length)*255)},100%,50%)`
ctx.fillRect(220+x,40+y,1,1)
}
}
},10)
</script>
</body>
</html>