@mineproness/tensorloop 1.2.4 → 1.2.5

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/index.js CHANGED
@@ -5,10 +5,14 @@ import { Softmax as softmax2 } from "./src/Softmax/index.js";
5
5
  import { crossEntropy } from "./src/crossEntropy/index.js";
6
6
  import {tools} from './src/tools/index.js'
7
7
  import {ReLU} from './src/ReLU/index.js'
8
+ import {PositionEmbedding as PE} from './src/PositionEmbedding/index.js'
9
+ import {SelfAttention as ST} from './src/SelfAttention/index.js'
8
10
  export const Tokenizer = tokenizer
9
11
  export const Embedding = embeddding2
10
12
  export const Linear = li2
11
13
  export const Softmax = softmax2
12
14
  export const CrossEntropy = crossEntropy
13
15
  export const Tools = tools
14
- export const ReLu = ReLU
16
+ export const ReLu = ReLU
17
+ export const PositionEmbedding = PE
18
+ export const SelfAttention = ST
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.2.4",
3
+ "version": "1.2.5",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
@@ -7,6 +7,6 @@ export class Linear{
7
7
  }
8
8
  })
9
9
  forward(input : number[]) : Float32Array[]
10
- backward(outGradient : number[] , learingRate: number) : Float32Array[]
10
+ backward(outGradient : number[] , learingRate: number, idx: number) : Float32Array[]
11
11
  Save() : void
12
12
  }
@@ -44,7 +44,7 @@ export class Linear {
44
44
  Save(this.configs.save.filename[1], this.Bias)
45
45
  }
