sentiment_classifier.nlp.models package

This is the package where we keep the Machine Learning models.

So far we have different modules in it:

  • model: module where the base class Model is defined. Every new Machine Learning model should inherit from it. It is an abstract class that provides the basic methods for training and making predictions.
  • shallow_networks: module where we keep the shallow networks models, such as the basic LogisticRegression, or one hidden layer neural network.
  • deep_networks: module for the deeper neural networks, like Recurrent Neural Nets or Convolutionnal ones.

Submodules

sentiment_classifier.nlp.models.deep_networks module

Code for deep neural networks models.

class sentiment_classifier.nlp.models.deep_networks.BiLSTM[source]

Bases: sentiment_classifier.nlp.models.model.Model

build_model(input_shape)[source]

Method for building the model.

Parameters:input_shape (int) – Size of the input
Returns:a keras model, to be compiled and trained
Return type:model (keras.Models)
train(reader, filepath)[source]

Method for training the model. Must be implemented by the subclasses.

Parameters:
  • reader (nlp.reader.Reader) – a Reader instance that contains the data to train the model on.
  • filepath (str) – path to where the model will be stored
Returns:

None

sentiment_classifier.nlp.models.model module

Module containing the root Model class that every new model must inherit from.

The Model class has the following attributes:

  • model: the ML model, so far built using Keras
  • tokenizer: responsible for mapping words into indices

The Model class implements the following methods:

  • build_model: builds the model
  • train: trains the model
  • save: saves the model weights & tokenizer
  • predict: predicts on sentences
  • _make_training_data: a private method that creates the train/test matrices from a Reader object
class sentiment_classifier.nlp.models.model.Model[source]

Bases: abc.ABC

build_model(input_shape)[source]

Method for building the model.

Parameters:input_shape (int) – Size of the input
Returns:a keras model, to be compiled and trained
Return type:model (keras.Models)
load(filepath)[source]

Load the model weights and tokenizer

Parameters:filepath (str) – Path where to load the model.
predict(texts, preprocessing_function)[source]

Predict on a sentence

Parameters:
  • texts (np.ndarray) – the texts to predict on
  • preprocessing_function – a preprocessing function, from nlp.preprocessing module.
Returns:

the cleaned texts

Return type:

cleaned_texts(list)

save(filepath)[source]

Save the model weights and tokenizer

Parameters:filepath (str) – Path where to store the model.
train(reader, filepath)[source]

Method for training the model. Must be implemented by the subclasses.

Parameters:
  • reader (nlp.reader.Reader) – a Reader instance that contains the data to train the model on.
  • filepath (str) – path to where the model will be stored
Returns:

None

sentiment_classifier.nlp.models.shallow_networks module

Code for shallow neural networks models.

class sentiment_classifier.nlp.models.shallow_networks.ExampleModel[source]

Bases: sentiment_classifier.nlp.models.model.Model

build_model(input_shape)[source]

Method for building the model.

Parameters:input_shape (int) – Size of the input
Returns:a keras model, to be compiled and trained
Return type:model (keras.Models)
train(reader, filepath)[source]

Method for training the model. Must be implemented by the subclasses.

Parameters:
  • reader (nlp.reader.Reader) – a Reader instance that contains the data to train the model on.
  • filepath (str) – path to where the model will be stored
Returns:

None