From af7d5963ae4c566f738416965169f457fbacd138 Mon Sep 17 00:00:00 2001 From: yujunhao <772986150@qq.com> Date: Wed, 25 Oct 2023 14:33:53 +0800 Subject: [PATCH] Modified code specification --- CAT/model/IRT.py | 41 +++++++++++++++++++++------------- CAT/strategy/BECAT_strategy.py | 8 ++----- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/CAT/model/IRT.py b/CAT/model/IRT.py index ce3dfde..7585e8a 100644 --- a/CAT/model/IRT.py +++ b/CAT/model/IRT.py @@ -284,22 +284,24 @@ def get_fisher(self, student_id, question_id, pred_all): q = 1 - pred fisher_info = (q*pred*(alpha * alpha.T)).numpy() return fisher_info - def IRT_derivate(self,pred_all): - - new_predictions = {} - for sid, qid_dict in pred_all.items(): - new_predictions[sid] = {} - for qid, pred in qid_dict.items(): - new_pred = pred * (1 - pred) - new_predictions[sid][qid] = new_pred - + def bce_loss_derivative(self,pred, target): + """ get bce_loss_derivative + Args: + pred: float, + target: int, + Returns: + the derivative of bce_loss + """ derivative = (pred - target) / (pred * (1 - pred)) return derivative + def get_BE_weights(self, pred_all): - """ + """ get BE matrix + Args: + pred_all: dict, the questions you want to sample and their probability Returns: - predictions, dict[sid][qid] + the BE matrix weights """ d = 100 Pre_true={} @@ -329,6 +331,13 @@ def get_BE_weights(self, pred_all): return w_ij_matrix def F_s_func(self,S_set,w_ij_matrix): + """ get F_s of the questions have been chosen + Args: + S_set:list , the questions have been chosen + w_ij_matrix: dict, the weight matrix + Returns: + the F_s of the chosen questions + """ res = 0.0 for w_i in w_ij_matrix: if(w_i not in S_set): @@ -336,17 +345,18 @@ def F_s_func(self,S_set,w_ij_matrix): for j in S_set: if w_ij_matrix[w_i][j] > mx: mx = w_ij_matrix[w_i][j] - res +=mx - + res +=mx return res def delta_q_S_t(self, question_id, pred_all,S_set,sampled_elements): """ get BECAT Questions weights delta Args: - student_id: int, student id question_id: int, question id + pred_all:dict, the untest questions and their probability + S_set:dict, chosen questions + sampled_elements:nparray, sampled set from untest questions Returns: - v: float, Each weight information + delta_q: float, delta_q of questions id """ Sp_set = list(S_set) @@ -364,7 +374,6 @@ def delta_q_S_t(self, question_id, pred_all,S_set,sampled_elements): F_sp =self.F_s_func(Sp_set,w_ij_matrix) return F_sp - F_s - def expected_model_change(self, sid: int, qid: int, adaptest_data: AdapTestDataset, pred_all: dict): """ get expected model change Args: diff --git a/CAT/strategy/BECAT_strategy.py b/CAT/strategy/BECAT_strategy.py index dcaff11..48559fb 100644 --- a/CAT/strategy/BECAT_strategy.py +++ b/CAT/strategy/BECAT_strategy.py @@ -9,27 +9,23 @@ class BECATstrategy(AbstractStrategy): def __init__(self): super().__init__() + @property def name(self): return 'BECAT Strategy' + def adaptest_select(self, model: AbstractModel, adaptest_data: AdapTestDataset,S_set): - """ - submodular computation - """ assert hasattr(model, 'delta_q_S_t'), \ 'the models must implement delta_q_S_t method' assert hasattr(model, 'get_pred'), \ 'the models must implement get_pred method for accelerating' pred_all = model.get_pred(adaptest_data) - - #reduced_pred_all = {**reduced_pred_all, **selected_questions_sample} selection = {} for sid in range(adaptest_data.num_students): tmplen = (len(S_set[sid])) untested_questions = np.array(list(adaptest_data.untested[sid])) sampled_elements = np.random.choice(untested_questions, tmplen + 5) untested_deltaq = [model.delta_q_S_t(qid, pred_all[sid],S_set[sid],sampled_elements) for qid in untested_questions] - j = np.argmax(untested_deltaq) selection[sid] = untested_questions[j] # Question bank Q