-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactoring parameter class #133
base: main
Are you sure you want to change the base?
Changes from all commits
11a319f
e1bc082
1038265
a2d5295
2be535a
bdd65d7
cf82663
7e0f4eb
36e1596
7815fad
a4895cb
1c8a8d0
8cfdfde
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -145,3 +145,4 @@ scripts/tapt/output_dir/* | |
|
||
# The dev folder | ||
dev/* | ||
/scripts/charlies_wip/ |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -397,7 +397,7 @@ def __init__( | |
self._model = None | ||
|
||
# set device | ||
self.device = torch.device(self.parameters["cuda_device"]) | ||
self.device = torch.device(self.parameters.cuda_device) | ||
|
||
def train_afresh( | ||
self, | ||
|
@@ -408,7 +408,7 @@ def train_afresh( | |
): | ||
# If we're refreshing every iteration, and the most recent set of | ||
# samples were all skipped, then don't do any trying | ||
if new_tokenized_samples is not None and self.parameters["refresh_every"] == 1: | ||
if new_tokenized_samples is not None and self.parameters.refresh_every == 1: | ||
new_tokenized_samples = new_tokenized_samples.filter( | ||
lambda x: x[SKIPS_COLUMN_NAME] == 0 | ||
) | ||
|
@@ -432,11 +432,11 @@ def train_afresh( | |
|
||
# create a dataloader for the train dataset | ||
train_dataloader = DataLoader( | ||
tokenized_train, shuffle=True, batch_size=self.parameters["batch_size"] | ||
tokenized_train, shuffle=True, batch_size=self.parameters.batch_size | ||
) | ||
|
||
# Run the training loop | ||
self._train(train_dataloader, self.parameters["num_epochs_afresh"], iteration) | ||
self._train(train_dataloader, self.parameters.num_epochs_afresh, iteration) | ||
|
||
def train_update( | ||
self, | ||
|
@@ -463,11 +463,11 @@ def train_update( | |
|
||
# Make a sample loader from the latest batch of labelled samples | ||
samples_dataloader = DataLoader( | ||
tokenized_samples, shuffle=True, batch_size=self.parameters["batch_size"] | ||
tokenized_samples, shuffle=True, batch_size=self.parameters.batch_size | ||
) | ||
|
||
# Run the training loop | ||
self._train(samples_dataloader, self.parameters["num_epochs_update"], iteration) | ||
self._train(samples_dataloader, self.parameters.num_epochs_update, iteration) | ||
|
||
def _initialise(self): | ||
pass | ||
|
@@ -484,7 +484,7 @@ def _load_fresh_model(self): | |
models = [] | ||
|
||
# Load fresh versions of the model | ||
for i in range(self.parameters["num_classifier_models"]): | ||
for i in range(self.parameters.num_classifier_models): | ||
models.append( | ||
AutoModelForSequenceClassification.from_pretrained( | ||
self.MODEL_NAME, num_labels=len(self.dataset_container.CATEGORIES) | ||
|
@@ -521,26 +521,26 @@ def _setup_model(self): | |
|
||
def _train(self, train_dataloader: DataLoader, num_epochs: int, iteration: int): | ||
# create an optimizer for the model | ||
optimizer = AdamW(self._model.parameters(), lr=self.parameters["learning_rate"]) | ||
optimizer = AdamW(self._model.parameters(), lr=self.parameters.learning_rate) | ||
|
||
# The eval dataloader | ||
eval_dataloader = DataLoader( | ||
self.dataset_container.tokenized_validation, | ||
batch_size=self.parameters["eval_batch_size"], | ||
batch_size=self.parameters.eval_batch_size, | ||
) | ||
|
||
# The test dataloader | ||
test_dataloader = DataLoader( | ||
self.dataset_container.tokenized_test, | ||
batch_size=self.parameters["eval_batch_size"], | ||
batch_size=self.parameters.eval_batch_size, | ||
) | ||
|
||
# create a learning rate scheduler | ||
num_training_steps = num_epochs * len(train_dataloader) | ||
lr_scheduler = get_scheduler( | ||
name="linear", | ||
optimizer=optimizer, | ||
num_warmup_steps=self.parameters["num_warmup_steps"], | ||
num_warmup_steps=self.parameters.num_warmup_steps, | ||
num_training_steps=num_training_steps, | ||
) | ||
|
||
|
@@ -570,24 +570,33 @@ def _train(self, train_dataloader: DataLoader, num_epochs: int, iteration: int): | |
("eval", eval_dataloader), | ||
("test", test_dataloader), | ||
]: | ||
# If the eval loop should run this epoch, or if it is the last epoch | ||
run_eval = ( | ||
self.parameters[f"{split}_every"] > 0 | ||
and (epoch + 1) % self.parameters[f"{split}_every"] == 0 | ||
) | ||
run_eval = run_eval or ( | ||
self.parameters[f"{split}_every"] >= 0 and epoch == num_epochs - 1 | ||
) | ||
if split == "eval": | ||
eval_frequency = self.parameters.eval_every | ||
elif split == "test": | ||
eval_frequency = self.parameters.test_every | ||
else: | ||
raise ValueError | ||
|
||
if run_eval: | ||
# Run the evaluation loop, obtaining the metrics | ||
print(f"- Running {split} loop") | ||
eval_metrics = self._eval_epoch(dataloader) | ||
print( | ||
f"{split.capitalize()} mean loss: {eval_metrics['loss']:.8}; " | ||
f"{split.capitalize()} f1: {eval_metrics['f1']:.6}" | ||
) | ||
results_to_log[split] = eval_metrics | ||
# if eval_frequency == -1, we should never evaluate the model | ||
if eval_frequency < 0: | ||
assert eval_frequency == -1 | ||
pass | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you replace this with There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also I think that any validation should be done in the Parameters class, rather than here with assert statements |
||
else: | ||
if eval_frequency == 0: | ||
regular_eval_is_due = False | ||
else: | ||
regular_eval_is_due = (epoch + 1) % eval_frequency == 0 | ||
is_last_epoch = epoch == num_epochs - 1 | ||
|
||
if regular_eval_is_due or is_last_epoch: | ||
# Run the evaluation loop, obtaining the metrics | ||
print(f"- Running {split} loop") | ||
eval_metrics = self._eval_epoch(dataloader) | ||
print( | ||
f"{split.capitalize()} mean loss:" | ||
f" {eval_metrics['loss']:.8}; " | ||
f"{split.capitalize()} f1: {eval_metrics['f1']:.6}" | ||
) | ||
SamAdamDay marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Record the metrics with W&B | ||
self.wandb_run.log(results_to_log) | ||
|
@@ -798,7 +807,7 @@ def calculate_uncertainties_tokenized( | |
num_samples = tokenized_samples.shape[0] | ||
|
||
# Store the batch size with a shorter variable name | ||
batch_size = self.parameters["eval_batch_size"] | ||
batch_size = self.parameters.eval_batch_size | ||
|
||
# Make a PyTorch dataloader for the samples | ||
samples_dataloader = DataLoader(tokenized_samples, batch_size=batch_size) | ||
|
@@ -894,11 +903,11 @@ def _load_fresh_model(self): | |
models, training_args = load_tapted_model( | ||
self.wandb_run, | ||
self.MODEL_NAME, | ||
self.parameters["dataset_name"], | ||
self.parameters.dataset_name, | ||
"classifier", | ||
num_categories=len(self.dataset_container.CATEGORIES), | ||
tapted_model_version=self.parameters["tapted_model_version"], | ||
num_models=self.parameters["num_classifier_models"], | ||
tapted_model_version=self.parameters.tapted_model_version, | ||
num_models=self.parameters.num_classifier_models, | ||
) | ||
self._model = HuggingFaceClassifierEnsemble(models) | ||
self.training_parameters = training_args | ||
|
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.
This case is impossible. Can you remove it? It confused me at first