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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258 |
1x
18x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
1x
1x
2x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
1x
3x
1x
1x
1x
1x
1x
3x
9x
1x
1x
3x
1x
1x
1x
1x | 'use strict';
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) E{ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { IEif (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
// lodash/fp random has a fixed arity of 2, without the last (and useful) param
var _d3Scale = require('d3-scale');
var _d3Array = require('d3-array');
var _fp = require('lodash/fp');
var _fp2 = _interopRequireDefault(_fp);
var _mlPca = require('ml-pca');
var _mlPca2 = _interopRequireDefault(_mlPca);
var _vector = require('./vector');
var _random = require('lodash/random');
var _random2 = _interopRequireDefault(_random);
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// A basic implementation of Kohonen map
// The main class
//
//
var Kohonen = function () {
// The constructor needs two params :
// * neurons : an already built neurons grid as an array
// * data : data set to consider
// * maxStep : the max step that will be clamped in scaleStepLearningCoef and
// scaleStepNeighborhood
// * maxLearningCoef
// * minLearningCoef
// * maxNeighborhood
// * minNeighborhood
//
// each neuron should provide a 2D vector pos,
// which refer to the grid position
//
// You should use an hexagon grid as it is the easier case
// to deal with neighborhood.
//
// You also should normalized your neighborhood in such a way that 2 neighbors
// got an euclidian distance of 1 between each other.
function Kohonen(_ref) {
var neurons = _ref.neurons;
var data = _ref.data;
var _ref$maxStep = _ref.maxStep;
var maxStep = _ref$maxStep === undefined ? 10000 : _ref$maxStep;
var _ref$maxLearningCoef = _ref.maxLearningCoef;
var maxLearningCoef = _ref$maxLearningCoef === undefined ? 1 : _ref$maxLearningCoef;
var _ref$minLearningCoef = _ref.minLearningCoef;
var minLearningCoef = _ref$minLearningCoef === undefined ? .3 : _ref$minLearningCoef;
var _ref$maxNeighborhood = _ref.maxNeighborhood;
var maxNeighborhood = _ref$maxNeighborhood === undefined ? 1 : _ref$maxNeighborhood;
var _ref$minNeighborhood = _ref.minNeighborhood;
var minNeighborhood = _ref$minNeighborhood === undefined ? .3 : _ref$minNeighborhood;
_classCallCheck(this, Kohonen);
this.size = data[0].length;
this.step = 0;
this.maxStep = maxStep;
// generate scaleStepLearningCoef,
// as the learning coef decreases with time
this.scaleStepLearningCoef = (0, _d3Scale.scaleLinear)().clamp(true).domain([0, maxStep]).range([maxLearningCoef, minLearningCoef]);
// decrease neighborhood with time
this.scaleStepNeighborhood = (0, _d3Scale.scaleLinear)().clamp(true).domain([0, maxStep]).range([maxNeighborhood, minNeighborhood]);
// retrive min and max for each feature
var unnormalizedExtents = _fp2.default.flow(_fp2.default.unzip, _fp2.default.map(_d3Array.extent))(data);
// build scales for data normalization
var scales = unnormalizedExtents.map(function (extent) {
return (0, _d3Scale.scaleLinear)().domain(extent).range([0, 1]);
});
// build normalized data
this.data = this.normalize(data, scales);
// then we store means and deviations for normalized datas
this.means = _fp2.default.flow(_fp2.default.unzip, _fp2.default.map(_d3Array.mean))(this.data);
this.deviations = _fp2.default.flow(_fp2.default.unzip, _fp2.default.map(_d3Array.deviation))(this.data);
// On each neuron, generate a random vector v
// of <size> dimension
var randomInitialVectors = this.generateInitialVectors(neurons.length);
this.neurons = neurons.map(function (n, i) {
return Object.assign({}, n, {
v: randomInitialVectors[i]
});
});
}
_createClass(Kohonen, [{
key: 'normalize',
value: function normalize(data, scales) {
return data.map(function (v) {
return v.map(function (s, i) {
return scales[i](s);
});
});
}
// learn and return corresponding neurons for the dataset
}, {
key: 'training',
value: function training() {
var log = arguments.length <= 0 || arguments[0] === undefined ? function () {} : arguments[0];
for (var i = 0; i < this.maxStep; i++) {
// generate a random vector
this.learn(this.generateLearningVector());
log(this.neurons, this.step);
}
}
}, {
key: 'mapping',
value: function mapping() {
return _fp2.default.flow(_fp2.default.map(this.findBestMatchingUnit.bind(this)), _fp2.default.map(function (n) {
return n.pos;
}))(this.data);
}
// The U-Matrix value of a particular node
// is the average distance between the node's weight vector and that of its closest neighbors.
}, {
key: 'umatrix',
value: function umatrix() {
var _this = this;
var roundToTwo = function roundToTwo(num) {
return +(Math.round(num + "e+2") + "e-2");
};
var findNeighors = function findNeighors(cn) {
return _fp2.default.filter(function (n) {
return roundToTwo((0, _vector.dist)(n.pos, cn.pos)) === 1;
}, _this.neurons);
};
return _fp2.default.map(function (n) {
return (0, _d3Array.mean)(findNeighors(n).map(function (nb) {
return (0, _vector.dist)(nb.v, n.v);
}));
}, this.neurons);
}
// pick a random vector among data
}, {
key: 'generateLearningVector',
value: function generateLearningVector() {
return this.data[_fp2.default.random(0, this.data.length - 1)];
}
}, {
key: 'generateInitialVectors',
value: function generateInitialVectors(dataSize) {
var _this2 = this;
// principal component analysis
// standardize to false as we already standardize ours
var pca = new _mlPca2.default(this.data, {
standardize: false
});
// centered covariance eigenvectors
var eigenvectors = pca.getEigenvectors();
// eigenvalues
var eigenvalues = pca.getEigenvalues();
// scale eigenvectors to the square root of eigenvalues
// we'll only keep the 2 largest eigenvectors
var scaledEigenvectors = _fp2.default.take(2, eigenvectors.map(function (v, i) {
return (0, _vector.mult)(v, Math.sqrt(eigenvalues[i]));
}));
// function to generate random vectors into eigenvectors space
var generateRandomVecWithinEigenvectorsSpace = function generateRandomVecWithinEigenvectorsSpace() {
return (0, _vector.add)((0, _vector.mult)(scaledEigenvectors[0], (0, _random2.default)(-1, 1, true)), (0, _vector.mult)(scaledEigenvectors[1], (0, _random2.default)(-1, 1, true)));
};
// we generate all random vectors and uncentered them by adding means vector
return _fp2.default.map(function () {
return (0, _vector.add)(generateRandomVecWithinEigenvectorsSpace(), _this2.means);
}, _fp2.default.range(0, dataSize));
}
}, {
key: 'learn',
value: function learn(v) {
var _this3 = this;
// find bmu
var bmu = this.findBestMatchingUnit(v);
// compute current learning coef
var currentLearningCoef = this.scaleStepLearningCoef(this.step);
this.neurons.forEach(function (n) {
// compute neighborhood
var currentNeighborhood = _this3.neighborhood({ bmu: bmu, n: n });
// compute delta for the current neuron
var delta = (0, _vector.mult)((0, _vector.diff)(n.v, v), currentNeighborhood * currentLearningCoef);
// update current vector
n.v = (0, _vector.add)(n.v, delta);
});
this.step += 1;
}
// Find closer neuron
}, {
key: 'findBestMatchingUnit',
value: function findBestMatchingUnit(v) {
return _fp2.default.flow(_fp2.default.sortBy(function (n) {
return (0, _vector.dist)(v, n.v);
}), _fp2.default.first)(this.neurons);
}
// http://en.wikipedia.org/wiki/Gaussian_function#Two-dimensional_Gaussian_function
//
// http://mathworld.wolfram.com/GaussianFunction.html
//
// neighborhood function made with a gaussian
}, {
key: 'neighborhood',
value: function neighborhood(_ref2) {
var bmu = _ref2.bmu;
var n = _ref2.n;
var a = 1;
var sigmaX = 1;
var sigmaY = 1;
return a * Math.exp(-(Math.pow(n.pos[0] - bmu.pos[0], 2) / 2 * Math.pow(sigmaX, 2) + Math.pow(n.pos[1] - bmu.pos[1], 2) / 2 * Math.pow(sigmaY, 2))) * this.scaleStepNeighborhood(this.step);
}
}]);
return Kohonen;
}();
exports.default = Kohonen; |