Skip to main content

Text Classifier using LSTM, GRU, and Transformer BERT

Project description

Text Classifier using LSTM, GRU, and Transformer BERT

Install Package

!pip install databits

Data preparation

Prepare the data X_train, y_train and X_test, y_test in list form.
X_train -> list (text)
X_test -> list (text)
y_train -> lits label (integer starts from 1)
y_test -> lits label (integer starts from 1)

Define Hyperparameters

import torch
import torch.nn as nn
import numpy as np
from databits import CreateModel
from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, accuracy_score

BATCH_SIZE = 32
SEQUENCE_LENGTH = 100
EPOCHS = 5
EMBED_DIM = 512
N_LAYERS = 2
DROPOUT_RATE = 0.1
NUM_CLASSES = len(np.unique(np.array(y_train)))
OPTIMIZER = torch.optim.Adam
LR = 0.001
LOSS = nn.CrossEntropyLoss

Define Model

model = CreateModel(X_train, y_train,
                 X_test, y_test,
                 batch=BATCH_SIZE,
                 seq=SEQUENCE_LENGTH,
                 embedding_dim=EMBED_DIM,
                 n_layers=N_LAYERS,
                 dropout_rate=DROPOUT_RATE,
                 num_classes=NUM_CLASSES)

Train Model

model.LSTM() # lstm model
model.GRU() # gru model
model.TRANSFORMER() # tranformer model
model.BERT() # bert model
model.FASTTEXT() # fasttext model

example, use gru model:

model.GRU()
history = model.fit(epochs=EPOCHS, optimizer=OPTIMIZER, lr=LR, loss=LOSS)

example, use bert model:

model.BERT()
history = model.fit(epochs=EPOCHS, optimizer=OPTIMIZER, lr=LR, loss=LOSS)

Get y_true and predict label

y_true, y_pred = model.eval() # no argumen needed

Compute Accuracy, Precisiom, Recall, F1, and Cofusion Matrix

from sklearn.metrics import confusion_matrix, precision_score, recall_score, f1_score, accuracy_score

precision = precision_score(y_true, y_pred, average='macro')
recall = recall_score(y_true, y_pred, average='macro')
f1 = f1_score(y_true, y_pred, average='macro')
accuracy = accuracy_score(y_true, y_pred)

print(f"Precision: {precision:.4f}")
print(f"Recall: {recall:.4f}")
print(f"F1 Score: {f1:.4f}")
print(f"Akurasi: {accuracy:.4f}")

cm = confusion_matrix(y_true, y_pred)
print(cm)

Inference

text = "this is text"
pred = model.predict(text) # or
pred = model(text)
print(pred) # text label in int format

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

databits-2.0.1.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

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

databits-2.0.1-py3-none-any.whl (22.5 kB view details)

Uploaded Python 3

File details

Details for the file databits-2.0.1.tar.gz.

File metadata

  • Download URL: databits-2.0.1.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.13

File hashes

Hashes for databits-2.0.1.tar.gz
Algorithm Hash digest
SHA256 be65d591175fa68d050eb8008b07afcff5cd183651c7016cca3215db9598e08c
MD5 984a2b6a48b40e08887160a6d0ac2192
BLAKE2b-256 cd4c4eaa99eee74607fdabae3c95cdc1e0f441b8cce58bbbd3c93df6fa93e0bc

See more details on using hashes here.

File details

Details for the file databits-2.0.1-py3-none-any.whl.

File metadata

  • Download URL: databits-2.0.1-py3-none-any.whl
  • Upload date:
  • Size: 22.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.9.13

File hashes

Hashes for databits-2.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b21cbc08356ec8011035b58dfdef3b37bcb4725793fbba0fa6a3ce16ec857cbb
MD5 b81f5e9f7c39fd077a6da3dd72e6d3f6
BLAKE2b-256 84213eb474019567001a4f33215af3a75d5664fc484250aedba1a09b8b1317b5

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