@mineproness/tensorloop 2.1.44 → 2.1.45
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 +1 -1
- package/src/Model/index.d.ts +7 -0
- package/src/Model/index.js +93 -0
package/package.json
CHANGED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
import { Embedding, PositionEmbedding, Linear, Tokenizer, TransformerBlock, Softmax } from '../../index.js'
|
|
2
|
+
import path, { join } from 'path'
|
|
3
|
+
export default class Model {
|
|
4
|
+
embedding;
|
|
5
|
+
linear;
|
|
6
|
+
posEmbedding;
|
|
7
|
+
softmax;
|
|
8
|
+
tokenizer;
|
|
9
|
+
block1;
|
|
10
|
+
block2;
|
|
11
|
+
contextSize;
|
|
12
|
+
constructor(embeddingSize, contextSize, configs) {
|
|
13
|
+
this.tokenizer = new Tokenizer()
|
|
14
|
+
this.contextSize = contextSize
|
|
15
|
+
this.embedding = new Embedding(embeddingSize, this.tokenizer.vocabSize, contextSize, {
|
|
16
|
+
save: {
|
|
17
|
+
filename: path.join(configs.dirname, "vectors.bin")
|
|
18
|
+
}
|
|
19
|
+
})
|
|
20
|
+
this.posEmbedding = new PositionEmbedding(embeddingSize, contextSize, {
|
|
21
|
+
save: {
|
|
22
|
+
filename: join(configs.dirname, 'posVec.bin')
|
|
23
|
+
}
|
|
24
|
+
})
|
|
25
|
+
this.linear = new Linear(embeddingSize, this.tokenizer.vocabSize, {
|
|
26
|
+
save: {
|
|
27
|
+
filename: [join(configs.dirname, 'weights.bin'), join(configs.dirname, 'bias.bin')]
|
|
28
|
+
}
|
|
29
|
+
})
|
|
30
|
+
this.softmax = new Softmax()
|
|
31
|
+
this.block1 = new TransformerBlock(embeddingSize, {
|
|
32
|
+
dirname: join(configs.dirname, "block1"),
|
|
33
|
+
activation: configs.activation
|
|
34
|
+
})
|
|
35
|
+
this.block2 = new TransformerBlock(embeddingSize, {
|
|
36
|
+
dirname: join(configs.dirname, "block2"),
|
|
37
|
+
activation: configs.activation
|
|
38
|
+
})
|
|
39
|
+
}
|
|
40
|
+
train(text = "", batchSize = 5000, batchExecution = 0, epochs = 500, learningRate = 0.01) {
|
|
41
|
+
process.stdout.write("\x1Bc")
|
|
42
|
+
console.log("Preparing Training Pairs....")
|
|
43
|
+
const tokens = text.split(/\r?n/).map((e) => e.trim()).filter((e) => e.length > 0).map((e)=> e + " <EOS>").map((e) => this.tokenizer.encoder(e).tokenIDs).flat()
|
|
44
|
+
const trainingPairs2 = []
|
|
45
|
+
for (let a = 0; a + this.contextSize < tokens.length; a++) {
|
|
46
|
+
trainingPairs2.push({
|
|
47
|
+
inputIDX: tokens.slice(a, a + this.contextSize),
|
|
48
|
+
targetIDX: tokens.slice(a + 1, a + this.contextSize + 1)
|
|
49
|
+
})
|
|
50
|
+
}
|
|
51
|
+
const trainingPairs = []
|
|
52
|
+
for (let a = 0; a < trainingPairs2.length; batchSize++) {
|
|
53
|
+
trainingPairs.push(trainingPairs2.slice(a, a + batchSize))
|
|
54
|
+
}
|
|
55
|
+
const trainingPair = trainingPairs[batchExecution]
|
|
56
|
+
let firstRun = true;
|
|
57
|
+
console.log(`TotalPairs: ${trainingPair.length}`)
|
|
58
|
+
if (trainingPair.length == 0 || !trainingPair) throw new Error(`The Batch Execution ${batchExecution} is doesn't exist.`);
|
|
59
|
+
for (let i = 0; i < epochs; i++) {
|
|
60
|
+
let totalLoss = 0;
|
|
61
|
+
for (let a = 0; a < trainingPair.length; a++) {
|
|
62
|
+
const input = trainingPair[a].inputIDX
|
|
63
|
+
const target = trainingPair[a].targetIDX
|
|
64
|
+
let vector = this.embedding.forward(input)
|
|
65
|
+
vector = this.posEmbedding.forward(vector)
|
|
66
|
+
vector = this.block1.forward(vector)
|
|
67
|
+
vector = this.block2.forward(vector)
|
|
68
|
+
const logits = this.linear.forward(vector)
|
|
69
|
+
const probs = this.softmax.forward(logits)
|
|
70
|
+
const loss = this.softmax.crossEntropy(probs, target)
|
|
71
|
+
const dLogits = this.softmax.backward(probs, target)
|
|
72
|
+
totalLoss += loss;
|
|
73
|
+
let dOutput = this.linear.backward(dLogits, learningRate);
|
|
74
|
+
dOutput = this.block2.backward(dOutput, learningRate)
|
|
75
|
+
dOutput = this.block1.backward(dOutput, learningRate)
|
|
76
|
+
this.posEmbedding.backward(dOutput, learningRate)
|
|
77
|
+
this.embedding.backward(input, dOutput, learningRate)
|
|
78
|
+
if (firstRun) {
|
|
79
|
+
firstRun = null;
|
|
80
|
+
console.log("")
|
|
81
|
+
}
|
|
82
|
+
const percentense = (((a + 1) / trainingPair.length) * 100).toFixed(2)
|
|
83
|
+
const filted = Math.round((percentense / 100) * 20);
|
|
84
|
+
const empty = 20 - filted
|
|
85
|
+
process.stdout.write(`\r[${"=".repeat(filted)}${" ".repeat(empty)}] ${percentense}% (${a + 1}/${trainingPair.length}) | loss ${(totalLoss / (a + 1)).toFixed(2)}`)
|
|
86
|
+
}
|
|
87
|
+
this.embedding.Save()
|
|
88
|
+
this.linear.Save()
|
|
89
|
+
this.block1.Save()
|
|
90
|
+
this.block2.Save()
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|