-
Notifications
You must be signed in to change notification settings - Fork 67
[FEATURE] Add SKT model #34
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
Open
weizhehuang0827
wants to merge
12
commits into
bigdata-ustc:main
Choose a base branch
from
weizhehuang0827:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
6b51918
[feat] add SKT example
8567580
[feat] add SKT
d7e9f1b
[docs] update docs for skt
4d69d08
[docs] fix skt paper url
weizhehuang0827 c7a53fb
Merge branch 'bigdata-ustc:main' into main
weizhehuang0827 5feecd1
[style] fix flake8
weizhehuang0827 f4b5837
Merge remote-tracking branch 'origin/main'
weizhehuang0827 b585057
[style] fix flake8
weizhehuang0827 bfe6d1e
[test] fix skt test converage
weizhehuang0827 c2ebd32
Merge branch 'main' into main
weizhehuang0827 21b4b24
[revert] revert GKT
weizhehuang0827 0802d5a
Merge branch 'main' into main
weizhehuang0827 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,94 @@ | ||
# coding: utf-8 | ||
# 2023/3/17 @ weizhehuang0827 | ||
|
||
import logging | ||
import numpy as np | ||
import torch | ||
from tqdm import tqdm | ||
from EduKTM import KTM | ||
from .SKTNet import SKTNet | ||
from EduKTM.utils import SLMLoss, tensor2list, pick | ||
from sklearn.metrics import roc_auc_score, accuracy_score | ||
|
||
|
||
class SKT(KTM): | ||
def __init__(self, ku_num, graph_params, hidden_num, net_params: dict = None, loss_params=None): | ||
super(SKT, self).__init__() | ||
self.skt_model = SKTNet( | ||
ku_num, | ||
graph_params, | ||
hidden_num, | ||
**(net_params if net_params is not None else {}) | ||
) | ||
self.loss_params = loss_params if loss_params is not None else {} | ||
|
||
def train(self, train_data, test_data=None, *, epoch: int, device="cpu", lr=0.001) -> ...: | ||
loss_function = SLMLoss(**self.loss_params).to(device) | ||
self.skt_model = self.skt_model.to(device) | ||
trainer = torch.optim.Adam(self.skt_model.parameters(), lr) | ||
|
||
for e in range(epoch): | ||
losses = [] | ||
for (question, data, data_mask, label, pick_index, label_mask) in tqdm(train_data, "Epoch %s" % e): | ||
# convert to device | ||
question: torch.Tensor = question.to(device) | ||
data: torch.Tensor = data.to(device) | ||
data_mask: torch.Tensor = data_mask.to(device) | ||
label: torch.Tensor = label.to(device) | ||
pick_index: torch.Tensor = pick_index.to(device) | ||
label_mask: torch.Tensor = label_mask.to(device) | ||
|
||
# real training | ||
predicted_response, _ = self.skt_model( | ||
question, data, data_mask) | ||
|
||
loss = loss_function(predicted_response, | ||
pick_index, label, label_mask) | ||
|
||
# back propagation | ||
trainer.zero_grad() | ||
loss.backward() | ||
trainer.step() | ||
|
||
losses.append(loss.mean().item()) | ||
print("[Epoch %d] SLMoss: %.6f" % (e, float(np.mean(losses)))) | ||
|
||
if test_data is not None: | ||
auc, accuracy = self.eval(test_data, device=device) | ||
print("[Epoch %d] auc: %.6f, accuracy: %.6f" % | ||
(e, auc, accuracy)) | ||
|
||
def eval(self, test_data, device="cpu") -> tuple: | ||
self.skt_model.eval() | ||
y_true = [] | ||
y_pred = [] | ||
|
||
for (question, data, data_mask, label, pick_index, label_mask) in tqdm(test_data, "evaluating"): | ||
# convert to device | ||
question: torch.Tensor = question.to(device) | ||
data: torch.Tensor = data.to(device) | ||
data_mask: torch.Tensor = data_mask.to(device) | ||
label: torch.Tensor = label.to(device) | ||
pick_index: torch.Tensor = pick_index.to(device) | ||
label_mask: torch.Tensor = label_mask.to(device) | ||
|
||
# real evaluating | ||
output, _ = self.skt_model(question, data, data_mask) | ||
output = output[:, :-1] | ||
output = pick(output, pick_index.to(output.device)) | ||
pred = tensor2list(output) | ||
label = tensor2list(label) | ||
for i, length in enumerate(label_mask.cpu().tolist()): | ||
length = int(length) | ||
y_true.extend(label[i][:length]) | ||
y_pred.extend(pred[i][:length]) | ||
self.skt_model.train() | ||
return roc_auc_score(y_true, y_pred), accuracy_score(y_true, np.array(y_pred) >= 0.5) | ||
|
||
def save(self, filepath) -> ...: | ||
torch.save(self.skt_model.state_dict(), filepath) | ||
logging.info("save parameters to %s" % filepath) | ||
|
||
def load(self, filepath): | ||
self.skt_model.load_state_dict(torch.load(filepath)) | ||
logging.info("load parameters from %s" % filepath) |
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.
Uh oh!
There was an error while loading. Please reload this page.