Skip to content

Commit

Permalink
Formatted with black defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Thilina Rajapakse committed May 18, 2021
1 parent cd07d5e commit a44487d
Show file tree
Hide file tree
Showing 91 changed files with 5,589 additions and 1,472 deletions.
22 changes: 17 additions & 5 deletions examples/hyperparameter tuning/extended-tuning/sweep_layerwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from simpletransformers.classification import ClassificationArgs, ClassificationModel
from utils import load_rte_data_file

layer_parameters = {f"layer_{i}-{i + 6}": {"min": 0.0, "max": 5e-5} for i in range(0, 24, 6)}
layer_parameters = {
f"layer_{i}-{i + 6}": {"min": 0.0, "max": 5e-5} for i in range(0, 24, 6)
}

sweep_config = {
"name": "layerwise-sweep-batch-16",
Expand Down Expand Up @@ -62,7 +64,11 @@ def train():
wandb.init()

# Get sweep hyperparameters
args = {key: value["value"] for key, value in wandb.config.as_dict().items() if key != "_wandb"}
args = {
key: value["value"]
for key, value in wandb.config.as_dict().items()
if key != "_wandb"
}

# Extracting the hyperparameter values
cleaned_args = {}
Expand All @@ -89,7 +95,9 @@ def train():
{
"params": [params_key],
"lr": value,
"weight_decay": model_args.weight_decay if "bias" not in params_key else 0.0,
"weight_decay": model_args.weight_decay
if "bias" not in params_key
else 0.0,
}
)
else:
Expand All @@ -100,13 +108,17 @@ def train():
model_args.update_from_dict(cleaned_args)

# Create a TransformerModel
model = ClassificationModel("roberta", "roberta-large", use_cuda=True, args=model_args)
model = ClassificationModel(
"roberta", "roberta-large", use_cuda=True, args=model_args
)

# Train the model
model.train_model(
train_df,
eval_df=eval_df,
accuracy=lambda truth, predictions: accuracy_score(truth, [round(p) for p in predictions]),
accuracy=lambda truth, predictions: accuracy_score(
truth, [round(p) for p in predictions]
),
)

# model.eval_model(eval_df, f1=f1_score)
Expand Down
17 changes: 14 additions & 3 deletions examples/hyperparameter tuning/extended-tuning/sweep_vanilla.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@
"name": "vanilla-sweep-batch-16",
"method": "bayes",
"metric": {"name": "accuracy", "goal": "maximize"},
"parameters": {"num_train_epochs": {"min": 1, "max": 40}, "learning_rate": {"min": 0, "max": 4e-4},},
"parameters": {
"num_train_epochs": {"min": 1, "max": 40},
"learning_rate": {"min": 0, "max": 4e-4},
},
"early_terminate": {"type": "hyperband", "min_iter": 6,},
}

Expand Down Expand Up @@ -54,13 +57,21 @@ def train():
wandb.init()

# Create a TransformerModel
model = ClassificationModel("roberta", "roberta-large", use_cuda=True, args=model_args, sweep_config=wandb.config,)
model = ClassificationModel(
"roberta",
"roberta-large",
use_cuda=True,
args=model_args,
sweep_config=wandb.config,
)

# Train the model
model.train_model(
train_df,
eval_df=eval_df,
accuracy=lambda truth, predictions: accuracy_score(truth, [round(p) for p in predictions]),
accuracy=lambda truth, predictions: accuracy_score(
truth, [round(p) for p in predictions]
),
)

# Sync wandb
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@
model.train_model(
train_df,
eval_df=eval_df,
accuracy=lambda truth, predictions: accuracy_score(truth, [round(p) for p in predictions]),
accuracy=lambda truth, predictions: accuracy_score(
truth, [round(p) for p in predictions]
),
)

model.eval_model(test_df, verbose=True)
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@
{
"params": [params_key],
"lr": value[0],
"weight_decay": model_args.weight_decay if "bias" not in params_key else 0.0,
"weight_decay": model_args.weight_decay
if "bias" not in params_key
else 0.0,
}
)
elif key == "num_train_epochs":
Expand All @@ -91,7 +93,9 @@
model.train_model(
train_df,
eval_df=eval_df,
accuracy=lambda truth, predictions: accuracy_score(truth, [round(p) for p in predictions]),
accuracy=lambda truth, predictions: accuracy_score(
truth, [round(p) for p in predictions]
),
)

model.eval_model(test_df, verbose=True)
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@
model.train_model(
train_df,
eval_df=eval_df,
accuracy=lambda truth, predictions: accuracy_score(truth, [round(p) for p in predictions]),
accuracy=lambda truth, predictions: accuracy_score(
truth, [round(p) for p in predictions]
),
)

model.eval_model(test_df, verbose=True)
4 changes: 3 additions & 1 deletion examples/hyperparameter tuning/extended-tuning/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@

def load_rte_data_file(filepath):
df = pd.read_json(filepath, lines=True)
df = df.rename(columns={"premise": "text_a", "hypothesis": "text_b", "label": "labels"})
df = df.rename(
columns={"premise": "text_a", "hypothesis": "text_b", "label": "labels"}
)
df = df[["text_a", "text_b", "labels"]]
return df

