From ed2212fd3891998a19dc379e1e27eed61e951a4d Mon Sep 17 00:00:00 2001 From: nnnyt <793313994@qq.com> Date: Thu, 14 Jan 2021 17:19:40 +0800 Subject: [PATCH] fix bugs --- CAT/__init__.py | 8 +++----- CAT/dataset/adaptest_dataset.py | 22 +++++++++++----------- CAT/dataset/dataset.py | 18 +++++++++--------- CAT/dataset/train_dataset.py | 3 +-- CAT/model/IRT.py | 18 +++++++++--------- CAT/model/abstract_model.py | 6 +++--- CAT/strategy/random_strategy.py | 16 ++++++++++++---- 7 files changed, 48 insertions(+), 43 deletions(-) diff --git a/CAT/__init__.py b/CAT/__init__.py index 4fce0a0..7407047 100644 --- a/CAT/__init__.py +++ b/CAT/__init__.py @@ -1,5 +1,3 @@ -# coding: utf-8 - -from .dataset import * -from .model import * -from .strategy import * \ No newline at end of file +import CAT.dataset +import CAT.model +import CAT.strategy \ No newline at end of file diff --git a/CAT/dataset/adaptest_dataset.py b/CAT/dataset/adaptest_dataset.py index 23fb179..4768d36 100644 --- a/CAT/dataset/adaptest_dataset.py +++ b/CAT/dataset/adaptest_dataset.py @@ -27,8 +27,8 @@ def __init__(self, data, concept_map, num_students, num_questions, num_concepts) # initialize tested and untested set - self.tested = None - self.untested = None + self._tested = None + self._untested = None self.reset() def apply_selection(self, student_idx, question_idx): @@ -38,27 +38,27 @@ def apply_selection(self, student_idx, question_idx): student_idx: int question_idx: int """ - assert question_idx in self.untested[student_idx], \ + assert question_idx in self._untested[student_idx], \ 'Selected question not allowed' - self.untested[student_idx].remove(question_idx) - self.tested[student_idx].append(question_idx) + self._untested[student_idx].remove(question_idx) + self._tested[student_idx].append(question_idx) def reset(self): """ Set tested set empty """ - self.tested = defaultdict(deque) - self.untested = defaultdict(set) + self._tested = defaultdict(deque) + self._untested = defaultdict(set) for sid in self.data: - self.untested[sid] = set(self.data[sid].keys()) + self._untested[sid] = set(self.data[sid].keys()) @property def tested(self): - return self.tested + return self._tested @property def untested(self): - return self.untested + return self._untested def get_tested_dataset(self, last=False): """ @@ -69,7 +69,7 @@ def get_tested_dataset(self, last=False): TrainDataset """ triplets = [] - for sid, qids in self.tested.items(): + for sid, qids in self._tested.items(): if last: qid = qids[-1] triplets.append((sid, qid, self.data[sid][qid])) diff --git a/CAT/dataset/dataset.py b/CAT/dataset/dataset.py index 48195a3..3b65371 100644 --- a/CAT/dataset/dataset.py +++ b/CAT/dataset/dataset.py @@ -13,18 +13,18 @@ def __init__(self, data, concept_map, num_questions: int, total question number num_concepts: int, total concept number """ - self.raw_data = data - self.concept_map = concept_map + self._raw_data = data + self._concept_map = concept_map self.n_students = num_students self.n_questions = num_questions self.n_concepts = num_concepts # reorganize datasets - self.data = {} + self._data = {} for sid, qid, correct in data: - self.data.setdefault(sid, {}) - self.data[sid].setdefault(qid, {}) - self.data[sid][qid] = correct + self._data.setdefault(sid, {}) + self._data[sid].setdefault(qid, {}) + self._data[sid][qid] = correct student_ids = set(x[0] for x in data) question_ids = set(x[1] for x in data) @@ -51,12 +51,12 @@ def num_concepts(self): @property def raw_data(self): - return self.raw_data + return self._raw_data @property def data(self): - return self.data + return self._data @property def concept_map(self): - return self.concept_map \ No newline at end of file + return self._concept_map \ No newline at end of file diff --git a/CAT/dataset/train_dataset.py b/CAT/dataset/train_dataset.py index 718052d..e7af6c0 100644 --- a/CAT/dataset/train_dataset.py +++ b/CAT/dataset/train_dataset.py @@ -25,8 +25,7 @@ def __init__(self, data, concept_map, def __getitem__(self, item): sid, qid, score = self.raw_data[item] - concepts = self.concept_map[qid] - return sid, qid, score, concepts + return sid, qid, score def __len__(self): return len(self.raw_data) \ No newline at end of file diff --git a/CAT/model/IRT.py b/CAT/model/IRT.py index 6d6773e..3ec5f78 100644 --- a/CAT/model/IRT.py +++ b/CAT/model/IRT.py @@ -11,11 +11,11 @@ try: # for python module from .abstract_model import AbstractModel - from ..dataset import AdapTestDataset, TrainDataset, _Dataset + from ..dataset import AdapTestDataset, TrainDataset, Dataset except (ImportError, SystemError): # pragma: no cover # for python script from abstract_model import AbstractModel - from dataset import AdapTestDataset, TrainDataset, _Dataset + from dataset import AdapTestDataset, TrainDataset, Dataset class IRT(nn.Module): @@ -57,7 +57,7 @@ def __init__(self, **config): def name(self): return 'Item Response Theory' - def init_model(self, data: _Dataset): + def init_model(self, data: Dataset): self.model = IRT(data.num_students, data.num_questions, self.config['num_dim']) def train(self, train_data: TrainDataset): @@ -74,7 +74,7 @@ def train(self, train_data: TrainDataset): for ep in range(1, epochs + 1): loss = 0.0 log_step = 1 - for cnt, (student_ids, question_ids, labels, _) in enumerate(train_loader): + for cnt, (student_ids, question_ids, labels) in enumerate(train_loader): student_ids = student_ids.to(device) question_ids = question_ids.to(device) labels = labels.to(device).float() @@ -83,7 +83,7 @@ def train(self, train_data: TrainDataset): optimizer.zero_grad() bz_loss.backward() optimizer.step() - loss += bz_loss.item() + loss += bz_loss.data.float() if cnt % log_step == 0: logging.info('Epoch [{}] Batch [{}]: loss={:.5f}'.format(ep, cnt, loss / cnt)) @@ -118,16 +118,16 @@ def adaptest_update(self, adaptest_data: AdapTestDataset): for ep in range(1, epochs + 1): loss = 0.0 log_steps = 100 - for cnt, (student_ids, question_ids, correctness, _) in enumerate(dataloader): + for cnt, (student_ids, question_ids, labels) in enumerate(dataloader): student_ids = student_ids.to(device) question_ids = question_ids.to(device) - correctness = correctness.to(device).float() + labels = labels.to(device).float() pred = self.model(student_ids, question_ids).view(-1) - bz_loss = self._loss_function(pred, correctness) + bz_loss = self._loss_function(pred, labels) optimizer.zero_grad() bz_loss.backward() optimizer.step() - loss += bz_loss.item() + loss += bz_loss.data.float() if cnt % log_steps == 0: print('Epoch [{}] Batch [{}]: loss={:.3f}'.format(ep, cnt, loss / cnt)) diff --git a/CAT/model/abstract_model.py b/CAT/model/abstract_model.py index b0e9c01..17123cd 100644 --- a/CAT/model/abstract_model.py +++ b/CAT/model/abstract_model.py @@ -1,10 +1,10 @@ from abc import ABC, abstractmethod try: # for python module - from ..dataset import AdapTestDataset, TrainDataset, _Dataset + from ..dataset import AdapTestDataset, TrainDataset, Dataset except (ImportError, SystemError): # pragma: no cover # for python script - from dataset import AdapTestDataset, TrainDataset, _Dataset + from dataset import AdapTestDataset, TrainDataset, Dataset class AbstractModel(ABC): @@ -23,7 +23,7 @@ def evaluate(self, adaptest_data: AdapTestDataset): raise NotImplementedError @abstractmethod - def init_model(self, data: _Dataset): + def init_model(self, data: Dataset): raise NotImplementedError @abstractmethod diff --git a/CAT/strategy/random_strategy.py b/CAT/strategy/random_strategy.py index 23eea37..af34a82 100644 --- a/CAT/strategy/random_strategy.py +++ b/CAT/strategy/random_strategy.py @@ -1,7 +1,15 @@ import numpy as np -from .abstract_strategy import AbstractStrategy -from ..model import AbstractModel -from ..dataset import AdapTestDataset + +try: + # for python module + from .abstract_strategy import AbstractStrategy + from ..model import AbstractModel + from ..dataset import AdapTestDataset +except (ImportError, SystemError): # pragma: no cover + # for python script + from abstract_strategy import AbstractStrategy + from model import AbstractModel + from dataset import AdapTestDataset class RandomStrategy(AbstractStrategy): @@ -17,5 +25,5 @@ def adaptest_select(self, model: AbstractModel, adaptest_data: AdapTestDataset): selection = {} for sid in range(adaptest_data.num_students): untested_questions = np.array(list(adaptest_data.untested[sid])) - selection[sid] = np.random.randint(len(untested_questions)) + selection[sid] = untested_questions[np.random.randint(len(untested_questions))] return selection \ No newline at end of file