-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaith
278 lines (219 loc) · 11.1 KB
/
Maith
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
import logging
import torch
import torch.nn as nn
import torch.optim as optim
import random
import numpy as np
import hypothetical_lora_adapter as lora # Assuming you have a Lora adapter interface
from transformers import BertForSequenceClassification, BertTokenizer, AdamW
from tensorflow.keras.models import Model
from tensorflow.keras.layers import Input, Flatten, Dense, Dropout, Lambda, Conv2D
from tensorflow.keras.optimizers import Adam
from tensorflow.keras import backend as K
from nltk.sentiment import SentimentIntensityAnalyzer
from nltk.tokenize import word_tokenize
from nltk.corpus import stopwords
from typing import List
class DQN(nn.Module):
def __init__(self, input_size, output_size):
super(DQN, self).__init__()
self.fc1 = nn.Linear(input_size, 128)
self.fc2 = nn.Linear(128, 64)
self.fc3 = nn.Linear(64, output_size)
def forward(self, x):
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
class MathLLMAgent:
EVALUATION_STRATEGY = "epoch"
def __init__(self, num_iterations: int, learning_rate: float = 0.0001, discount_factor: float = 0.9):
self.num_iterations = num_iterations
self.learning_rate = learning_rate
self.discount_factor = discount_factor
self.setup_bert_model()
self.setup_logger()
self.setup_optimizer()
self.setup_siamese_network()
self.setup_reinforcement_learning_agent()
self.setup_lora_adapter()
def setup_bert_model(self):
self.bert_model = BertForSequenceClassification.from_pretrained('bert-base-uncased', num_labels=1)
self.bert_tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
def setup_logger(self):
self.logger = logging.getLogger(__name__)
def setup_optimizer(self):
self.optimizer = AdamW(self.bert_model.parameters(), lr=self.learning_rate)
def setup_siamese_network(self):
input_shape = (105, 105, 1)
self.base_network = self.create_base_network(input_shape)
input_a = Input(shape=input_shape)
input_b = Input(shape=input_shape)
processed_a = self.base_network(input_a)
processed_b = self.base_network(input_b)
distance = Lambda(self.euclidean_distance)([processed_a, processed_b])
self.siamese_model = Model([input_a, input_b], distance)
self.siamese_model.compile(loss=self.contrastive_loss, optimizer=Adam())
def create_base_network(self, input_shape):
input = Input(shape=input_shape)
x = Conv2D(32, (5, 5), activation='relu')(input)
x = Dropout(0.1)(x)
x = Flatten()(x)
x = Dense(128, activation='relu')(x)
return Model(input, x)
def euclidean_distance(self, vects):
x, y = vects
return K.sqrt(K.maximum(K.sum(K.square(x - y), axis=1, keepdims=True), K.epsilon()))
def contrastive_loss(self, y_true, y_pred):
margin = 1
square_pred = K.square(y_pred)
margin_square = K.square(K.maximum(margin - y_pred, 0))
return K.mean(y_true * square_pred + (1 - y_true) * margin_square)
def setup_reinforcement_learning_agent(self):
self.rl_agent = ReinforcementLearningAgent(self.num_iterations, self.learning_rate, self.discount_factor)
def setup_lora_adapter(self):
self.lora_adapter = lora.Adapter()
def choose_iteration(self):
return np.random.choice(self.num_iterations)
def update_q_values(self, chosen_iteration, reward):
self.rl_agent.update_q_values(chosen_iteration, reward)
def get_sentiment_score(self, text):
return SentimentIntensityAnalyzer().polarity_scores(text)['compound']
def extract_keywords(self, feedback):
stop_words = set(stopwords.words('english'))
tokens = word_tokenize(feedback)
keywords = [word.lower() for word in tokens if word.isalnum() and word.lower() not in stop_words]
return keywords
def calculate_sentiment_reward(self, sentiment_score):
return int(np.interp(sentiment_score, [-1, 1], [1, 5]))
def calculate_keyword_reward(self, keywords):
return len(keywords)
def scale_and_combine_rewards(self, sentiment_reward, keyword_reward):
return sentiment_reward + keyword_reward
def fine_tune_bert(self, math_problems, labels):
encodings = self.bert_tokenizer(math_problems, truncation=True, padding=True, return_tensors='pt', return_token_type_ids=False)
labels = torch.tensor(labels, dtype=torch.float32).view(-1, 1)
self.bert_model.train()
for epoch in range(3): # 3 epochs for fine-tuning
outputs = self.bert_model(**encodings, labels=labels)
loss = outputs.loss
self.optimizer.zero_grad()
loss.backward()
self.optimizer.step()
self.bert_model.eval()
def train_siamese_network(self, pairs, labels):
input_shape = (105, 105, 1)
pairs_a = []
pairs_b = []
for pair in pairs:
pair_a, pair_b = self.bert_tokenizer(pair[0], pair[1], padding=True, truncation=True, max_length=128, return_tensors='pt').values()
pairs_a.append(pair_a)
pairs_b.append(pair_b)
pairs_a = torch.stack(pairs_a).reshape(-1, *input_shape)
pairs_b = torch.stack(pairs_b).reshape(-1, *input_shape)
labels = torch.tensor(labels, dtype=torch.float32).view(-1, 1)
self.siamese_model.fit([pairs_a, pairs_b], labels, epochs=5, batch_size=64, validation_split=0.1)
def generate_pairs(self, math_problems: List[str]):
num_problems = len(math_problems)
pairs = []
labels = []
for i in range(num_problems):
for j in range(i+1, num_problems):
pairs.append((math_problems[i], math_problems[j]))
if i == j-1:
labels.append(0)
else:
labels.append(1)
return pairs, labels
def get_similar_math_problem(self, math_problems: List[str], current_problem_index: int):
pairs, _ = self.generate_pairs(math_problems)
input_shape = (105, 105, 1)
pairs_a = []
pairs_b = []
for pair in pairs:
pair_a, pair_b = self.bert_tokenizer(pair[0], pair[1], padding=True, truncation=True, max_length=128, return_tensors='pt').values()
pairs_a.append(pair_a)
pairs_b.append(pair_b)
pairs_a = torch.stack(pairs_a).reshape(-1, *input_shape)
pairs_b = torch.stack(pairs_b).reshape(-1, *input_shape)
similarities = self.siamese_model.predict([pairs_a, pairs_b]).reshape(-1)
similarities[current_problem_index:] = -1 # Ignore similarities with the current problem
most_similar_index = np.argmax(similarities)
return math_problems[most_similar_index], most_similar_index
def solve_math_problems(self, math_problems: List[str], initial_problem_index: int):
current_problem_index = initial_problem_index
for i in range(self.num_iterations):
current_problem = math_problems[current_problem_index]
sentiment_score = self.get_sentiment_score(current_problem)
keywords = self.extract_keywords(current_problem)
sentiment_reward = self.calculate_sentiment_reward(sentiment_score)
keyword_reward = self.calculate_keyword_reward(keywords)
reward = self.scale_and_combine_rewards(sentiment_reward, keyword_reward)
self.update_q_values(current_problem_index, reward)
similar_problem, similar_index = self.get_similar_math_problem(math_problems, current_problem_index)
if self.rl_agent.q_values[similar_index] > self.rl_agent.q_values[current_problem_index]:
current_problem_index = similar_index
# Send solution over Lora
solution = math_problems[current_problem_index]
self.lora_adapter.send(solution)
class MathLLMAgent:
# ... (previous code)
def simulate_human_interaction(self, num_episodes):
for episode in range(num_episodes):
self.logger.info(f"Episode {episode + 1}")
current_problem_index = self.choose_iteration()
current_problem = math_problems[current_problem_index]
self.logger.info(f"Current problem: {current_problem}")
sentiment_score = self.get_sentiment_score(current_problem)
self.logger.info(f"Sentiment score: {sentiment_score}")
keywords = self.extract_keywords(current_problem)
self.logger.info(f"Keywords: {keywords}")
sentiment_reward = self.calculate_sentiment_reward(sentiment_score)
keyword_reward = self.calculate_keyword_reward(keywords)
reward = self.scale_and_combine_rewards(sentiment_reward, keyword_reward)
self.logger.info(f"Reward: {reward}")
self.update_q_values(current_problem_index, reward)
similar_problem, similar_index = self.get_similar_math_problem(math_problems, current_problem_index)
self.logger.info(f"Similar problem: {similar_problem}")
if self.rl_agent.q_values[similar_index] > self.rl_agent.q_values[current_problem_index]:
current_problem_index = similar_index
self.logger.info("Moving to a similar problem")
else:
self.logger.info("Staying with the current problem")
# Send the final solution over Lora
final_solution = math_problems[current_problem_index]
self.lora_adapter.send(final_solution)
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
num_iterations = 5
math_llm_agent = MathLLMAgent(num_iterations)
num_episodes = 10
math_problems_for_fine_tuning = ["What is 2+2?", "Solve x for 2x = 6"]
labels_for_fine_tuning = [4, 3]
math_llm_agent.fine_tune_bert(math_problems_for_fine_tuning, labels_for_fine_tuning)
math_llm_agent.simulate_human_interaction(num_episodes)
class ReinforcementLearningAgent:
def __init__(self, num_iterations: int, learning_rate: float = 0.1, discount_factor: float = 0.9):
self.num_iterations = num_iterations
self.q_values = np.zeros(num_iterations)
self.learning_rate = learning_rate
self.discount_factor = discount_factor
def choose_iteration(self):
epsilon = 0.1
if np.random.uniform(0, 1) < epsilon:
return np.random.choice(self.num_iterations)
else:
return np.argmax(self.q_values)
def update_q_values(self, chosen_iteration, reward):
old_q_value = self.q_values[chosen_iteration]
new_q_value = old_q_value + self.learning_rate * (reward + self.discount_factor * np.max(self.q_values) - old_q_value)
self.q_values[chosen_iteration] = new_q_value
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
num_iterations = 5
math_llm_agent = MathLLMAgent(num_iterations)
num_episodes = 10
math_problems_for_fine_tuning = ["What is 2+2?", "Solve x for 2x = 6"]
labels_for_fine_tuning = [4, 3]
math_llm_agent.fine_tune_bert(math_problems_for_fine_tuning, labels_for_fine_tuning)
math_llm_agent.simulate_human_interaction(num_episodes)