Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2024.1.12 Add new CAT Strategy #10

Merged
merged 12 commits into from
Jan 15, 2024
Prev Previous commit
Change NCAT code structure
  • Loading branch information
Hhhhhhand committed Jan 14, 2024
commit 42da066aea5862862b9749f15a8a0b7374ada07e
71 changes: 3 additions & 68 deletions CAT/model/NCAT.py → CAT/strategy/NCAT_nn/NCAT.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import numpy as np
from scipy.optimize import minimize
from CAT.dataset import AdapTestDataset,Dataset
from CAT.model.IRT import IRT
from CAT.model.IRT import IRT,IRTModel

SavedAction = namedtuple('SavedAction', ['log_prob', 'value'])

Expand Down Expand Up @@ -300,72 +300,6 @@ def __init__(self, d_model, d_ff, dropout=0.1):
def forward(self, x):
return self.w_2(self.dropout(F.relu(self.w_1(x))))

class CDM(nn.Module):
def __init__(self, data: Dataset,config):
# num_dim: IRT if num_dim == 1 else MIRT
super().__init__()
self.model = IRT(data.num_students, data.num_questions, self.config['num_dim'])
def adaptest_update(self, adaptest_data: AdapTestDataset):
"""
Update CDM with tested data
"""
lr = self.config['learning_rate']
batch_size = self.config['batch_size']
epochs = self.config['num_epochs']
device = self.config['device']
optimizer = torch.optim.Adam(self.theta.parameters(), lr=lr)

tested_dataset = adaptest_data.get_tested_dataset(last=True)
dataloader = torch.utils.data.DataLoader(tested_dataset, batch_size=batch_size, shuffle=True)
ls = 0.0
for ep in range(1, epochs + 1):

loss = 0.0
for cnt, (student_ids, question_ids, _, labels) in enumerate(dataloader):
student_ids = student_ids.to(device)
question_ids = question_ids.to(device)
labels = labels.to(device).float()
pred = self.forward(student_ids, question_ids).view(-1)
bz_loss = self._loss_function(pred, labels)
optimizer.zero_grad()
bz_loss.backward(retain_graph=True)
optimizer.step()
loss += bz_loss.data.float()
ls = ls + loss
return ls/(epochs)
def _loss_function(self, pred, real):
return -(real * torch.log(0.0001 + pred) + (1 - real) * torch.log(1.0001 - pred)).mean()
def evaluate(self, adaptest_data: AdapTestDataset):
data = adaptest_data.data
concept_map = adaptest_data.concept_map
device = self.config['device']

real = []
pred = []
with torch.no_grad():
self.eval()
for sid in data:
student_ids = [sid] * len(data[sid])
question_ids = list(data[sid].keys())
real += [data[sid][qid] for qid in question_ids]
student_ids = torch.LongTensor(student_ids).to(device)
question_ids = torch.LongTensor(question_ids).to(device)
output = self.forward(student_ids, question_ids).view(-1)
pred += output.tolist()
self.train()

real = np.array(real)
real = np.where(real < 0.5, 0.0, 1.0)
pred = np.array(pred)
auc = roc_auc_score(real, pred)

# Calculate accuracy
threshold = 0.5 # You may adjust the threshold based on your use case
binary_pred = (pred >= threshold).astype(int)
acc = accuracy_score(real, binary_pred)

return auc,acc

class env:
def __init__(self,data,concept_map,config,T):
self.config = config
Expand Down Expand Up @@ -432,7 +366,8 @@ def load_data(self, ncatdata,concept):

def load_CDM(self,name,data,pth_path,config):
if name == 'IRT':
model = CDM(data,config)
model = IRTModel(**config)
model.init_model(data)
model.load_state_dict(torch.load(pth_path), strict=False)
model.to(self.config['device'])
return model ,data.data
Expand Down
6 changes: 3 additions & 3 deletions CAT/strategy/NCAT_strategy.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from collections import namedtuple
from scipy.optimize import minimize
from CAT.strategy.abstract_strategy import AbstractStrategy
from CAT.model.NCAT import NCATModel
from CAT.strategy.NCAT_nn.NCAT import NCATModel

class NCATs(AbstractStrategy):

Expand All @@ -21,7 +21,7 @@ def adaptest_select(self, adaptest_data: AdapTestDataset,concept_map,config,tes
for sid in range(adaptest_data.num_students):
NCATdata = adaptest_data
model = NCATModel(NCATdata,concept_map,config,test_length)
THRESHOLD = config['THRESHOLD']
model.ncat_policy(sid,THRESHOLD,used_actions)
threshold = config['THRESHOLD']
model.ncat_policy(sid,threshold,used_actions)

return used_actions