# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     name: python3
# ---

# + [markdown] id="view-in-github" colab_type="text"
# <a href="https://colab.research.google.com/github/MOTURUPRAVEENBHARGAV/bert_models/blob/main/BERT(ktrain)_Sentiment_Analysis.ipynb" target="_parent"><img src="https://colab.research.google.com/assets/colab-badge.svg" alt="Open In Colab"/></a>

# + [markdown] id="UcQbleiLQOdo"
# Step 1: Installing ktrain

# + id="JunyltpzEZNs" colab={"base_uri": "https://localhost:8080/"} outputId="9de0b5f8-395f-4637-9547-ea19af8ff555"
#installing ktrain
# !pip install ktrain

# + [markdown] id="Pjrc-hhkQKeg"
# Step 2: Importing modules and reading the training file

# + id="hv59UcpRTECJ" colab={"base_uri": "https://localhost:8080/"} outputId="d984cd89-d794-42cf-de60-712a385c4190"
# %%time
import time
import ktrain
from ktrain import text
import pandas as pd
from sklearn.model_selection import train_test_split
import numpy as np

# + id="NeiyRuVHTSGL" colab={"base_uri": "https://localhost:8080/", "height": 204} outputId="b23fa708-8df9-40f0-b1ef-dde06a90fe42"
data=pd.read_csv("/content/drive/My Drive/Sentiment Analysis_Preskale/Cleaned_reviews.csv",index_col="Unnamed: 0")
data.head()

# + id="O1pZvvuwTtHe" colab={"base_uri": "https://localhost:8080/"} outputId="61a5c4cd-8a94-4ef5-e063-8f6605d308be"
data=data.loc[:,["content","Polarity","clean_text"]]
data.columns

# + id="LhPyzWOSNOtd" colab={"base_uri": "https://localhost:8080/"} outputId="d7ba22cf-f76e-45b2-cb3f-a764eb006b8f"
data.Polarity.value_counts()

# + id="X7mg2iCZNUsr" colab={"base_uri": "https://localhost:8080/", "height": 124} outputId="5d4e8741-5b0e-49be-9f69-152cae9a6ffc"
# cond=[df.Polarity=="Positive",df.Polarity=="Neutral",df.Polarity=="Negative"]
# choice=[2,1,0]
# df.Polarity=np.select(cond,choice)
# df.Polarity.head()

# + id="p9eXi5dwMKfv"
# #Splitting the data into X,y

# X=df["content"]
# y=df["Polarity"]
# X_train,X_test,y_train,y_test=train_test_split(X,y,shuffle=True,test_size=0.30, random_state=1)

# + id="_-UZy7ejrnuC" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="df5df71e-c8b2-4432-c898-a1c9078d1eb5"
type(X_train)

# + id="HaEhg8wcMtpz" colab={"base_uri": "https://localhost:8080/", "height": 88} outputId="be283d2a-8192-42ad-cf4d-6ede150af8be"

# print(f"X_train: {X_train.shape}")
# print(f"y_train: {y_train.shape}")
# print(f"X_test: {X_test.shape}")
# print(f"y_test: {y_test.shape}")

# + [markdown] id="Kq0gTzXdPuiB"
# ####Step 3: Convert data to Features for BERT
# ktrain provides a very convenient feature of directly converting the text
# data directly into features for the model that is needed. All the text
# preprocessing steps do not need to be manually performed and will be
# taken care of by the library itself. Since we will be reading the data from
# pandas series objects, we will use the function texts_from_array .
#

# + id="6Wa6Trx5Ptwe" colab={"base_uri": "https://localhost:8080/", "height": 134} outputId="2c2ecd31-af0a-4ad5-a013-77e06ead440c"
(X_train_bert,y_train_bert), (X_val_bert,y_val_bert), preproc= text.texts_from_df(train_df=data,
                                                                                  text_column="content",
                                                                                  label_columns="Polarity",
                                                                                  maxlen=150,
                                                                                  preprocess_mode='bert')

# + id="mf6445Kdua5A" colab={"base_uri": "https://localhost:8080/", "height": 266} outputId="18df5eea-e25a-4f07-a651-efc5d48f4098"
X_val_bert

# + id="2rKTGlE2uyDt" colab={"base_uri": "https://localhost:8080/"} outputId="d0eda6e5-3867-4800-e95c-7975bfafe3fc"
print(len(X_val_bert))
print("Total length of the data:" ,data.shape)
print("Shape of Validation Data :" , X_val_bert[0].shape)
print("Shape of Train Data:", X_train_bert[0].shape)

# + id="91OKMNL2IRvb"


# + id="c1ok047iISDa"


# + [markdown] id="6aIuZlnqXggh"
# Step 4: Load Bert in a learner object

# + id="FN-fhXv1XV7K" colab={"base_uri": "https://localhost:8080/"} outputId="72e702b2-9473-4292-9174-0dd4e378cb08"
model = text.text_classifier(name = 'bert',
                             train_data = (X_train_bert, y_train_bert),
                             preproc = preproc)

