-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneure.js
45 lines (41 loc) · 1.1 KB
/
neure.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
import * as active from './activation';
function random() {
return Math.random() * 2 - 1;
}
export default class Neure {
constructor(length, activation, isFirstLayer = false) {
this.length = length;
this.activation = activation;
this.b = 0;
this.weights = [];
for (let i = 0; i < length; i ++) { // 随机初始化权重
const weight = isFirstLayer ? 1 : random();
this.weights.push(weight);
}
}
// 加权求和
predict(input) {
let length = input.length;
let result = 0;
if (length !== this.length) {
throw new Error('输入数据与神经元权重长度不一致。');
}
for (let i = 0; i < length; i ++) {
result += this.weights[i] * input[i];
}
result += this.b;
// 激活函数处理
const fn = this.activation && active[this.activation];
if (fn) {
result = active[this.activation](result);
}
return result;
}
optimize(dw, db, step = 0.01) {
this.b -= db * step;
for (let i = 0; i < this.length; i ++) {
let weight = this.weights[i];
this.weights[i] -= dw[i] * step;
}
}
}