@misterscan/sesi 1.1.1 → 1.2.0
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/README.md +44 -19
- package/bin/sesi.js +35 -34
- package/dist/ai-runtime.d.ts +5 -0
- package/dist/ai-runtime.d.ts.map +1 -1
- package/dist/ai-runtime.js +157 -7
- package/dist/ai-runtime.js.map +1 -1
- package/dist/builtins.d.ts +1 -1
- package/dist/builtins.d.ts.map +1 -1
- package/dist/builtins.js +114 -1
- package/dist/builtins.js.map +1 -1
- package/dist/interpreter.d.ts +6 -1
- package/dist/interpreter.d.ts.map +1 -1
- package/dist/interpreter.js +210 -36
- package/dist/interpreter.js.map +1 -1
- package/dist/parser.d.ts.map +1 -1
- package/dist/parser.js +2 -0
- package/dist/parser.js.map +1 -1
- package/dist/sesi.bundled.js +55012 -0
- package/dist/types.d.ts +9 -1
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js.map +1 -1
- package/docs/ARCHITECTURE.md +9 -9
- package/docs/BUILTINS.md +87 -8
- package/docs/DISTRIBUTED_SYSTEMS.md +1 -1
- package/docs/IMAGE_GENERATION.md +82 -1
- package/docs/IMPLEMENTATION_SUMMARY.md +544 -533
- package/docs/QUICKSTART.md +52 -33
- package/docs/README.md +21 -39
- package/docs/ROADMAP.md +10 -11
- package/docs/SPECIFICATION.md +37 -14
- package/docs/SYSTEMS_REASONING.md +35 -11
- package/docs/bakery_logo.png +0 -0
- package/docs/coffee_mug.png +0 -0
- package/docs/desk_lamp.png +0 -0
- package/docs/logo.png +0 -0
- package/docs/notebook.png +0 -0
- package/docs/sesi_ai_chronicles.md +209 -0
- package/examples/16_modules.sesi +28 -0
- package/examples/17_http_client.sesi +29 -0
- package/examples/18_parallel_requests.sesi +37 -0
- package/main/chatbot.sesi +36 -0
- package/main/conversational_classifier_weights.json +45 -0
- package/main/conversational_sentences.json +304 -0
- package/main/epochs.sesi +94 -0
- package/main/gpu_orchestrator.sesi +36 -0
- package/main/hardware_diagnostics.sesi +118 -0
- package/main/inference.sesi +54 -0
- package/main/native_chatbot.sesi +180 -0
- package/main/native_synthesizer.sesi +83 -0
- package/main/nn_personas_trainer.sesi +302 -0
- package/main/nn_responses_trainer.sesi +269 -0
- package/main/nn_sentences_trainer.sesi +330 -0
- package/main/personas.json +124 -0
- package/main/personas_classifier_weights.json +45 -0
- package/main/playground.sesi +3 -1
- package/main/predictive_typing.sesi +127 -0
- package/main/query_brain.sesi +45 -0
- package/main/response_classifier_weights.json +45 -0
- package/main/retro_chat.html +239 -0
- package/main/retro_chat_generator.sesi +745 -0
- package/main/sesi_ai.sesi +158 -0
- package/main/sesi_db_chatbot.sesi +261 -0
- package/main/terminal_chat.py +385 -0
- package/main/tests/temp_math_mod.sesi +3 -0
- package/main/tests/test_image_input.sesi +40 -0
- package/main/tests/test_v2_features.sesi +48 -0
- package/main/unified_sesi_ai.sesi +334 -0
- package/main/varied_responses.json +304 -0
- package/package.json +12 -5
- package/main/atm_deposit.sesi +0 -37
- package/main/atm_withdraw.sesi +0 -37
- package/main/data.txt +0 -1
- package/main/math_aggregator.sesi +0 -15
- package/main/math_generator.sesi +0 -7
- package/main/math_processor.sesi +0 -23
- package/main/tax_calculator.sesi +0 -15
- package/main/vault.sesi +0 -15
|
@@ -0,0 +1,269 @@
|
|
|
1
|
+
// Sesi Script: nn_responses_trainer.sesi
|
|
2
|
+
// 🧠 NATIVE DEEP LEARNING RESPONSE CLASSIFIER TRAINER
|
|
3
|
+
// Compiles Bag-of-Words feature vectors from main/varied_responses.json,
|
|
4
|
+
// trains a 10x3 dynamic neural synapse matrix over 1500 gradient descent epochs,
|
|
5
|
+
// saves weights to disk, and tests custom prediction sentences!
|
|
6
|
+
|
|
7
|
+
print "=================================================="
|
|
8
|
+
print "🧠 INITIATING Sesi DEEP LEARNING RESPONSE TRAINER"
|
|
9
|
+
print "=================================================="
|
|
10
|
+
|
|
11
|
+
// Custom integer floor function
|
|
12
|
+
fn floor(x) {
|
|
13
|
+
let s = str(x)
|
|
14
|
+
let parts = split(s, ".")
|
|
15
|
+
return num(parts[0])
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
// Fast Sigmoid (Softsign) activation
|
|
19
|
+
fn activate(x) {
|
|
20
|
+
let absX = x
|
|
21
|
+
if x < 0.0 {
|
|
22
|
+
absX = 0.0 - x
|
|
23
|
+
}
|
|
24
|
+
return 0.5 + (0.5 * (x / (1.0 + absX)))
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
fn activate_derivative(y) {
|
|
28
|
+
return y * (1.0 - y)
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// 1. Define 10-keyword Vocabulary for Bag-of-Words vectors
|
|
32
|
+
let vocab = ["synthesizer", "vinyl", "record", "wood", "gears", "clock", "compiler", "framework", "memory", "offline"]
|
|
33
|
+
print "📚 Vocabulary Features Index: " + str(vocab)
|
|
34
|
+
|
|
35
|
+
// 2. Load 300 responses
|
|
36
|
+
print "📂 Loading main/varied_responses.json dataset..."
|
|
37
|
+
let raw_responses = read_file("main/varied_responses.json")
|
|
38
|
+
let parsed = from_json(raw_responses)
|
|
39
|
+
let responses = parsed["responses"]
|
|
40
|
+
let num_responses = len(responses)
|
|
41
|
+
print "📊 Ingested " + str(num_responses) + " sentences."
|
|
42
|
+
|
|
43
|
+
// 3. Compile inputs and targets natively in Sesi
|
|
44
|
+
print "🧹 Processing text sentences into Bag-of-Words numerical vectors..."
|
|
45
|
+
let inputs = []
|
|
46
|
+
let targets = []
|
|
47
|
+
|
|
48
|
+
let s_idx = 0
|
|
49
|
+
while s_idx < num_responses {
|
|
50
|
+
let sentence = responses[s_idx]
|
|
51
|
+
let words = split(sentence, " ")
|
|
52
|
+
let w_len = len(words)
|
|
53
|
+
|
|
54
|
+
// 10-dimensional input vector
|
|
55
|
+
let x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
|
|
56
|
+
|
|
57
|
+
let v_idx = 0
|
|
58
|
+
while v_idx < 10 {
|
|
59
|
+
let keyword = vocab[v_idx]
|
|
60
|
+
let found = 0.0
|
|
61
|
+
|
|
62
|
+
let w = 0
|
|
63
|
+
while w < w_len {
|
|
64
|
+
let raw_word = words[w]
|
|
65
|
+
let word = raw_word
|
|
66
|
+
let w_char_len = len(word)
|
|
67
|
+
|
|
68
|
+
if w_char_len > 1 {
|
|
69
|
+
let last_char = word[w_char_len - 1]
|
|
70
|
+
if last_char == "." { word = split(word, ".")[0] }
|
|
71
|
+
if last_char == "!" { word = split(word, "!")[0] }
|
|
72
|
+
if last_char == "?" { word = split(word, "?")[0] }
|
|
73
|
+
if last_char == "," { word = split(word, ",")[0] }
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if word == keyword {
|
|
77
|
+
found = 1.0
|
|
78
|
+
}
|
|
79
|
+
w = w + 1
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
x[v_idx] = found
|
|
83
|
+
v_idx = v_idx + 1
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
push(inputs, x)
|
|
87
|
+
|
|
88
|
+
// One-Hot Target Vector [Audio, Mechanical, Systems]
|
|
89
|
+
let t = [0.0, 0.0, 0.0]
|
|
90
|
+
if s_idx < 100 {
|
|
91
|
+
t[0] = 1.0 // Audio Class
|
|
92
|
+
}
|
|
93
|
+
if s_idx >= 100 {
|
|
94
|
+
if s_idx < 200 {
|
|
95
|
+
t[1] = 1.0 // Mechanical Class
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if s_idx >= 200 {
|
|
99
|
+
t[2] = 1.0 // Systems Class
|
|
100
|
+
}
|
|
101
|
+
push(targets, t)
|
|
102
|
+
|
|
103
|
+
s_idx = s_idx + 1
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
print "✅ Dataset vectorization complete!"
|
|
107
|
+
print "--------------------------------------------------"
|
|
108
|
+
|
|
109
|
+
// 4. Initialize 10x3 Synapse Matrix
|
|
110
|
+
print "🎲 Initializing 10x3 random neural synapses..."
|
|
111
|
+
let weights = []
|
|
112
|
+
let biases = []
|
|
113
|
+
|
|
114
|
+
let c = 0
|
|
115
|
+
while c < 3 {
|
|
116
|
+
let w_row = []
|
|
117
|
+
let k = 0
|
|
118
|
+
while k < 10 {
|
|
119
|
+
let w_val = random() - 0.5
|
|
120
|
+
push(w_row, w_val)
|
|
121
|
+
k = k + 1
|
|
122
|
+
}
|
|
123
|
+
push(weights, w_row)
|
|
124
|
+
push(biases, 0.0)
|
|
125
|
+
c = c + 1
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// 5. Backpropagation Epoch Training Loop
|
|
129
|
+
let lr = 0.3
|
|
130
|
+
let max_epochs = 1500
|
|
131
|
+
print "🚀 Training synapses across " + str(max_epochs) + " epochs (lr = " + str(lr) + ")..."
|
|
132
|
+
|
|
133
|
+
let epoch = 1
|
|
134
|
+
while epoch <= max_epochs {
|
|
135
|
+
let total_loss = 0.0
|
|
136
|
+
let sample_idx = 0
|
|
137
|
+
|
|
138
|
+
while sample_idx < num_responses {
|
|
139
|
+
let x = inputs[sample_idx]
|
|
140
|
+
let t = targets[sample_idx]
|
|
141
|
+
|
|
142
|
+
// Outputs container
|
|
143
|
+
let outputs = [0.0, 0.0, 0.0]
|
|
144
|
+
let deltas = [0.0, 0.0, 0.0]
|
|
145
|
+
|
|
146
|
+
// Forward Pass
|
|
147
|
+
let c_idx = 0
|
|
148
|
+
while c_idx < 3 {
|
|
149
|
+
let sum = 0.0
|
|
150
|
+
let w_row = weights[c_idx]
|
|
151
|
+
let k = 0
|
|
152
|
+
while k < 10 {
|
|
153
|
+
sum = sum + (x[k] * w_row[k])
|
|
154
|
+
k = k + 1
|
|
155
|
+
}
|
|
156
|
+
let o = activate(sum + biases[c_idx])
|
|
157
|
+
outputs[c_idx] = o
|
|
158
|
+
|
|
159
|
+
// Calculate Loss and Delta
|
|
160
|
+
let err = t[c_idx] - o
|
|
161
|
+
total_loss = total_loss + (err * err)
|
|
162
|
+
deltas[c_idx] = err * activate_derivative(o)
|
|
163
|
+
|
|
164
|
+
c_idx = c_idx + 1
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
// Backpropagation Gradient Weight Updates
|
|
168
|
+
c_idx = 0
|
|
169
|
+
while c_idx < 3 {
|
|
170
|
+
let w_row = weights[c_idx]
|
|
171
|
+
let delta = deltas[c_idx]
|
|
172
|
+
let k = 0
|
|
173
|
+
while k < 10 {
|
|
174
|
+
w_row[k] = w_row[k] + (lr * delta * x[k])
|
|
175
|
+
k = k + 1
|
|
176
|
+
}
|
|
177
|
+
biases[c_idx] = biases[c_idx] + (lr * delta)
|
|
178
|
+
c_idx = c_idx + 1
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
sample_idx = sample_idx + 1
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
let avg_loss = total_loss / (num_responses * 3.0)
|
|
185
|
+
|
|
186
|
+
if (epoch % 300) == 0 {
|
|
187
|
+
print "Epoch " + str(epoch) + " - Average Class Loss: " + str(avg_loss)
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
epoch = epoch + 1
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
print "--------------------------------------------------"
|
|
194
|
+
print "🎉 Synapse Epoch Training Complete!"
|
|
195
|
+
print "--------------------------------------------------"
|
|
196
|
+
|
|
197
|
+
// 6. Save calibrated synapses to JSON
|
|
198
|
+
let saved_weights = {}
|
|
199
|
+
saved_weights["weights"] = weights
|
|
200
|
+
saved_weights["biases"] = biases
|
|
201
|
+
write_file("main/response_classifier_weights.json", to_json(saved_weights))
|
|
202
|
+
print "💾 Saved trained dynamic synapses to main/response_classifier_weights.json"
|
|
203
|
+
print "--------------------------------------------------"
|
|
204
|
+
|
|
205
|
+
// 7. Test calibrated network predictions
|
|
206
|
+
print "🔮 Testing Tuned Predictions on new inputs:"
|
|
207
|
+
|
|
208
|
+
// Helper to run offline forward pass inference
|
|
209
|
+
fn predict(test_sentence) {
|
|
210
|
+
let words = split(test_sentence, " ")
|
|
211
|
+
let w_len = len(words)
|
|
212
|
+
let x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
|
|
213
|
+
|
|
214
|
+
let v_idx = 0
|
|
215
|
+
while v_idx < 10 {
|
|
216
|
+
let keyword = vocab[v_idx]
|
|
217
|
+
let found = 0.0
|
|
218
|
+
let w = 0
|
|
219
|
+
while w < w_len {
|
|
220
|
+
let raw_word = words[w]
|
|
221
|
+
let word = raw_word
|
|
222
|
+
let w_char_len = len(word)
|
|
223
|
+
if w_char_len > 1 {
|
|
224
|
+
let last_char = word[w_char_len - 1]
|
|
225
|
+
if last_char == "." { word = split(word, ".")[0] }
|
|
226
|
+
if last_char == "!" { word = split(word, "!")[0] }
|
|
227
|
+
}
|
|
228
|
+
if word == keyword {
|
|
229
|
+
found = 1.0
|
|
230
|
+
}
|
|
231
|
+
w = w + 1
|
|
232
|
+
}
|
|
233
|
+
x[v_idx] = found
|
|
234
|
+
v_idx = v_idx + 1
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
// Forward pass through trained weights
|
|
238
|
+
let predictions = [0.0, 0.0, 0.0]
|
|
239
|
+
let c_idx = 0
|
|
240
|
+
while c_idx < 3 {
|
|
241
|
+
let sum = 0.0
|
|
242
|
+
let w_row = weights[c_idx]
|
|
243
|
+
let k = 0
|
|
244
|
+
while k < 10 {
|
|
245
|
+
sum = sum + (x[k] * w_row[k])
|
|
246
|
+
k = k + 1
|
|
247
|
+
}
|
|
248
|
+
predictions[c_idx] = activate(sum + biases[c_idx])
|
|
249
|
+
c_idx = c_idx + 1
|
|
250
|
+
}
|
|
251
|
+
return predictions
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
let test_1 = "My analog modular synthesizer uses voltage filters."
|
|
255
|
+
let pred_1 = predict(test_1)
|
|
256
|
+
print "Input 1: \"" + test_1 + "\""
|
|
257
|
+
print " ➜ Predictions: [Audio: " + str(pred_1[0]) + ", Mechanical: " + str(pred_1[1]) + ", Systems: " + str(pred_1[2]) + "]"
|
|
258
|
+
|
|
259
|
+
let test_2 = "The wooden clock gears align beautifully."
|
|
260
|
+
let pred_2 = predict(test_2)
|
|
261
|
+
print "Input 2: \"" + test_2 + "\""
|
|
262
|
+
print " ➜ Predictions: [Audio: " + str(pred_2[0]) + ", Mechanical: " + str(pred_2[1]) + ", Systems: " + str(pred_2[2]) + "]"
|
|
263
|
+
|
|
264
|
+
let test_3 = "Our native Sesi compiler is optimized completely offline."
|
|
265
|
+
let pred_3 = predict(test_3)
|
|
266
|
+
print "Input 3: \"" + test_3 + "\""
|
|
267
|
+
print " ➜ Predictions: [Audio: " + str(pred_3[0]) + ", Mechanical: " + str(pred_3[1]) + ", Systems: " + str(pred_3[2]) + "]"
|
|
268
|
+
|
|
269
|
+
print "=================================================="
|
|
@@ -0,0 +1,330 @@
|
|
|
1
|
+
// Sesi Script: nn_sentences_trainer.sesi
|
|
2
|
+
// 🧠 NATIVE NEURAL NETWORK SENTENCE CLASSIFIER (TRAINED ON PERSONAS)
|
|
3
|
+
// Trains a 10x3 synapse network on the 20 personas inside main/personas.json,
|
|
4
|
+
// then zero-shot classifies all 300 conversational sentences in main/conversational_sentences.json!
|
|
5
|
+
|
|
6
|
+
print "=================================================="
|
|
7
|
+
print "🧠 INITIATING Sesi NEURAL DIALOGUE CLASSIFIER"
|
|
8
|
+
print "=================================================="
|
|
9
|
+
|
|
10
|
+
// Custom integer floor function
|
|
11
|
+
fn floor(x) {
|
|
12
|
+
let s = str(x)
|
|
13
|
+
let parts = split(s, ".")
|
|
14
|
+
return num(parts[0])
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
// Fast Sigmoid (Softsign) activation
|
|
18
|
+
fn activate(x) {
|
|
19
|
+
let absX = x
|
|
20
|
+
if x < 0.0 {
|
|
21
|
+
absX = 0.0 - x
|
|
22
|
+
}
|
|
23
|
+
return 0.5 + (0.5 * (x / (1.0 + absX)))
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
fn activate_derivative(y) {
|
|
27
|
+
return y * (1.0 - y)
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// 1. Define 10-keyword Vocabulary for Bag-of-Words vectors
|
|
31
|
+
let vocab = ["synthesizer", "vinyl", "record", "wood", "gears", "clock", "compiler", "framework", "memory", "offline"]
|
|
32
|
+
print "📚 Vocabulary Features: " + str(vocab)
|
|
33
|
+
|
|
34
|
+
// 2. Load 20 Personas Database
|
|
35
|
+
print "📂 Loading main/personas.json dataset..."
|
|
36
|
+
let raw_personas = read_file("main/personas.json")
|
|
37
|
+
let parsed_personas = from_json(raw_personas)
|
|
38
|
+
let personas = parsed_personas["personas"]
|
|
39
|
+
let num_personas = len(personas)
|
|
40
|
+
print "👥 Loaded " + str(num_personas) + " personas."
|
|
41
|
+
|
|
42
|
+
// Helper to determine persona category class natively
|
|
43
|
+
fn get_persona_category(persona) {
|
|
44
|
+
let p = persona["personality"]
|
|
45
|
+
let t = persona["tone"]
|
|
46
|
+
let combined = p + " " + t
|
|
47
|
+
let words = split(combined, " ")
|
|
48
|
+
let w_len = len(words)
|
|
49
|
+
|
|
50
|
+
let w = 0
|
|
51
|
+
while w < w_len {
|
|
52
|
+
let word = words[w]
|
|
53
|
+
if word == "synthesizer" { return "audio" }
|
|
54
|
+
if word == "vinyl" { return "audio" }
|
|
55
|
+
if word == "record" { return "audio" }
|
|
56
|
+
if word == "jazz" { return "audio" }
|
|
57
|
+
if word == "beatmaker" { return "audio" }
|
|
58
|
+
if word == "music" { return "audio" }
|
|
59
|
+
if word == "audio" { return "audio" }
|
|
60
|
+
if word == "chiptune" { return "audio" }
|
|
61
|
+
|
|
62
|
+
if word == "wood" { return "mechanical" }
|
|
63
|
+
if word == "furniture" { return "mechanical" }
|
|
64
|
+
if word == "watch" { return "mechanical" }
|
|
65
|
+
if word == "clock" { return "mechanical" }
|
|
66
|
+
if word == "gears" { return "mechanical" }
|
|
67
|
+
if word == "Game" { return "mechanical" }
|
|
68
|
+
if word == "Boy" { return "mechanical" }
|
|
69
|
+
if word == "botanist" { return "mechanical" }
|
|
70
|
+
if word == "architect" { return "mechanical" }
|
|
71
|
+
w = w + 1
|
|
72
|
+
}
|
|
73
|
+
return "systems"
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// 3. Compile inputs and targets from Personas
|
|
77
|
+
print "🧹 Processing persona bio descriptions into training matrices..."
|
|
78
|
+
let inputs = []
|
|
79
|
+
let targets = []
|
|
80
|
+
|
|
81
|
+
let p_idx = 0
|
|
82
|
+
while p_idx < num_personas {
|
|
83
|
+
let p = personas[p_idx]
|
|
84
|
+
let bio = p["personality"] + " " + p["tone"]
|
|
85
|
+
let words = split(bio, " ")
|
|
86
|
+
let w_len = len(words)
|
|
87
|
+
|
|
88
|
+
// 10-dimensional input vector
|
|
89
|
+
let x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
|
|
90
|
+
|
|
91
|
+
let v_idx = 0
|
|
92
|
+
while v_idx < 10 {
|
|
93
|
+
let keyword = vocab[v_idx]
|
|
94
|
+
let found = 0.0
|
|
95
|
+
let w = 0
|
|
96
|
+
while w < w_len {
|
|
97
|
+
let raw_word = words[w]
|
|
98
|
+
let word = raw_word
|
|
99
|
+
let w_char_len = len(word)
|
|
100
|
+
if w_char_len > 1 {
|
|
101
|
+
let last_char = word[w_char_len - 1]
|
|
102
|
+
if last_char == "." { word = split(word, ".")[0] }
|
|
103
|
+
if last_char == "," { word = split(word, ",")[0] }
|
|
104
|
+
}
|
|
105
|
+
if word == keyword {
|
|
106
|
+
found = 1.0
|
|
107
|
+
}
|
|
108
|
+
w = w + 1
|
|
109
|
+
}
|
|
110
|
+
x[v_idx] = found
|
|
111
|
+
v_idx = v_idx + 1
|
|
112
|
+
}
|
|
113
|
+
push(inputs, x)
|
|
114
|
+
|
|
115
|
+
// One-Hot Target Class mapping [Audio, Mechanical, Systems]
|
|
116
|
+
let cat = get_persona_category(p)
|
|
117
|
+
let t = [0.0, 0.0, 0.0]
|
|
118
|
+
if cat == "audio" { t[0] = 1.0 }
|
|
119
|
+
if cat == "mechanical" { t[1] = 1.0 }
|
|
120
|
+
if cat == "systems" { t[2] = 1.0 }
|
|
121
|
+
push(targets, t)
|
|
122
|
+
|
|
123
|
+
p_idx = p_idx + 1
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
print "✅ Persona vectorization complete!"
|
|
127
|
+
print "--------------------------------------------------"
|
|
128
|
+
|
|
129
|
+
// 4. Initialize 10x3 Synapse Matrix
|
|
130
|
+
print "🎲 Initializing 10x3 random neural synapses..."
|
|
131
|
+
let weights = []
|
|
132
|
+
let biases = []
|
|
133
|
+
|
|
134
|
+
let c = 0
|
|
135
|
+
while c < 3 {
|
|
136
|
+
let w_row = []
|
|
137
|
+
let k = 0
|
|
138
|
+
while k < 10 {
|
|
139
|
+
let w_val = random() - 0.5
|
|
140
|
+
push(w_row, w_val)
|
|
141
|
+
k = k + 1
|
|
142
|
+
}
|
|
143
|
+
push(weights, w_row)
|
|
144
|
+
push(biases, 0.0)
|
|
145
|
+
c = c + 1
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
// 5. Backpropagation Epoch Training Loop (2500 epochs for high convergence on 20 samples)
|
|
149
|
+
let lr = 0.3
|
|
150
|
+
let max_epochs = 2500
|
|
151
|
+
print "🚀 Training synapses across " + str(max_epochs) + " epochs (lr = " + str(lr) + ")..."
|
|
152
|
+
|
|
153
|
+
let epoch = 1
|
|
154
|
+
while epoch <= max_epochs {
|
|
155
|
+
let total_loss = 0.0
|
|
156
|
+
let sample_idx = 0
|
|
157
|
+
|
|
158
|
+
while sample_idx < num_personas {
|
|
159
|
+
let x = inputs[sample_idx]
|
|
160
|
+
let t = targets[sample_idx]
|
|
161
|
+
|
|
162
|
+
// Outputs container
|
|
163
|
+
let outputs = [0.0, 0.0, 0.0]
|
|
164
|
+
let deltas = [0.0, 0.0, 0.0]
|
|
165
|
+
|
|
166
|
+
// Forward Pass
|
|
167
|
+
let c_idx = 0
|
|
168
|
+
while c_idx < 3 {
|
|
169
|
+
let sum = 0.0
|
|
170
|
+
let w_row = weights[c_idx]
|
|
171
|
+
let k = 0
|
|
172
|
+
while k < 10 {
|
|
173
|
+
sum = sum + (x[k] * w_row[k])
|
|
174
|
+
k = k + 1
|
|
175
|
+
}
|
|
176
|
+
let o = activate(sum + biases[c_idx])
|
|
177
|
+
outputs[c_idx] = o
|
|
178
|
+
|
|
179
|
+
// Calculate Loss and Delta
|
|
180
|
+
let err = t[c_idx] - o
|
|
181
|
+
total_loss = total_loss + (err * err)
|
|
182
|
+
deltas[c_idx] = err * activate_derivative(o)
|
|
183
|
+
|
|
184
|
+
c_idx = c_idx + 1
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// Backpropagation Gradient Weight Updates
|
|
188
|
+
c_idx = 0
|
|
189
|
+
while c_idx < 3 {
|
|
190
|
+
let w_row = weights[c_idx]
|
|
191
|
+
let delta = deltas[c_idx]
|
|
192
|
+
let k = 0
|
|
193
|
+
while k < 10 {
|
|
194
|
+
w_row[k] = w_row[k] + (lr * delta * x[k])
|
|
195
|
+
k = k + 1
|
|
196
|
+
}
|
|
197
|
+
biases[c_idx] = biases[c_idx] + (lr * delta)
|
|
198
|
+
c_idx = c_idx + 1
|
|
199
|
+
}
|
|
200
|
+
sample_idx = sample_idx + 1
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
let avg_loss = total_loss / (num_personas * 3.0)
|
|
204
|
+
|
|
205
|
+
if (epoch % 500) == 0 {
|
|
206
|
+
print "Epoch " + str(epoch) + " - Average Class Loss: " + str(avg_loss)
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
epoch = epoch + 1
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
print "--------------------------------------------------"
|
|
213
|
+
print "🎉 Synapse Epoch Training Complete!"
|
|
214
|
+
print "--------------------------------------------------"
|
|
215
|
+
|
|
216
|
+
// 6. Save calibrated synapses to JSON
|
|
217
|
+
let saved_weights = {}
|
|
218
|
+
saved_weights["weights"] = weights
|
|
219
|
+
saved_weights["biases"] = biases
|
|
220
|
+
write_file("main/personas_classifier_weights.json", to_json(saved_weights))
|
|
221
|
+
print "💾 Saved trained synapses to main/personas_classifier_weights.json"
|
|
222
|
+
print "--------------------------------------------------"
|
|
223
|
+
|
|
224
|
+
// Helper to run forward pass inference on a text sentence
|
|
225
|
+
fn predict(sentence_text) {
|
|
226
|
+
let words = split(sentence_text, " ")
|
|
227
|
+
let w_len = len(words)
|
|
228
|
+
let x = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
|
|
229
|
+
|
|
230
|
+
let v_idx = 0
|
|
231
|
+
while v_idx < 10 {
|
|
232
|
+
let keyword = vocab[v_idx]
|
|
233
|
+
let found = 0.0
|
|
234
|
+
let w = 0
|
|
235
|
+
while w < w_len {
|
|
236
|
+
let raw_word = words[w]
|
|
237
|
+
let word = raw_word
|
|
238
|
+
let w_char_len = len(word)
|
|
239
|
+
if w_char_len > 1 {
|
|
240
|
+
let last_char = word[w_char_len - 1]
|
|
241
|
+
if last_char == "." { word = split(word, ".")[0] }
|
|
242
|
+
if last_char == "!" { word = split(word, "!")[0] }
|
|
243
|
+
if last_char == "?" { word = split(word, "?")[0] }
|
|
244
|
+
if last_char == "," { word = split(word, ",")[0] }
|
|
245
|
+
}
|
|
246
|
+
if word == keyword {
|
|
247
|
+
found = 1.0
|
|
248
|
+
}
|
|
249
|
+
w = w + 1
|
|
250
|
+
}
|
|
251
|
+
x[v_idx] = found
|
|
252
|
+
v_idx = v_idx + 1
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Forward pass
|
|
256
|
+
let predictions = [0.0, 0.0, 0.0]
|
|
257
|
+
let c_idx = 0
|
|
258
|
+
while c_idx < 3 {
|
|
259
|
+
let sum = 0.0
|
|
260
|
+
let w_row = weights[c_idx]
|
|
261
|
+
let k = 0
|
|
262
|
+
while k < 10 {
|
|
263
|
+
sum = sum + (x[k] * w_row[k])
|
|
264
|
+
k = k + 1
|
|
265
|
+
}
|
|
266
|
+
predictions[c_idx] = activate(sum + biases[c_idx])
|
|
267
|
+
c_idx = c_idx + 1
|
|
268
|
+
}
|
|
269
|
+
return predictions
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
// 7. Load Conversational Sentences and zero-shot evaluate all 300!
|
|
273
|
+
print "📂 Loading main/conversational_sentences.json dataset for sentence classification..."
|
|
274
|
+
let raw_conversations = read_file("main/conversational_sentences.json")
|
|
275
|
+
let parsed_conv = from_json(raw_conversations)
|
|
276
|
+
let sentences = parsed_conv["sentences"]
|
|
277
|
+
let num_sentences = len(sentences)
|
|
278
|
+
|
|
279
|
+
print "🔮 ZERO-SHOT EVALUATING 300 CONVERSATIONAL SENTENCES:"
|
|
280
|
+
print "--------------------------------------------------"
|
|
281
|
+
|
|
282
|
+
let audio_count = 0
|
|
283
|
+
let mech_count = 0
|
|
284
|
+
let sys_count = 0
|
|
285
|
+
|
|
286
|
+
let s_idx = 0
|
|
287
|
+
while s_idx < num_sentences {
|
|
288
|
+
let sentence = sentences[s_idx]
|
|
289
|
+
let pred = predict(sentence)
|
|
290
|
+
|
|
291
|
+
let max_val = pred[0]
|
|
292
|
+
let pred_class = "AUDIO"
|
|
293
|
+
if pred[1] > max_val {
|
|
294
|
+
max_val = pred[1]
|
|
295
|
+
pred_class = "MECHANICAL"
|
|
296
|
+
}
|
|
297
|
+
if pred[2] > max_val {
|
|
298
|
+
max_val = pred[2]
|
|
299
|
+
pred_class = "SYSTEMS"
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
if pred_class == "AUDIO" { audio_count = audio_count + 1 }
|
|
303
|
+
if pred_class == "MECHANICAL" { mech_count = mech_count + 1 }
|
|
304
|
+
if pred_class == "SYSTEMS" { sys_count = sys_count + 1 }
|
|
305
|
+
|
|
306
|
+
// Log first 6 sentences of each 100-sentence slice as representation samples
|
|
307
|
+
let log_item = false
|
|
308
|
+
if s_idx < 6 { log_item = true }
|
|
309
|
+
if s_idx >= 100 {
|
|
310
|
+
if s_idx < 106 { log_item = true }
|
|
311
|
+
}
|
|
312
|
+
if s_idx >= 200 {
|
|
313
|
+
if s_idx < 206 { log_item = true }
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
if log_item {
|
|
317
|
+
print "[" + str(s_idx) + "] \"" + sentence + "\""
|
|
318
|
+
print " ➜ Classified: " + pred_class + " [Audio: " + str(pred[0]) + ", Mech: " + str(pred[1]) + ", Sys: " + str(pred[2]) + "]"
|
|
319
|
+
print ""
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
s_idx = s_idx + 1
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
print "--------------------------------------------------"
|
|
326
|
+
print "📊 FINAL SENTENCE CLASS DISTRIBUTION STATS:"
|
|
327
|
+
print "🎧 AUDIO SENTENCES: " + str(audio_count)
|
|
328
|
+
print "⚙️ MECHANICAL SENTENCES: " + str(mech_count)
|
|
329
|
+
print "🖥️ SYSTEMS/GEN SENTENCES: " + str(sys_count)
|
|
330
|
+
print "=================================================="
|