Skip to content

Commit

Permalink
Validate and log validation schedule (#141)
Browse files Browse the repository at this point in the history
* Add sanity check for `validatefreq < niters`
* Add log message for validation schedule
* Add sanity check for `niters<validatefreq` in tensorflow trainer
  • Loading branch information
ali-abz authored Mar 26, 2021
1 parent 7b7dc1d commit 0121f6e
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
20 changes: 20 additions & 0 deletions capreolus/trainer/pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ def build(self):
if self.config["niters"] <= 0:
raise ValueError("niters must be > 0")

if self.config["niters"] < self.config["validatefreq"]:
raise ValueError("niters must be equal or greater than validatefreq")

if self.config["itersize"] < self.config["batch"]:
raise ValueError("itersize must be >= batch")

Expand Down Expand Up @@ -161,6 +164,22 @@ def fastforward_training(self, reranker, weights_path, loss_fn, best_metric_fn):
logger.info("attempted to load weights from %s but failed, starting at iteration 0", weights_fn)
return default_return_values

def get_validation_schedule_msg(self, initial_iter=0):
"""Describe validation schedule considering `niters` and `validatefreq`
Args:
initial_iter (int): starting point of iteration. defined by train method.
Example:
Assuming self.config["niters"] = 20 and self.config["validatefreq"] = 3, this method will return:
`Validation is scheduled on iterations: [3, 6, 9, 12, 15, 18]` given initial_iter in [0, 1, 2]
`Validation is scheduled on iterations: [6, 9, 12, 15, 18]` given initial_iter == 3
"""
validation_schedule = [validate for validate in range(initial_iter+1, self.config["niters"]+1)
if validate % self.config["validatefreq"] == 0]
msg = f"Validation is scheduled on iterations: {validation_schedule}"
return msg

def train(self, reranker, train_dataset, train_output_path, dev_data, dev_output_path, qrels, metric, relevance_level=1):
"""Train a model following the trainer's config (specifying batch size, number of iterations, etc).
Expand Down Expand Up @@ -225,6 +244,7 @@ def train(self, reranker, train_dataset, train_output_path, dev_data, dev_output
logger.debug("fastforwarding train_dataloader to iteration %s", initial_iter)
self.exhaust_used_train_data(train_dataloader, n_batch_to_exhaust=initial_iter * self.n_batch_per_iter)

logger.info(self.get_validation_schedule_msg(initial_iter))
train_start_time = time.time()
for niter in range(initial_iter, self.config["niters"]):
niter = niter + 1 # index from 1
Expand Down
2 changes: 2 additions & 0 deletions capreolus/trainer/tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ def validate(self):
raise ValueError("storage, tpuname and tpuzone configs must be provided when training on TPU")
if self.tpu and self.config["storage"] and not self.config["storage"].startswith("gs://"):
raise ValueError("For TPU utilization, the storage config should start with 'gs://'")
if self.config["niters"] < self.config["validatefreq"]:
raise ValueError("niters must be equal or greater than validatefreq")

def train(self, reranker, train_dataset, train_output_path, dev_data, dev_output_path, qrels, metric, relevance_level=1):
if self.tpu:
Expand Down

0 comments on commit 0121f6e

Please sign in to comment.