Skip to content

Commit

Permalink
fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
nnnyt committed Jan 19, 2021
1 parent e90fc3b commit ed2212f
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 43 deletions.
8 changes: 3 additions & 5 deletions CAT/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# coding: utf-8

from .dataset import *
from .model import *
from .strategy import *
import CAT.dataset
import CAT.model
import CAT.strategy
22 changes: 11 additions & 11 deletions CAT/dataset/adaptest_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
"""
Expand All @@ -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]))
Expand Down
18 changes: 9 additions & 9 deletions CAT/dataset/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
return self._concept_map
3 changes: 1 addition & 2 deletions CAT/dataset/train_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
18 changes: 9 additions & 9 deletions CAT/model/IRT.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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):
Expand All @@ -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()
Expand All @@ -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))

Expand Down Expand Up @@ -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))

Expand Down
6 changes: 3 additions & 3 deletions CAT/model/abstract_model.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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
Expand Down
16 changes: 12 additions & 4 deletions CAT/strategy/random_strategy.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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

0 comments on commit ed2212f

Please sign in to comment.