Expand Down
13 changes: 11 additions & 2 deletions examples/hyperparameter tuning/sweeps.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
sweep_config = {
"method": "bayes", # grid, random
"metric": {"name": "train_loss", "goal": "minimize"},
"parameters": {"num_train_epochs": {"values": [2, 3, 5]}, "learning_rate": {"min": 5e-5, "max": 4e-4}},
"parameters": {
"num_train_epochs": {"values": [2, 3, 5]},
"learning_rate": {"min": 5e-5, "max": 4e-4},
},
}

sweep_id = wandb.sweep(sweep_config, project="Simple Sweep")
Expand Down Expand Up @@ -51,7 +54,13 @@ def train():
wandb.init()

# Create a TransformerModel
model = ClassificationModel("roberta", "roberta-base", use_cuda=True, args=model_args, sweep_config=wandb.config,)
model = ClassificationModel(
"roberta",
"roberta-base",
use_cuda=True,
args=model_args,
sweep_config=wandb.config,
)

# Train the model
model.train_model(train_df, eval_df=eval_df)
Expand Down
12 changes: 9 additions & 3 deletions examples/language_generation/generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
transformers_logger = logging.getLogger("transformers")
transformers_logger.setLevel(logging.WARNING)

model = LanguageGenerationModel("gpt2", "outputs/from_scratch/", args={"max_length": 200})
model = LanguageGenerationModel(
"gpt2", "outputs/from_scratch/", args={"max_length": 200}
)
# model = LanguageGenerationModel("gpt2", "outputs/fine-tuned", args={"max_length": 200})
# model = LanguageGenerationModel("gpt2", "gpt2", args={"max_length": 200})

Expand All @@ -23,6 +25,10 @@
generated = model.generate(prompt, verbose=False)

generated = ".".join(generated[0].split(".")[:-1]) + "."
print("=============================================================================")
print(
"============================================================================="
)
print(generated)
print("=============================================================================")
print(
"============================================================================="
)
5 changes: 4 additions & 1 deletion examples/language_generation/train_new_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@

parser = argparse.ArgumentParser()
parser.add_argument(
"--local_rank", type=int, default=-1, help="Local rank. Necessary for using the torch.distributed.launch utility."
"--local_rank",
type=int,
default=-1,
help="Local rank. Necessary for using the torch.distributed.launch utility.",
)
args = parser.parse_args()

Expand Down
18 changes: 14 additions & 4 deletions examples/language_representation/binary_classification_dummy.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,16 @@

from simpletransformers.language_representation import RepresentationModel

train_data = [["Example sentence belonging to class 1", 1], ["Example sentence belonging to class 0", 0]]
train_data = [
["Example sentence belonging to class 1", 1],
["Example sentence belonging to class 0", 0],
]
train_df = pd.DataFrame(train_data, columns=["text", "target"])

eval_data = [["Example eval sentence belonging to class 1", 1], ["Example eval sentence belonging to class 0", 0]]
eval_data = [
["Example eval sentence belonging to class 1", 1],
["Example eval sentence belonging to class 0", 0],
]
eval_df = pd.DataFrame(eval_data, columns=["text", "target"])

model = RepresentationModel(
Expand All @@ -16,8 +22,12 @@
use_cuda=False,
args={"no_save": True, "reprocess_input_data": True, "overwrite_output_dir": True},
)
train_vectors = model.encode_sentences(train_df["text"].to_list(), combine_strategy="mean")
eval_vectors = model.encode_sentences(eval_df["text"].to_list(), combine_strategy="mean")
train_vectors = model.encode_sentences(
train_df["text"].to_list(), combine_strategy="mean"
)
eval_vectors = model.encode_sentences(
eval_df["text"].to_list(), combine_strategy="mean"
)


clf_model = RidgeClassifier()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

from simpletransformers.language_representation import RepresentationModel

project_root = dirname(dirname(dirname(dirname(__file__)))) # path to root of the project
project_root = dirname(
dirname(dirname(dirname(__file__)))
) # path to root of the project

MODEL_TYPE = "gpt2" # change this to test other model types: bert, roberta, gpt2

Expand All @@ -22,9 +24,13 @@
train_df[0] = (train_df[0] == 2).astype(int)
eval_df[0] = (eval_df[0] == 2).astype(int)
# don't use entire dataset, since it's too big and will tale a long time to run, select only a portion of it
train_df = pd.DataFrame({"text": train_df[1].replace(r"\n", " ", regex=True), "labels": train_df[0]})[:1000]
train_df = pd.DataFrame(
{"text": train_df[1].replace(r"\n", " ", regex=True), "labels": train_df[0]}
)[:1000]
print(train_df.head())
eval_df = pd.DataFrame({"text": eval_df[1].replace(r"\n", " ", regex=True), "labels": eval_df[0]})[:100]
eval_df = pd.DataFrame(
{"text": eval_df[1].replace(r"\n", " ", regex=True), "labels": eval_df[0]}
)[:100]
print(eval_df.head())


