Skip to main content

A package to build neural networks based on two separate text fields.

Project description

Alt text

Fine-tune a neural network with two text modes of tunable weights

pip install twomodalbert

Introduction

Let's consider whether we want to classify the below sample as positive or negative.

context text
This product is horrible, which I didn't expect. Its website is just incredible!

When only the message is considered, you can see its sentiment is positive due to the word 'incredible'. However, when the context is considered, sentiment will be rather negative.

The TwoModalBERT package allows us to quickly run an experiment with the two-modal neural network architecture described in this section. It allows quickly constructing a model on top of PyTorch and transformers libraries and enables experimenting with weights of two input texts. So, how to use the package?

Usage

  1. First, create a config.ini file in your working directory (parameters are described here).

    [GENERAL]
     EPOCHS = 3
     RANDOM_SEED = 42
     BATCH_SIZE = 16
     MAX_SEQ_LEN = 200
     NUM_WORKERS = 2
     PRETRAINED_MODEL_NAME_OR_PATH = bert-base-uncased
     MODEL_SAVE_PATH = best_model_state.bin
    
  2. Read the file.

     from configparser import ConfigParser
     config = ConfigParser()
     config.read("config.ini")
    
  3. Set the device.

    os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    

Train

  1. initialize DataPreparation and Trainer modules.

    from twomodalbert.DataPreparation import TwoModalDapaPreparation
    from twomodalbert.Trainer import TwoModalBertTrainer
    
    DataPreparation = TwoModalDataPreparation(config=config)
    Trainer = TwoModalBertTrainer(device=DEVICE, config=config)
    
  2. Split input df with text, context, label columns and create data loaders.

    (
        train_data_loader,
        train,
        val_data_loader,
        val,
        test_data_loader,
        test,
    ) = DataPreparation.prepare_data(
        df,
        text_column="text",
        context_column="context",
        label_column="label",
        train_size=0.8,
        val_size=0.1,
    )
    
  3. Train the model (nn parameters described in this section)

    model, history = Trainer.train_model(
        train_data_loader,
        train,
        val_data_loader,
        val,
        text_size=100,
        context_size=50,
        binary=False,
        text_p=0.3,
        context_p=0.3,
        output_p=0.3,
    
    )
    

Predict

  1. Load the model.

    model = TwoModalBERTModel(
        text_size=100,
        context_size=50,
        binary=False,
        text_p=0.3,
        context_p=0.3,
        output_p=0.3,
    )
    
    model.load_state_dict(torch.load(config["GENERAL"]["MODEL_SAVE_PATH"]))
    
  2. Evaluate it on a test set (choose any metric from sklearn).

    y_pred, y_test = test_model(model, test_data_loader, DEVICE)
    
  3. Run the model on two text inputs

    text = "Its website is just incredible!"
    
    context = "This product is horrible, which I didn't expect."
    
    predict_on_text(model, text, context, DEVICE)
    

Neural network architecture

Below you can find how TwoModalBERT is constructed and what are the class parameters.

Alt text

First of all, on top of the last BERT layer, a linear layer is added. To be precise, it is added on top of the CLS token. As the CLS token aggregates the entire sequence representation, it is often used in a classification task.

The linear layer transforms input features with hidden size relevant to the BERT model, which is usually 768 for the models available in the transformers package, into features with hidden size equal to predefined context_size and text_size.

In the next step, the dropout layers with probabilities context_p and text_p are added on the top. Why? Because it makes the neural network less sensitive to the specific weights of neurons and not prone to overfitting.

Finally, both branches created similarly are combined and followed by another dropout layer of output_p and an activation function (Sigmoid if binary, else Softmax).

Parameters in config.ini

Settings to be defined in the config.ini file:

Variable Description Default Value
EPOCHS the number of complete passes through the training dataset 3
RANDOM_SEED a number used to initialize a pseudorandom number generator 42
BARCH_SIZE the number of training samples to work through before the model’s internal parameters are updated 16
MAX_SEQ_LEN the maximum length in number of tokens for the inputs to the transformer model 100
NUM_WORKERS the number of processes that generate batches in parallel 2
PRETRAINED_MODEL_NAME_OR_PATH of a pre-trained model configuration to load from cache or download (equivalent to transformers) bert-base-uncased
MODEL_SAVE_PATH the model save path best_model.bin

Requirenments

  • configparser-5.3.0

  • scikit-learn-1.0.2

  • torch-1.12.1+cu113

  • transformers-4.24.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twomodalbert-0.0.1.tar.gz (241.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twomodalbert-0.0.1-py3-none-any.whl (9.2 kB view details)

Uploaded Python 3

File details

Details for the file twomodalbert-0.0.1.tar.gz.

File metadata

  • Download URL: twomodalbert-0.0.1.tar.gz
  • Upload date:
  • Size: 241.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for twomodalbert-0.0.1.tar.gz
Algorithm Hash digest
SHA256 95ac66f6d22ebe4d5c06465ec17166fd53b617760abf6e401ac37ddc0b8bac20
MD5 3288dd0386601cbddcb441670d5409c2
BLAKE2b-256 406ad1e0074b76bdbea3e4fdd893524d9b38647d0d7c9984236f33413c84b412

See more details on using hashes here.

File details

Details for the file twomodalbert-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: twomodalbert-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.7.3

File hashes

Hashes for twomodalbert-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3e6d11001b787195c45200b5f5f3ca79e8cc81c8ee1f86420da075040c311bd8
MD5 c18467063a2a1dd151071bfefb3d242e
BLAKE2b-256 5843e9864b090fa4bd5a89035efcd7380a9c6f6cfd3fb4954c96fa39ad035d40

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page