46
46
  forward(input) {
47
- this.input = input
47
+ this.input.push(input)
48
48
  const output = new Float32Array(this.Weights.length)
49
49
  for (let a = 0; a < this.Weights.length; a++) {
50
50
  let sum = this.Bias[a]
@@ -55,10 +55,10 @@ export class Linear {
55
55
  }
56
56
  return output
57
57
  }
58
- backward(outputGradient, learningRate) {
59
- const input = this.input;
58
+ backward(outputGradient, learningRate, idx) {
59
+ const input = this.input[idx];
60
60
  const weights = this.Weights
61
- const inputGradient = new Float32Array(this.input.length)
61
+ const inputGradient = new Float32Array(input.length)
62
62
  for (let a = 0; a < weights.length; a++) {
63
63
  const gran = outputGradient[a]
64
64
  const w = weights[a]
@@ -0,0 +1,12 @@
1
+ export class PositionEmbedding{
2
+ vectors: Float32Array[]
3
+ constructor(embeddingSize: number, contextSize: number, configs: {
4
+ save: {
5
+ filename: string
6
+ }
7
+ })
8
+ forward(position: number | number[]) : Float32Array[] | Float32Array[][]
9
+ backward(pos: number , inputGradient: number[] | Float32Array[], learingRate: number) : void
10
+ Save() : void;
11
+ addPos(PSEM, vectors : Float32Array[][]) : Float32Array
12
+ }
@@ -0,0 +1,72 @@
1
+ import fs, { existsSync } from 'fs'
2
+ import { GetEmbeddingVectors, SaveEmbeddingVectors, SaveVectors } from '../GetConfigs.js'
3
+ // import { config } from 'process'
4
+
5
+ function GenerateVectors(embeddingSize, vocabSize) {
6
+ const vector = []
7
+ for (let token = 0; token < vocabSize; token++) {
8
+ vector[token] = new Float32Array(embeddingSize)
9
+ for (let a = 0; a < embeddingSize; a++) {
10
+ vector[token][a] = (Math.random() * 2 - 1) * 0.2
11
+ }
12
+ }
13
+ return vector
14
+ }
15
+
16
+ export class PositionEmbedding {
17
+ vectors = []
18
+ configs;
19
+ embeddingSize = 0
20
+ constructor(embeddingSize, vocabSize, configs) {
21
+ this.configs = configs
22
+ this.embeddingSize = embeddingSize
23
+ if (configs && configs.save) {
24
+ if (existsSync(configs.save.filename)) {
25
+ this.vectors = []
26
+ } else {
27
+ this.vectors = GenerateVectors(embeddingSize, vocabSize)
28
+ this.Save()
29
+ }
30
+ } else {
31
+ this.vectors = GenerateVectors(embeddingSize, vocabSize)
32
+
33
+ }
34
+ }
35
+ forward(token) {
36
+ if (Array.isArray(token)) {
37
+ return token.map((e) => {
38
+ const id = this.vectors[e]
39
+ if (id) return id
40
+ this.vectors[e] = GetEmbeddingVectors(e, this.embeddingSize, this.configs.save.filename)
41
+ return this.vectors[e]
42
+ })
43
+ } else {
44
+ if (!this.vectors[token]) {
45
+ this.vectors[token] = GetEmbeddingVectors(token, this.embeddingSize, this.configs.save.filename)
46
+ }
47
+ return this.vectors[token]
48
+ }
49
+ }
50
+ backward(token, inputGradient, learingRate) {
51
+ for (let a = 0; a < this.vectors[token].length; a++) {
52
+ this.vectors[token][a] -= learingRate * inputGradient[a]
53
+ }
54
+ }
55
+ Save() {
56
+ SaveEmbeddingVectors(this.vectors, this.configs.save.filename)
57
+ }
58
+ addPos(PSEM, xEM) {
59
+ const out = []
60
+
61
+ for (let pos = 0; pos < xEM.length; pos++) {
62
+ const tokenVector = xEM[pos]
63
+ const posVector = PSEM.forward(pos)
64
+ const x = new Float32Array(xEM[0].length)
65
+ for (let a = 0; a < tokenVector.length; a++) {
66
+ x[a] = tokenVector[a] + posVector[a]
67
+ }
68
+ out.push(x)
69
+ }
70
+ return out
71
+ }
72
+ }
@@ -0,0 +1,11 @@
1
+ export class SelfAttention{
2
+ q : Float32Array[]
3
+ v : Float32Array[]
4
+ k : Float32Array[]
5
+ input;
6
+ weights : Float32Array[]
7
+ score : number
8
+ forward(vectors: Float32Array[])
9
+ Save() : void
10
+ backward(dOutput, learningRate)
11
+ }
@@ -0,0 +1,118 @@
1
+ import { Softmax } from '../../index.js';
2
+ import { Linear } from '../Linear/index.js'
3
+
4
+ export class SelfAttention {
5
+ query;
6
+ key;
7
+ value;
8
+ v;
9
+ k;
10
+ q;
11
+ input;
12
+ configs;
13
+ score;
14
+ softmax;
15
+ weights;
16
+ constructor(embeddingSize, configs) {
17
+ this.configs = configs
18
+ this.softmax = new Softmax()
19
+ this.score = Math.sqrt(embeddingSize)
20
+ this.query = new Linear(embeddingSize, embeddingSize, { save: { filename: [`./${configs.dirname}/query.bin`, `./${configs.dirname}/queryBias.bin`] } })
21
+ this.key = new Linear(embeddingSize, embeddingSize, { save: { filename: [`./${configs.dirname}/key.bin`, `./${configs.dirname}/keyBias.bin`] } })
22
+ this.value = new Linear(embeddingSize, embeddingSize, { save: { filename: [`./${configs.dirname}/value.bin`, `./${configs.dirname}/valueBias.bin`] } })
23
+ }
24
+ forward(vectors) {
25
+ this.q = vectors.map((e) => this.query.forward(e))
26
+ this.v = vectors.map((e) => this.value.forward(e))
27
+ this.k = vectors.map((e) => this.key.forward(e))
28
+ // console.log(this.q, this.k, this.v)
29
+ const attentionSocres = []
30
+ for (let i = 0; i < this.q.length; i++) {
31
+ attentionSocres[i] = new Float32Array(this.k.length)
32
+ for (let a = 0; a < this.k.length; a++) {
33
+ let dot = 0;
34
+ for (let d = 0; d < this.q[i].length; d++) {
35
+ dot += this.q[i][d] * this.k[a][d]
36
+ }
37
+ attentionSocres[i][a] = dot / this.score
38
+ }
39
+ }
40
+ const weights = attentionSocres.map((e) => this.softmax.forward(e))
41
+ this.weights = weights
42
+ const output = [];
43
+
44
+ for (let i = 0; i < weights.length; i++) {
45
+ output[i] = new Float32Array(this.v[0].length);
46
+
47
+ for (let j = 0; j < this.v.length; j++) {
48
+ for (let d = 0; d < this.v[j].length; d++) {
49
+ output[i][d] += weights[i][j] * this.v[j][d];
50
+ }
51
+ }
52
+ }
53
+
54
+ return output;
55
+ }
56
+ softmaxBack(probs, dOut) {
57
+ const dIn = new Float32Array(probs.length);
58
+
59
+ for (let i = 0; i < probs.length; i++) {
60
+ let sum = 0;
61
+
62
+ for (let j = 0; j < probs.length; j++) {
63
+ const jacobian =
64
+ i === j
65
+ ? probs[i] * (1 - probs[i])
66
+ : -probs[i] * probs[j];
67
+
68
+ sum += jacobian * dOut[j];
69
+ }
70
+
71
+ dIn[i] = sum;
72
+ }
73
+
74
+ return dIn;
75
+ }
76
+ backward(dOutput, learningRate) {
77
+ const dV = this.v.map((e) => new Float32Array(e.length));
78
+ const dWeights = []
79
+ for (let a = 0; a < this.weights.length; a++) {
80
+ dWeights[a] = new Float32Array(this.weights[a].length)
81
+ for (let j = 0; j < this.v.length; j++) {
82
+ for (let r = 0; r < this.v[j].length; r++) {
83
+ dWeights[a][j] += dOutput[a][r] * this.v[j][r]
84
+ dV[j][r] += dOutput[a][r] * this.weights[a][j]
85
+ }
86
+ }
87
+ }
88
+ const dScores = []
89
+ for (let t = 0; t < dWeights.length; t++) {
90
+ dScores[t] = this.softmaxBack(this.weights[t], dWeights[t])
91
+ }
92
+ const dQ = this.q.map(q => new Float32Array(q.length));
93
+ const dK = this.k.map(k => new Float32Array(k.length));
94
+ for (let a = 0; a < this.q.length; a++) {
95
+ for (let j = 0; j < this.k.length; j++) {
96
+ for (let r = 0; r < this.q[a].length; r++) {
97
+ dQ[a][r] += dScores[a][j] * this.k[j][r] / this.score;
98
+ dK[j][r] += dScores[a][j] * this.q[a][r] / this.score;
99
+ }
100
+ }
101
+ }
102
+ const dInput = this.q.map(() => new Float32Array(this.q[0].length))
103
+ for (let a = 0; a < this.q.length; a++) {
104
+ const dq = this.query.backward(dQ[a], learningRate, a)
105
+ const dk = this.key.backward(dK[a], learningRate, a)
106
+ const dv = this.value.backward(dV[a], learningRate, a)
107
+ for (let b = 0; b < dq.length; b++) {
108
+ dInput[a][b] = dq[b] + dk[b] + dv[b]
109
+ }
110
+ }
111
+ return dInput
112
+ }
113
+ Save() {
114
+ this.query.Save()
115
+ this.key.Save()
116
+ this.value.Save()
117
+ }
118
+ }