Expand All @@ -44,8 +50,12 @@
args={"no_save": True, "reprocess_input_data": True, "overwrite_output_dir": True},
)

train_vectors = model.encode_sentences(train_df["text"].to_list(), combine_strategy="mean")
eval_vectors = model.encode_sentences(eval_df["text"].to_list(), combine_strategy="mean")
train_vectors = model.encode_sentences(
train_df["text"].to_list(), combine_strategy="mean"
)
eval_vectors = model.encode_sentences(
eval_df["text"].to_list(), combine_strategy="mean"
)


clf_model = RidgeClassifier()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@
eval_df = pd.DataFrame(eval_data, columns=["sentence_id", "words", "labels"])

# Create a NERModel
model = NERModel("bert", "bert-base-cased", args={"overwrite_output_dir": True, "reprocess_input_data": True})
model = NERModel(
"bert",
"bert-base-cased",
args={"overwrite_output_dir": True, "reprocess_input_data": True},
)

# # Train the model
# model.train_model(train_df)
Expand Down
11 changes: 9 additions & 2 deletions examples/question_answering/lazy_qa.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,12 @@
"question": "What was the name of the 1937 treaty?",
"answers": [{"text": "Bald Eagle Protection Act", "answer_start": 167}],
},
{"id": "00004", "is_impossible": True, "question": "How did Alexandar Hamilton die?", "answers": [],},
{
"id": "00004",
"is_impossible": True,
"question": "How did Alexandar Hamilton die?",
"answers": [],
},
],
},
] # noqa: ignore flake8"
Expand Down Expand Up @@ -79,7 +84,9 @@
}

# Create the QuestionAnsweringModel
model = QuestionAnsweringModel("bert", "bert-base-cased", args=train_args, use_cuda=True, cuda_device=0)
model = QuestionAnsweringModel(
"bert", "bert-base-cased", args=train_args, use_cuda=True, cuda_device=0
)

# Train the model with JSON file
model.train_model("data/train.jsonl", eval_data="data/train.json")
Expand Down
40 changes: 32 additions & 8 deletions examples/seq2seq/paraphrasing/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,12 @@
train_df = train_df.loc[train_df["label"] == "1"]
eval_df = eval_df.loc[eval_df["label"] == "1"]

train_df = train_df.rename(columns={"sentence1": "input_text", "sentence2": "target_text"})
eval_df = eval_df.rename(columns={"sentence1": "input_text", "sentence2": "target_text"})
train_df = train_df.rename(
columns={"sentence1": "input_text", "sentence2": "target_text"}
)
eval_df = eval_df.rename(
columns={"sentence1": "input_text", "sentence2": "target_text"}
)

train_df = train_df[["input_text", "target_text"]]
eval_df = eval_df[["input_text", "target_text"]]
Expand All @@ -30,13 +34,25 @@
eval_df["prefix"] = "paraphrase"

# MSRP Data
train_df = pd.concat([train_df, load_data("data/msr_paraphrase_train.txt", "#1 String", "#2 String", "Quality"),])
eval_df = pd.concat([eval_df, load_data("data/msr_paraphrase_test.txt", "#1 String", "#2 String", "Quality"),])
train_df = pd.concat(
[
train_df,
load_data("data/msr_paraphrase_train.txt", "#1 String", "#2 String", "Quality"),
]
)
eval_df = pd.concat(
[
eval_df,
load_data("data/msr_paraphrase_test.txt", "#1 String", "#2 String", "Quality"),
]
)

# Quora Data

# The Quora Dataset is not separated into train/test, so we do it manually the first time.
df = load_data("data/quora_duplicate_questions.tsv", "question1", "question2", "is_duplicate")
df = load_data(
"data/quora_duplicate_questions.tsv", "question1", "question2", "is_duplicate"
)
q_train, q_test = train_test_split(df)

q_train.to_csv("data/quora_train.tsv", sep="\t")
Expand Down Expand Up @@ -91,13 +107,19 @@
model_args.wandb_project = "Paraphrasing with BART"


model = Seq2SeqModel(encoder_decoder_type="bart", encoder_decoder_name="facebook/bart-large", args=model_args,)
model = Seq2SeqModel(
encoder_decoder_type="bart",
encoder_decoder_name="facebook/bart-large",
args=model_args,
)

model.train_model(train_df, eval_data=eval_df)

to_predict = [
prefix + ": " + str(input_text)
for prefix, input_text in zip(eval_df["prefix"].tolist(), eval_df["input_text"].tolist())
for prefix, input_text in zip(
eval_df["prefix"].tolist(), eval_df["input_text"].tolist()
)
]
truth = eval_df["target_text"].tolist()

Expand All @@ -116,4 +138,6 @@
f.write("Prediction:\n")
for pred in preds[i]:
f.write(str(pred) + "\n")
f.write("________________________________________________________________________________\n")
f.write(
"________________________________________________________________________________\n"
)
Loading

0 comments on commit a44487d

Please sign in to comment.