-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
933ebd8
commit 150d3df
Showing
21 changed files
with
876 additions
and
38 deletions.
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
Empty file.
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,85 @@ | ||
import pandas as pd | ||
import datasets | ||
from datasets import load_dataset | ||
from sklearn.model_selection import train_test_split | ||
|
||
TRAINING_CLASSIFIER_PROMPT_v2 = """### Q:{sentence} ### Math:{label}""" | ||
INFERENCE_CLASSIFIER_PROMPT_v2 = """### Sentence:{sentence} ### Class:""" | ||
|
||
def clean_newsgroup_data(texts, labels): | ||
label2data = {} | ||
clean_data, clean_labels = [], [] | ||
for data, label in zip(texts, labels): | ||
if isinstance(data, str) and isinstance(label, str): | ||
clean_data.append(data) | ||
clean_labels.append(label) | ||
|
||
if label not in label2data: | ||
label2data[label] = data | ||
|
||
return label2data, clean_data, clean_labels | ||
|
||
def get_newsgroup_instruction_data(mode, texts, labels): | ||
if mode == "train": | ||
prompt = TRAINING_CLASSIFIER_PROMPT_v2 | ||
elif mode == "inference": | ||
prompt = INFERENCE_CLASSIFIER_PROMPT_v2 | ||
|
||
instructions = [] | ||
|
||
for text, label in zip(texts, labels): | ||
if mode == "train": | ||
example = prompt.format( | ||
sentence=text, | ||
label=label, | ||
) | ||
elif mode == "inference": | ||
example = prompt.format( | ||
sentence=text, | ||
) | ||
instructions.append(example) | ||
|
||
return instructions | ||
|
||
def get_newsgroup_data_for_ft(mode="train", train_sample_fraction=0.99): | ||
newsgroup_dataset = load_dataset("rungalileo/20_Newsgroups_Fixed") | ||
train_data = newsgroup_dataset["train"]["text"] | ||
train_labels = newsgroup_dataset["train"]["label"] | ||
label2data, train_data, train_labels = clean_newsgroup_data(train_data, train_labels) | ||
|
||
test_data = newsgroup_dataset["test"]["text"] | ||
test_labels = newsgroup_dataset["test"]["label"] | ||
_, test_data, test_labels = clean_newsgroup_data(test_data, test_labels) | ||
|
||
# sample n points from training data | ||
train_df = pd.DataFrame(data={"text": train_data, "label": train_labels}) | ||
train_df, _ = train_test_split( | ||
train_df, | ||
train_size=train_sample_fraction, | ||
stratify=train_df["label"], | ||
random_state=42, | ||
) | ||
train_data = train_df["text"] | ||
train_labels = train_df["label"] | ||
|
||
train_instructions = get_newsgroup_instruction_data(mode, train_data, train_labels) | ||
test_instructions = get_newsgroup_instruction_data(mode, test_data, test_labels) | ||
|
||
train_dataset = datasets.Dataset.from_pandas( | ||
pd.DataFrame( | ||
data={ | ||
"instructions": train_instructions, | ||
"labels": train_labels, | ||
} | ||
) | ||
) | ||
test_dataset = datasets.Dataset.from_pandas( | ||
pd.DataFrame( | ||
data={ | ||
"instructions": test_instructions, | ||
"labels": test_labels, | ||
} | ||
) | ||
) | ||
|
||
return train_dataset, test_dataset |
Oops, something went wrong.