Data module

class, vocab_size=3000)

NewsDataset class of different newspaper. The class heritates from class.


Save the text_vectorizer method used to transform the dataset into Bag of Words tensors. This file is saved with a binary pickle format.

Parameters:filename (str, optional) – Name of the text vectorizer file. The default is “vectorizer.pickle”.
Return type:None.

Tokenize a text and extract only the words.

Parameters:sample (list) – Text sample to tokenize.
Returns:sample – Tokenized text, containing only words.
Return type:list

Model module

class pynews.model.NewsModel(*layers_size)

Newspaper Model.


Predict the outputs from the given inputs. The ReLU function is used from the inputs to the last hidden layer, then a linear function from the last hidden layer to the outputs.

Parameters:inputs (torch tensor) – Inputs tensor.
Returns:outputs – Predicted tensor.
Return type:torch tensor

Trainer module

class pynews.trainer.Trainer(model, train_loader, model_name='model')

Train a model with a dataset.

run(criterion, optimizer, epochs, lr)

Method to run the trainer. Use this function to train your model with your data.

  • criterion (torch.nn.modules.loss) – The loss function.
  • optimizer (torch.optim) – Method to optimize the model.
  • epochs (int) – Number of iterations.
  • lr (float) – Learning rate.

train_losses – List of size epochs, containing

Return type:


Eval module


Analyse a confusion matrix by printing the True Positive (TP), False Positive (FP), … and the specificity and sensitivity as well for each classes.

Parameters:confusion_matrix (torch tensor of size (number_of classes, number_of_classes)) – The confusion matrix computed on the test data.
Return type:None.
pynews.eval.eval_func(batched_data, model)

Evaluate the model on the test data.

  • batched_data (torch DataLoader) – The loaded test data.
  • model (torch Model) – The PyTorch model to evaluate.

  • accuracy (float) – Global accuracy of the model.
  • predicted (torch tensor) – Predicted output.
  • gold_label (torch tensor) – Truth output.
  • confusion_matrix (torch tensor) – Confusion matrix.