-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
268 lines (245 loc) · 9.29 KB
/
main.py
File metadata and controls
268 lines (245 loc) · 9.29 KB
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
from jsonargparse import ArgumentParser, ActionConfigFile
from tqdm import tqdm
import os
import pickle
import numpy as np
import wandb
from env import ClusteringEnv, Hypothesis, Point
from agents import TeacherAgent, StudentAgent
from utils import create_result_path, set_random_seed, generate_hypotheses
def main(args):
set_random_seed(args.seed) # Set a random seed for reproducibility
# Initialize buffer to store results
result_buffer = {
"configs": vars(args),
"hypotheses": [],
"true_hypothesis_index": -1,
"data": [],
"student_beliefs": [],
"student_actions": [None],
"student_true_hypothesis_probs": [None],
"student_true_hypothesis_ranks": [None],
"teacher_beliefs": [],
"teacher_actions": [None],
}
result_file = create_result_path(args)
# Define hypotheses
if args.mock_test:
hypothesis1 = Hypothesis(
centroids=[Point([0, 0]), Point([5, 5])], radiuses=[1.0, 1.0]
)
hypothesis2 = Hypothesis(
centroids=[Point([0, 5]), Point([5, 0])], radiuses=[1.5, 1.5]
)
hypotheses = [hypothesis1, hypothesis2]
true_hypothesis = hypothesis1
true_hypothesis_index = hypotheses.index(true_hypothesis)
else:
hypotheses, true_hypothesis_index = generate_hypotheses(
n_hypotheses=args.n_hypotheses,
n_cluster=args.n_clusters,
n_features=args.n_features,
seed=42,
)
true_hypothesis = hypotheses[true_hypothesis_index]
result_buffer["hypotheses"] = [h.to_dict() for h in hypotheses]
result_buffer["true_hypothesis_index"] = true_hypothesis_index
# Initialize environment
env = ClusteringEnv(
n_features=args.n_features,
n_samples=args.n_samples,
data_initialization=args.data_initialization,
)
data = env.reset(true_hypothesis, seed=42)
data_likelihoods = env.compute_data_likelihoods(
hypotheses
) # shape (n_samples, n_clusters + 1, n_hypotheses)
result_buffer["data"] = [point.to_dict() for point in data]
# Initialize agents
teacher = TeacherAgent(
data=data,
hypotheses=hypotheses,
true_hypothesis=true_hypothesis,
strategy=args.teacher_strategy,
student_strategy=args.teacher_student_strategy_assumption,
student_mode=args.teacher_student_mode_assumption,
interaction_mode=args.interaction_mode,
env=env,
data_likelihoods=data_likelihoods,
alpha=args.teacher_alpha,
n_beliefs=args.teacher_n_beliefs,
)
result_buffer["teacher_beliefs"].append(teacher.belief.to_dict())
is_mismatched = (
args.teacher_student_mode_assumption != args.student_mode
or args.teacher_student_strategy_assumption != args.student_strategy
or args.teacher_alpha != args.student_beta
)
student = StudentAgent(
mode=args.student_mode,
beta=args.student_beta,
strategy=args.student_strategy,
teacher_strategy=args.student_teacher_strategy_assumption,
teacher_belief=teacher.belief,
interaction_mode=args.interaction_mode,
data=data,
hypotheses=hypotheses,
env=env,
data_likelihoods=data_likelihoods,
teacher_model=None if is_mismatched else teacher,
)
print(
"Student's belief of the true hypothesis:",
student.belief.probs[true_hypothesis_index],
)
result_buffer["student_beliefs"].append(student.belief.to_dict())
# Start simulation loop
for round in tqdm(range(args.n_rounds), desc="Simulation Progress"):
print(f"\n{'='*20} ROUND {round + 1} {'='*20}\n")
# Teacher selects a data point to show
print("[Teacher] Selecting data point...")
x_t, y_t = teacher.select_data_point()
print(f" → Selected Point #{data.index(x_t)}: {x_t} | Label: {y_t}\n")
# Student updates beliefs based on the shown data point
print("[Student] Updating belief...")
student.update_belief(x_t=x_t, y_t=y_t)
belief_true = student.belief.probs[true_hypothesis_index]
rank_true = (
np.argsort(-student.belief.probs).tolist().index(true_hypothesis_index) + 1
)
print(f" → Belief in True Hypothesis: {belief_true:.4f}")
print(f" → Rank of True Hypothesis: {rank_true}\n")
if args.interaction_mode in ["active_interaction", "lazy_teacher"]:
# Student makes an action (query a new data point or passive)
print("[Student] Making an action...")
a_t = student.make_action()
if a_t:
print(f" → Queried Point #{data.index(a_t)}: {a_t}\n")
else:
print(" → No action taken.\n")
# Teacher updates beliefs about the student's beliefs
print("[Teacher] Updating belief based on student's action...")
teacher.update_belief(a_t=a_t)
print(
" → Maximal teacher belief over student beliefs:",
teacher.belief.probs.max(),
"\n",
)
else:
a_t = None
print("[Interaction Mode] Lazy Student: No action taken.\n")
# Store results of this round
result_buffer["teacher_beliefs"].append(teacher.belief.to_dict())
result_buffer["teacher_actions"].append({"x": x_t.to_dict(), "y": y_t})
result_buffer["student_beliefs"].append(student.belief.to_dict())
result_buffer["student_actions"].append({"x": a_t.to_dict() if a_t else None})
result_buffer["student_true_hypothesis_probs"].append(belief_true)
result_buffer["student_true_hypothesis_ranks"].append(rank_true)
# Save intermediate results
print("[SYSTEM] Saving intermediate results...\n")
with open(result_file, "wb") as f:
pickle.dump(result_buffer, f)
wandb.log(
{
"true_belief_prob": belief_true,
"true_belief_rank": rank_true,
"round": round + 1,
}
)
print("-" * 60)
if __name__ == "__main__":
wandb.init(project="teachsim")
parser = ArgumentParser(description="Teaching Simulation")
parser.add_argument("--config", action=ActionConfigFile)
parser.add_argument("--seed", type=int, default=42, help="Random seed")
parser.add_argument(
"--mock_test",
action="store_true",
help="Run a mock test with predefined parameters",
)
parser.add_argument(
"--n_hypotheses", type=int, default=2, help="Number of hypotheses"
)
parser.add_argument(
"--n_clusters", type=int, default=2, help="Number of clusters per hypothesis"
)
parser.add_argument("--n_features", type=int, default=2, help="Number of features")
parser.add_argument("--n_samples", type=int, default=100, help="Number of samples")
parser.add_argument(
"--n_rounds", type=int, default=100, help="Number of simulation rounds"
)
parser.add_argument(
"--data_initialization",
type=str,
default="normal",
choices=["uniform", "normal"],
help="Data initialization method",
)
parser.add_argument(
"--interaction_mode",
type=str,
default="lazy_student",
choices=["active_interaction", "lazy_student", "lazy_teacher"],
help="Interaction mode between teacher and student",
)
parser.add_argument(
"--teacher_strategy",
type=str,
default="hypothesis",
choices=["random", "hypothesis"],
help="Teacher strategy to select data points",
)
parser.add_argument(
"--teacher_alpha", type=float, default=1.0, help="Teacher alpha parameter"
)
parser.add_argument(
"--teacher_n_beliefs", type=int, default=100, help="Number of teacher beliefs"
)
parser.add_argument(
"--teacher_student_strategy_assumption",
type=str,
default="",
choices=["", "random", "uncertainty", "hypothesis"],
help="Teacher assumption about student strategy for querying data points",
)
parser.add_argument(
"--teacher_student_mode_assumption",
type=str,
default="naive",
choices=["rational", "naive"],
help="Teacher assumption about student mode",
)
parser.add_argument(
"--student_mode",
type=str,
default="naive",
choices=["rational", "naive"],
help="Student mode",
)
parser.add_argument(
"--student_strategy",
type=str,
default="",
choices=["", "random", "uncertainty", "hypothesis"],
help="Student strategy for querying data points",
)
parser.add_argument(
"--student_beta", type=float, default=1.0, help="Student beta parameter"
)
parser.add_argument(
"--student_teacher_strategy_assumption",
type=str,
default="",
choices=["", "random", "hypothesis"],
help="Student assumption about teacher strategy to select data points",
)
parser.add_argument(
"--result_dir",
type=str,
default="results",
help="Output directory to save simulation results",
)
args = parser.parse_args()
os.makedirs(args.result_dir, exist_ok=True)
main(args)
wandb.finish()