-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurriculum_learning.py
91 lines (68 loc) · 3.41 KB
/
curriculum_learning.py
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
import numpy as np
import tensorflow as tf
from utils.data_generation import generate_dataset
from utils.evaluation import evaluate_readiness
from training.parallel_training import parallel_train_model
from config.settings import READINESS_THRESHOLDS
from visualization.real_time_plotter import RealTimePlotter
from utils.tokenization import tokenize_problem
from config.settings import MAX_LENGTH
def smooth_curriculum_learning(model, stages, initial_problems=4000, max_problems=5000, difficulty_increase_rate=0.05):
all_history = []
current_difficulty = 1.0
plotter = RealTimePlotter()
plotter.after(100, plotter.update) # Start the Tkinter loop
# Compile the model before starting the learning process
model.compile(optimizer='adam', loss='mse', metrics=['mae'])
# Asegurarse de que el modelo esté construido
if not model.built:
model.build(input_shape=(None, MAX_LENGTH))
for stage in stages:
print(f"\nEntering learning stage: {stage}")
model.set_learning_stage(stage)
problems_solved = 0
stage_history = []
while problems_solved < max_problems:
num_problems = min(initial_problems, max_problems - problems_solved)
problems = generate_dataset(num_problems, stage, current_difficulty)
# Prepare data for training
X = np.array([tokenize_problem(problem.problem, stage) for problem in problems])
y = np.array([problem.solution for problem in problems])
# Ensure X has the correct shape (batch_size, time_steps)
if len(X.shape) == 1:
X = np.expand_dims(X, axis=0)
# Reshape y if necessary
if y.ndim == 1:
y = y.reshape(-1, 1)
# Ensure X and y have the same number of samples
assert X.shape[0] == y.shape[0], f"Number of samples in X ({X.shape[0]}) and y ({y.shape[0]}) do not match"
fold_histories = parallel_train_model(model, problems, epochs=50)
# Update real-time plot with fold history loss data
for history in fold_histories:
for loss in history['history']['loss']:
plotter.update_plot(loss)
plotter.update_idletasks()
plotter.update()
model.set_weights(fold_histories[-1]['weights'])
stage_history.extend(fold_histories)
# Prepare validation data
val_problems = problems[-len(problems)//5:]
X_val = np.array([tokenize_problem(problem.problem, stage) for problem in val_problems])
y_val = np.array([problem.solution for problem in val_problems])
if y_val.ndim == 1:
y_val = y_val.reshape(-1, 1)
if evaluate_readiness(model, X_val, y_val, READINESS_THRESHOLDS[stage]):
print("Model ready to advance!")
current_difficulty += difficulty_increase_rate
break
problems_solved += num_problems
if current_difficulty > 3.0 and stage != stages[-1]:
print(f"Advancing to next stage: {stages[stages.index(stage) + 1]}")
break
all_history.append({
'stage': stage,
'fold_histories': stage_history
})
current_difficulty = max(1.0, current_difficulty - 0.5)
plotter.destroy() # Close the Tkinter window when done
return all_history