def run(self, iterator, criterion, optimizer, verbose=True): r"""Train one time the model on iterator data. Args: iterator (Iterator): iterator containing batch samples of data. criterion (Loss): loss function to measure scores. optimizer (Optimizer): optimizer used during training to update weights. verbose (bool): if ``True`` display a progress bar. Returns: dict: the performance and metrics of the training session. """ # Initialize the variables start_time = time.time() epoch_loss = 0 epoch_acc = 0 class_labels = list(range(self.output_dim)) class_labels.pop(self.pad_idx_label) confusion_matrix = ConfusionMatrix(labels=class_labels) # Train mode self.train() for (idx, batch) in enumerate(iterator): optimizer.zero_grad() # One forward step text, length = batch.text y_hat = self.forward(text, length) y_hat = y_hat.view(-1, y_hat.shape[-1]) label = batch.label.view(-1) # Get the predicted classes y_tilde = y_hat.argmax(dim=1, keepdim=True) # Compute the loss and update the weights loss = criterion(y_hat, label) loss.backward() optimizer.step() epoch_loss += loss.item() # Default accuracy acc = self.get_accuracy(y_tilde, label) epoch_acc += acc.item() # Optional: display a progress bar if verbose: progress_bar(idx, len(iterator) - 1, prefix="Training:\t", start_time=start_time) # Update the confusion matrix confusion_matrix.update(label.long().numpy(), y_tilde.long().numpy()) # Store the loss, accuracy and metrics in a dictionary results_train = {"loss": epoch_loss / len(iterator), "accuracy": epoch_acc / len(iterator), **confusion_matrix.to_dict() } return results_train
def download_from_url(url, save_path): """Download a file from an URL. Args: url (str): path to the URL. save_path (str): path to the saving directory. Returns: None """ response = requests.get(url, stream=True) total = response.headers.get('content-length') with open(save_path, 'wb') as f: if total is None: f.write(response.content) else: downloaded = 0 total = int(total) for data in response.iter_content(chunk_size=max(int(total / 1000), 1024 * 1024)): downloaded += len(data) f.write(data) progress_bar(downloaded, total, prefix="Downloading...")