-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
52a1e6f
Add BECAT
Hhhhhhand 5dbb244
Fix bug and Change README
Hhhhhhand 4acd44c
Change README
Hhhhhhand cd09dab
Update README.md
Hhhhhhand cbdd910
Modified code specification
Hhhhhhand 047fcc3
Merge branch 'master' of https://github.com/Hhhhhhand/CAT
Hhhhhhand 18861e4
Add BOBCAT and NCAT
Hhhhhhand 6044fbd
Change readme
Hhhhhhand 6df3983
Merge branch 'master' into master
Hhhhhhand 59d06e0
code formatted
Hhhhhhand 217a3e7
Merge branch 'master' of https://github.com/Hhhhhhand/CAT
Hhhhhhand 42da066
Change NCAT code structure
Hhhhhhand File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import torch | ||
import torch.nn as nn | ||
import torch.nn.functional as F | ||
|
||
def hard_sample(logits, dim=-1): | ||
y_soft = F.softmax(logits, dim=-1) | ||
index = y_soft.max(dim, keepdim=True)[1] | ||
y_hard = torch.zeros_like(y_soft).scatter_(dim, index, 1.0) | ||
ret = y_hard - y_soft.detach() + y_soft | ||
return ret, index | ||
|
||
class Actor(nn.Module): | ||
def __init__(self, state_dim, action_dim, n_latent_var=256): | ||
super().__init__() | ||
# actor | ||
self.obs_layer = nn.Linear(state_dim, n_latent_var) | ||
self.actor_layer = nn.Sequential( | ||
nn.Linear(n_latent_var, n_latent_var), | ||
nn.Tanh(), | ||
nn.Linear(n_latent_var, action_dim) | ||
) | ||
|
||
def forward(self, state, action_mask): | ||
hidden_state = self.obs_layer(state) | ||
logits = self.actor_layer(hidden_state) | ||
inf_mask = torch.clamp(torch.log(action_mask.float()), | ||
min=torch.finfo(torch.float32).min) | ||
logits = logits + inf_mask | ||
actions = hard_sample(logits) | ||
return actions | ||
|
||
class StraightThrough: | ||
def __init__(self, state_dim, action_dim, lr, config): | ||
self.lr = lr | ||
device = config['device'] | ||
self.betas = config['betas'] | ||
self.policy = Actor(state_dim, action_dim).to(device) | ||
self.optimizer = torch.optim.Adam( | ||
self.policy.parameters(), lr=lr, betas=self.betas) | ||
|
||
def update(self, loss): | ||
self.optimizer.zero_grad() | ||
loss.mean().backward() | ||
self.optimizer.step() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import numpy as np | ||
from scipy.optimize import minimize | ||
from CAT.strategy.abstract_strategy import AbstractStrategy | ||
from CAT.model import AbstractModel | ||
from CAT.dataset import AdapTestDataset | ||
|
||
class BOBCAT(AbstractStrategy): | ||
|
||
def __init__(self): | ||
super().__init__() | ||
|
||
@property | ||
def name(self): | ||
return 'BOBCAT' | ||
def adaptest_select(self, model: AbstractModel, adaptest_data: AdapTestDataset,S_set): | ||
assert hasattr(model, 'get_kli'), \ | ||
'the models must implement get_kli method' | ||
assert hasattr(model, 'get_pred'), \ | ||
'the models must implement get_pred method for accelerating' | ||
selection = {} | ||
for sid in range(adaptest_data.num_students): | ||
untested_questions = np.array(list(adaptest_data.untested[sid])) | ||
j = model.bobcat_policy(S_set[sid],untested_questions) | ||
selection[sid] = j | ||
return selection |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update the comments