generated from Lightning-AI/deep-learning-project-template
-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from unitaryai/updated_bias_model
Updated bias model training code
- Loading branch information
Showing
6 changed files
with
158 additions
and
25 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
configs/Unintended_bias_toxic_comment_classification_RoBERTa_combined.json
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,58 @@ | ||
{ | ||
"name": "Jigsaw_RoBERTa_combined", | ||
"n_gpu": 1, | ||
"batch_size": 10, | ||
"accumulate_grad_batches": 3, | ||
"num_main_classes": 7, | ||
"loss_weight": 0.75, | ||
"arch": { | ||
"type": "ROBERTA", | ||
"args": { | ||
"num_classes": 16, | ||
"model_type": "roberta-base", | ||
"model_name": "RobertaForSequenceClassification", | ||
"tokenizer_name": "RobertaTokenizer" | ||
} | ||
}, | ||
"dataset": { | ||
"type": "JigsawDataBias", | ||
"args": { | ||
"train_csv_file": [ | ||
"jigsaw_data/jigsaw-unintended-bias-in-toxicity-classification/train.csv", | ||
"jigsaw_data/jigsaw-toxic-comment-classification-challenge/train.csv" | ||
], | ||
"test_csv_file": "jigsaw_data/jigsaw-unintended-bias-in-toxicity-classification/test_public_expanded.csv", | ||
"val_fraction": null, | ||
"create_val_set": false, | ||
"loss_weight": 0.75, | ||
"classes": [ | ||
"toxicity", | ||
"severe_toxicity", | ||
"obscene", | ||
"identity_attack", | ||
"insult", | ||
"threat", | ||
"sexual_explicit" | ||
], | ||
"identity_classes": [ | ||
"male", | ||
"female", | ||
"homosexual_gay_or_lesbian", | ||
"christian", | ||
"jewish", | ||
"muslim", | ||
"black", | ||
"white", | ||
"psychiatric_or_mental_illness" | ||
] | ||
} | ||
}, | ||
"optimizer": { | ||
"type": "Adam", | ||
"args": { | ||
"lr": 3e-5, | ||
"weight_decay": 3e-6, | ||
"amsgrad": true | ||
} | ||
} | ||
} |
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,57 @@ | ||
import torch | ||
from collections import OrderedDict | ||
import hashlib | ||
import argparse | ||
|
||
|
||
def main(): | ||
"""Converts saved checkpoint to the expected format for detoxify. | ||
""" | ||
checkpoint = torch.load(ARGS.checkpoint, map_location=ARGS.device) | ||
|
||
new_state_dict = { | ||
"state_dict": OrderedDict(), | ||
"config": checkpoint["hyper_parameters"]["config"], | ||
} | ||
for k, v in checkpoint["state_dict"].items(): | ||
if k.startswith("model."): | ||
k = k[6:] # remove `model.` | ||
new_state_dict["state_dict"][k] = v | ||
|
||
torch.save(new_state_dict, ARGS.save_to) | ||
|
||
if ARGS.hash: | ||
with open(ARGS.save_to, "rb") as f: | ||
bytes = f.read() # read entire file as bytes | ||
readable_hash = hashlib.sha256(bytes).hexdigest() | ||
print("Hash: ", readable_hash) | ||
|
||
torch.save(new_state_dict, ARGS.save_to[:-5] + f"-{readable_hash[:8]}.ckpt") | ||
|
||
|
||
if __name__ == "__main__": | ||
parser = argparse.ArgumentParser() | ||
parser.add_argument( | ||
"--ckeckpoint", | ||
type=str, | ||
help="path to model checkpoint", | ||
) | ||
parser.add_argument( | ||
"--save_to", | ||
type=str, | ||
help="path to save the model to", | ||
) | ||
parser.add_argument( | ||
"--device", | ||
type=str, | ||
default="cpu", | ||
help="device to load the checkpoint on", | ||
) | ||
parser.add_argument( | ||
"--hash", | ||
type=bool, | ||
default=True, | ||
help="option to save hash in name", | ||
) | ||
ARGS = parser.parse_args() | ||
main() |
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
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