# + [markdown] id="4SmA7SC6YPh0"
# The  function " text_classifier " loads the pre-trained BERT model with a randomly initialized final Dense layer. It is worthwhile to mention that although the final Dense layer is randomly initialized, it will not be only one getting updated during the training process. Since we have not frozen any layers and all the layers of the model are trainable, the weights of all the layers of the model will be updated during backpropagation.

# + id="H5urDluXYOai"
learner = ktrain.get_learner(model=model, train_data=(X_train_bert, y_train_bert),
                   val_data = (X_val_bert, y_val_bert),
                   batch_size =32)

# + [markdown] id="OUJEO5UoY37L"
# The function " get_learner " creates a learner object with train and validation data which can be used to fine-tune the classifier. The last argument of get_learner is the batch size. We use a small batch size of 10.

# + [markdown] id="0i2i6jGdY_kN"
# Step 5: Training (Finetuning the BERT Classifier)

# + id="h5cEoWNxYzvx"
#learner.lr_find() to find the best learning rate

# + id="9P_al4edbtsA" colab={"base_uri": "https://localhost:8080/"} outputId="31823158-4400-4681-dab2-33fd6d5fbb52"
#Essentially fit is a very basic training loop, whereas fit one cycle uses the one cycle policy callback

history=learner.fit_onecycle(lr = 2e-5, epochs = 4)

# + id="HwRj9JW4FMNg" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="2d71d102-229e-49ef-8cfe-bf38a7b39b4d"
history.history

# + id="6JKqu9oCvljE" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="27478d1b-24f9-41af-fbb1-e6b2117c2060"
type(history.history)

# + id="qz3XwfdOFmOy" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="082cc47f-0834-4563-fa67-2c393cce60c4"
history.history.keys()

# + id="RqhXas-OGa_U" colab={"base_uri": "https://localhost:8080/", "height": 195} outputId="add539d5-02e0-490a-caba-d43149103c2e"
report=learner.validate(val_data=(X_val_bert, y_val_bert),class_names=['Negative', 'Neutral','Positive'])

# + id="YjIbwTkyTV5I" colab={"base_uri": "https://localhost:8080/", "height": 70} outputId="84c53de9-5c8c-4ea1-d01f-8aa2ed593f24"
report

# + id="NJDREQM3UyY2"
#plotting confusion matrix
import matplotlib.pyplot as plt
def plot_confusion_matrix(cm, classes,normalize=False,title='BERT Confusion matrix',cmap=plt.cm.YlOrRd):
    """
    See full source and example: 
    http://scikit-learn.org/stable/auto_examples/model_selection/plot_confusion_matrix.html
    
    This function prints and plots the confusion matrix.
    Normalization can be applied by setting `normalize=True`.
    """
    plt.imshow(cm, interpolation='nearest', cmap=cmap)
    plt.title(title)
    plt.colorbar()
    tick_marks = np.arange(len(classes))
    plt.xticks(tick_marks, classes, rotation=0)
    plt.yticks(tick_marks, classes)

    if normalize:
        cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
        print("Normalized confusion matrix")
    else:
        print('Confusion matrix, without normalization')

    thresh = cm.max() / 2.
    for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
        plt.text(j, i, cm[i, j],
                 horizontalalignment="right",
                 color="White" if cm[i, j] > thresh else "black")

    plt.tight_layout()
    plt.ylabel('True label')
    plt.xlabel('Predicted label')


# + id="cjfg5xrBVICo" colab={"base_uri": "https://localhost:8080/", "height": 329} outputId="67f89728-f91b-47ef-9ca0-259843f4c5c2"
import itertools
from google.colab import files
# plt.figure(figsize=(5,5))
plot_confusion_matrix(report, classes=['Negative', 'Neutral','Positive'])
# files.download( "bert confusion matrix.jpg" )    

# + id="HViRwR7hMJlT" colab={"base_uri": "https://localhost:8080/", "height": 1000} outputId="583dd3ea-f269-4130-8239-a999fe864b94"
learner.print_layers()

# + [markdown] id="pP5XuTeIG8Mo"
# ### Getting predictor variable

# + id="OEc2qsdkFpAF"
predictor = ktrain.get_predictor(learner.model, preproc)


# + [markdown] id="nQ3-FCwHHSQR"
# #### Saving the Model

# + id="Tj0c7mhgHVHd"
predictor.save('/content/drive/My Drive/Sentiment Analysis_Preskale')

# + id="0-XK1QWhH3XE"
#sample dataset to test on
data = ['this movie was horrible, the plot was really boring. acting was okay',
 'the fild is really sucked. there is not plot and acting was bad',
 'what a beautiful movie. great plot. acting was good. will see it again']


# + id="FL-4MAh4H7hs" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="76ee4c00-b385-4183-c5e7-cc6982345e51"
predictor.predict(data)

# + id="l4vZ5IAoIU9V"
#loading the model
predictor_load = ktrain.load_predictor('/content/drive/My Drive/Sentiment Analysis_Preskale')


# + id="_RoLvY0oT1rg" colab={"base_uri": "https://localhost:8080/", "height": 35} outputId="3a0030e4-a1c5-498b-b712-e03f02312777"
predictor_load.predict(data)

# + id="ALxCIKvrD0qN"


StarcoderdataJupyterScriptsDedupFiltered