<jupyter_start><jupyter_text><jupyter_code>!pip3 install ktrain
import os
import numpy as np
import tensorflow as tf
import ktrain
from ktrain import text<jupyter_output><empty_output><jupyter_text>Data Preprocessing : IMDB dataset<jupyter_code>dataset = tf.keras.utils.get_file(fname="aclImdb_v1.tar.gz", 
                                  origin = "http://ai.stanford.edu/~amaas/data/sentiment/aclImdb_v1.tar.gz",
                                  extract=True)
IMDB_DATADIR = os.path.join(os.path.dirname(dataset), 'aclImdb')
print(os.path.dirname(dataset))
print(IMDB_DATADIR)
#train test splits

(X_train, y_train), (X_test, y_test), preproc = text.texts_from_folder(datadir = IMDB_DATADIR,
                                                                       classes=['pos','neg'],
                                                                       maxlen=500,
                                                                       train_test_names=['train','test'],
                                                                       preprocess_mode='bert')
model = text.text_classifier(name='bert',
                             train_data = (X_train,y_train),
                             preproc = preproc)<jupyter_output>Is Multi-Label? False
maxlen is 500
done.<jupyter_text>Training the BERT model<jupyter_code>learner = ktrain.get_learner(model=model,
                             train_data = (X_train,y_train),
                             val_data = (X_test,y_test),
                             batch_size=6)
learner.lr_find()
learner.lr_plot()<jupyter_output>simulating training for different learning rates... this may take a few moments...
Epoch 1/1024
  17/4167 [..............................] - ETA: 1:06:35 - loss: 0.7274 - accuracy: 0.5575<jupyter_text>the lr_finder is pretty useless for this model so I just used the hyperparameters from a blog I found online. Actually surprised that it works pretty well.<jupyter_code>learner.fit_onecycle(lr=2e-5,
                     epochs=1)<jupyter_output>begin training using onecycle policy with max lr of 2e-05...
4167/4167 [==============================] - 5516s 1s/step - loss: 0.2474 - accuracy: 0.8990 - val_loss: 0.1645 - val_accuracy: 0.9369
{'fraction_non_alphanumeric': 0.11877224199288257, 'fraction_numerical': 0.03336298932384341, 'mean_word_length': 1.8468354430379748, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '90731', 'license': 'MIT', 'n_tokens_mistral': 703, 'n_tokens_neox': 650, 'n_words': 135, 'path': 'NLP_for_sentiment_analysis.ipynb', 'repo_name': 'abbaasalif/ktrain_bert', 'size': '2248'}