goodgoals commited on
Commit
053fc6f
·
verified ·
1 Parent(s): dfd8b3a

Upload model.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. model.py +125 -0
model.py ADDED
@@ -0,0 +1,125 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ import numpy as np
3
+ import copy
4
+ import time
5
+
6
+ class AlgebraicEquation:
7
+ def __init__(self):
8
+ self.strength = 1.0
9
+ self.usage_count = 0
10
+ self.last_used_step = 0
11
+
12
+ def fit(self, x, y):
13
+ pass
14
+
15
+ def update_parameters(self, x, y):
16
+ pass
17
+
18
+ def predict(self, x):
19
+ pass
20
+
21
+ def get_error(self, x, y):
22
+ prediction = self.predict(x)
23
+ return abs(prediction - y) if prediction is not None else float('inf')
24
+
25
+ def copy(self):
26
+ return copy.deepcopy(self)
27
+
28
+ class LinearEquation(AlgebraicEquation):
29
+ def __init__(self):
30
+ super().__init__()
31
+ self.c = 0.0
32
+ def fit(self, x, y): self.c = y - x
33
+ def update_parameters(self, x, y): self.c = y - x
34
+ def predict(self, x): return x + self.c
35
+
36
+ class MultiplicativeEquation(AlgebraicEquation):
37
+ def __init__(self):
38
+ super().__init__()
39
+ self.a = 1.0
40
+ def fit(self, x, y): self.a = y / x if x != 0 else 0.0
41
+ def update_parameters(self, x, y): self.a = y / x if x != 0 else 0.0
42
+ def predict(self, x): return self.a * x
43
+
44
+ class QuadraticEquation(AlgebraicEquation):
45
+ def __init__(self):
46
+ super().__init__()
47
+ self.c = 0.0
48
+ def fit(self, x, y): self.c = y - (x ** 2)
49
+ def update_parameters(self, x, y): self.c = y - (x ** 2)
50
+ def predict(self, x): return (x ** 2) + self.c
51
+
52
+ class NextTokenSystem:
53
+ def __init__(self, vocabulary):
54
+ self.vocabulary = vocabulary
55
+ self.token_to_score = {token: float(i + 1) for i, token in enumerate(vocabulary)}
56
+ self.score_to_token = {score: token for token, score in self.token_to_score.items()}
57
+ self.relationship_table = []
58
+ self.candidate_equations = []
59
+ self.timestep = 0
60
+
61
+ def get_score(self, token): return self.token_to_score.get(token)
62
+
63
+ def add_relationship(self, token_x, token_y):
64
+ sx, sy = self.get_score(token_x), self.get_score(token_y)
65
+ if sx is not None and sy is not None: self.relationship_table.append((sx, sy))
66
+
67
+ def generate_candidate_rules(self):
68
+ self.candidate_equations = []
69
+ for x, y in self.relationship_table:
70
+ for Cls in [LinearEquation, MultiplicativeEquation, QuadraticEquation]:
71
+ eq = Cls(); eq.fit(x, y); self.candidate_equations.append(eq)
72
+
73
+ class ConflictResolutionEngine(NextTokenSystem):
74
+ def __init__(self, vocabulary, decay_factor=0.9, reuse_bonus=0.2, inhibitory_penalty=10.0):
75
+ super().__init__(vocabulary)
76
+ self.decay_factor, self.reuse_bonus, self.inhibitory_penalty = decay_factor, reuse_bonus, inhibitory_penalty
77
+ self.current_step, self.last_selected_eq, self.local_anchors = 0, None, {}
78
+ self.reinforcement_bonus = 5.0
79
+
80
+ def calculate_dynamic_threshold(self, percentage=0.0001): return max(1.0, len(self.vocabulary) * percentage)
81
+
82
+ def register_local_anchor(self, token, equation): self.local_anchors[token] = equation.copy()
83
+
84
+ def refined_back_search(self, input_score, target_score, threshold=None):
85
+ if threshold is None: threshold = self.calculate_dynamic_threshold()
86
+ best_eq, min_diff = None, float('inf')
87
+ for eq in self.candidate_equations:
88
+ diff = abs(eq.predict(input_score) - target_score)
89
+ if diff <= threshold and diff < min_diff:
90
+ min_diff, best_eq = diff, eq
91
+ return best_eq
92
+
93
+ def select_governing_equation(self, current_token=None):
94
+ if current_token in self.local_anchors: return self.local_anchors[current_token]
95
+ self.current_step += 1
96
+ weights = []
97
+ for i, eq in enumerate(self.candidate_equations):
98
+ if eq.last_used_step < self.current_step - 1: eq.strength *= self.decay_factor
99
+ w = eq.strength + (self.relationship_table[i//3][0] + self.relationship_table[i//3][1]) / (2 * len(self.vocabulary))
100
+ weights.append(max(0, w))
101
+ best_eq = self.candidate_equations[np.argmax(weights)]
102
+ self.last_selected_eq = best_eq
103
+ return best_eq
104
+
105
+ def predict_next_token(self, current_token):
106
+ score = self.get_score(current_token)
107
+ eq = self.select_governing_equation(current_token)
108
+ target_y = eq.predict(score)
109
+ best_token = self.score_to_token[min(self.score_to_token.keys(), key=lambda k: abs(k - target_y))]
110
+ return {'predicted_token': best_token, 'governing_equation': type(eq).__name__}
111
+
112
+ def penalized_supervised_train(self, data_path, iterations=25, threshold=None):
113
+ with open(data_path, 'r') as f: examples = json.load(f)['training_examples']
114
+ if threshold is None: threshold = self.calculate_dynamic_threshold()
115
+ for i in range(iterations):
116
+ for ex in examples:
117
+ res = self.predict_next_token(ex['input'])
118
+ if res['predicted_token'] != ex['target']:
119
+ self.last_selected_eq.strength = max(0, self.last_selected_eq.strength - self.inhibitory_penalty)
120
+ best = self.refined_back_search(self.get_score(ex['input']), self.get_score(ex['target']), threshold)
121
+ if best:
122
+ opt = best.copy(); opt.update_parameters(self.get_score(ex['input']), self.get_score(ex['target']))
123
+ self.register_local_anchor(ex['input'], opt)
124
+
125
+ print('model.py has been successfully written to the current directory.')