@mineproness/tensorloop 1.3.4 → 1.3.7

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
@@ -7,6 +7,7 @@ import {tools} from './src/tools/index.js'
7
7
  import {ReLU} from './src/ReLU/index.js'
8
8
  import {PositionEmbedding as PE} from './src/PositionEmbedding/index.js'
9
9
  import {SelfAttention as ST} from './src/SelfAttention/index.js'
10
+ import {GELU as GU} from './src/GELU/index.js'
10
11
  export const Tokenizer = tokenizer
11
12
  export const Embedding = embeddding2
12
13
  export const Linear = li2
@@ -15,4 +16,5 @@ export const CrossEntropy = crossEntropy
15
16
  export const Tools = tools
16
17
  export const ReLu = ReLU
17
18
  export const PositionEmbedding = PE
18
- export const SelfAttention = ST
19
+ export const SelfAttention = ST
20
+ export const GELU = GU
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mineproness/tensorloop",
3
- "version": "1.3.4",
3
+ "version": "1.3.7",
4
4
  "description": "The most powerfull Machine Learning Library for AI.",
5
5
  "keywords": [
6
6
  "ai",
package/readme.md CHANGED
@@ -8,7 +8,7 @@ A Library that Supply AI Tools and Large Language model Development tools like E
8
8
  - Linear Weights and Bias.
9
9
  - Softmax forward and Backward.
10
10
  - CrossEntroy for Loss Calculation.
11
- - Relu for Stable Layer.
11
+ - Relu and GELU for Stable Layer.
12
12
  - Tokenizer for convert into tokens.
13
13
  - Self attention
14
14
  - Position Embedding
@@ -0,0 +1,6 @@
1
+ export class GELU{
2
+ input : Float32Array[];
3
+ forward(input: Float32Array, index: number) : Float32Array;
4
+ backward(Doutput: Float32Array, index: number) : Float32Array;
5
+ ClearInputCache() : void;
6
+ }
@@ -0,0 +1,43 @@
1
+ export default class GELU {
2
+ constructor() {
3
+ this.inputs = []
4
+ }
5
+
6
+ forward(input, index = 0) {
7
+ this.inputs[index] = new Float32Array(input)
8
+ const output = new Float32Array(input.length)
9
+ const k = Math.sqrt(2 / Math.PI)
10
+ for (let i = 0; i < input.length; i++) {
11
+ const x = input[i]
12
+ const u = k * (x + 0.044715 * x * x * x)
13
+ output[i] = 0.5 * x * (1 + Math.tanh(u))
14
+ }
15
+ return output
16
+ }
17
+
18
+ backward(dOutput, index = 0) {
19
+ const input = this.inputs[index]
20
+ if (!input) throw new Error(`No GELU cache for index ${index}`);
21
+ const grad = new Float32Array(dOutput.length)
22
+ const k = Math.sqrt(2 / Math.PI)
23
+ for (let i = 0; i < dOutput.length; i++) {
24
+ const x = input[i]
25
+ const x3 = x * x * x
26
+ const u = k * (x + 0.044715 * x3)
27
+ const t = Math.tanh(u)
28
+ const sech2 = 1 - t * t
29
+ const duDx = k * (1 + 3 * 0.044715 * x * x)
30
+ const geluGrad =
31
+ 0.5 * (1 + t) +
32
+ 0.5 * x * sech2 * duDx
33
+
34
+ grad[i] = dOutput[i] * geluGrad
35
+ }
36
+
37
+ return grad
38
+ }
39
+
40
+ ClearInputCache() {
41
+ this.inputs = []
42
+ }
43
+ }
@@ -4,9 +4,8 @@ export class SelfAttention{
4
4
  k : Float32Array[]
5
5
  input;
6
6
  constructor(embeddingSize : number, configs: {
7
- save: {
8
7
  dirname: string
9
- }
8
+
10
9
  })
11
10
  weights : Float32Array[]
12
11
  score : number