@mineproness/tensorloop 1.4.6 → 1.4.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.4.6",
3
+ "version": "1.4.8",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
@@ -49,22 +49,24 @@ export class LayerNorm {
49
49
  const invStd = cache.invStd;
50
50
  const size = input.length;
51
51
  const dInput = new Float32Array(size);
52
+ const normalized = new Float32Array(size);
52
53
  let sumDNorm = 0;
53
54
  let sumDNormX = 0;
54
- const normalized = new Float32Array(size);
55
55
  for (let i = 0; i < size; i++) {
56
- normalized[i] = (input[i] - mean) * invStd;
57
- const dNorm = dOutput[i] * this.gamma[i];
58
- sumDNorm += dNorm;
59
- sumDNormX += dNorm * normalized[i];
60
- const dGamma = dOutput[i] * normalized[i];
61
- const dBeta = dOutput[i];
56
+ normalized[i] =
57
+ (input[i] - mean) * invStd;
58
+ }
59
+ for (let i = 0; i < size; i++) {
60
+ const dNorm =
61
+ dOutput[i] * this.gamma[i];
62
62
 
63
- this.gamma[i] -= learningRate * dGamma;
64
- this.beta[i] -= learningRate * dBeta;
63
+ sumDNorm += dNorm;
64
+ sumDNormX +=
65
+ dNorm * normalized[i];
65
66
  }
66
67
  for (let i = 0; i < size; i++) {
67
- const dNorm = dOutput[i] * this.gamma[i];
68
+ const dNorm =
69
+ dOutput[i] * this.gamma[i];
68
70
  dInput[i] =
69
71
  (invStd / size) *
70
72
  (
@@ -73,6 +75,20 @@ export class LayerNorm {
73
75
  normalized[i] * sumDNormX
74
76
  );
75
77
  }
78
+ for (let i = 0; i < size; i++) {
79
+ const dGamma =
80
+ dOutput[i] * normalized[i];
81
+
82
+ const dBeta =
83
+ dOutput[i];
84
+
85
+ this.gamma[i] -=
86
+ learningRate * dGamma;
87
+
88
+ this.beta[i] -=
89
+ learningRate * dBeta;
90
+ }
91
+
76
92
  return dInput;
77
93
  }
78
94
 
@@ -18,62 +18,42 @@ export class TransformerBlock {
18
18
  normal1[a] = this.norm1.forward(input[a], a)
19
19
  }
20
20
  const attention = this.selfAttention.forward(normal1)
21
- const residual1 = []
22
- for (let a = 0; a < input.length; a++) {
23
- residual1[a] = new Float32Array(input[0].length)
24
- for (let i = 0; i < input[0].length; i++) {
25
- residual1[a][i] = input[a][i] + attention[a][i]
26
- }
27
- }
28
21
  const normal2 = []
29
- for (let a = 0; a < input.length; a++) {
30
- normal2[a] = this.norm2.forward(residual1[a], a)
31
- }
32
- const ffnOutput = this.ffn.forward(normal2)
33
- const output = []
34
- for (let g = 0; g < residual1.length; g++) {
35
- output[g] = new Float32Array(residual1[0].length)
36
- for (let a = 0; a < output[0].length; a++) {
37
- output[g][a] = residual1[g][a] + ffnOutput[g][a]
38
- }
22
+ for (let a = 0; a < attention.length; a++) {
23
+ normal2[a] = this.norm2.forward(attention[a], a)
39
24
  }
40
- return output
25
+ return this.ffn.forward(normal2)
41
26
  }
42
27
  backward(dOutput, learningRate) {
43
- const dFFN = this.ffn.backward(dOutput, learningRate)
44
- const dResidual1 = []
28
+ const dFFN =
29
+ this.ffn.backward(dOutput, learningRate)
30
+
31
+ const dAttentionInput = []
32
+
45
33
  for (let a = 0; a < dFFN.length; a++) {
46
- const dNorm2 = this.norm2.backward(
47
- dFFN[a],
48
- learningRate,
49
- a
50
- )
51
- dResidual1[a] = new Float32Array(dOutput[a].length)
52
- for (let i = 0; i < dOutput[a].length; i++) {
53
- dResidual1[a][i] =
54
- dOutput[a][i] + dNorm2[i]
55
- }
34
+ dAttentionInput[a] =
35
+ this.norm2.backward(
36
+ dFFN[a],
37
+ learningRate,
38
+ a
39
+ )
56
40
  }
57
- const dAttention = this.selfAttention.backward(
58
- dResidual1,
59
- learningRate
60
- )
61
- const dInputFromAttention = []
62
- for (let a = 0; a < dAttention.length; a++) {
63
- dInputFromAttention[a] = this.norm1.backward(
64
- dAttention[a],
65
- learningRate,
66
- a
41
+
42
+ const dAttention =
43
+ this.selfAttention.backward(
44
+ dAttentionInput,
45
+ learningRate
67
46
  )
68
- }
47
+
69
48
  const dInput = []
70
- for (let a = 0; a < dOutput.length; a++) {
71
- dInput[a] = new Float32Array(dOutput[a].length)
72
- for (let i = 0; i < dOutput[a].length; i++) {
73
- dInput[a][i] =
74
- dOutput[a][i] +
75
- dInputFromAttention[a][i]
76
- }
49
+
50
+ for (let a = 0; a < dAttention.length; a++) {
51
+ dInput[a] =
52
+ this.norm1.backward(
53
+ dAttention[a],
54
+ learningRate,
55
+ a
56
+ )
77
57
  }
78
58
 
79
59
  return dInput