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

# + [markdown] colab_type="text" id="CgOelJ11JvRF"
# ### Converting preprocessed text into vectors using fasttext embedding.

# + colab={"base_uri": "https://localhost:8080/", "height": 34} colab_type="code" executionInfo={"elapsed": 18670, "status": "ok", "timestamp": 1560094777544, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "10838423032874605851"}, "user_tz": -360} id="SXe8uEAgIRjR" outputId="b9f5eb95-28fb-461b-8668-ee9977f2495c"
from google.colab import drive
import numpy as np
import pandas as pd
drive.mount('/content/gdrive')
bengali_news_after_preprocessing = pd.read_pickle('/content/gdrive/My Drive/Projects/Bengali Text Classification/Bengali_Text_after_preprocessing.pkl')
from sklearn.externals import joblib
filename = '/content/gdrive/My Drive/Projects/Bengali Text Classification/fastText_Bangla_content_full.sav'
loaded_model = joblib.load(filename)

# + colab={} colab_type="code" id="zoutPCQLJIk0"
import keras.backend as K
import numpy as np
number_of_sample, max_number_of_words, word_vector_size = 40000, 50, 32
temp = bengali_news_after_preprocessing.loc[:number_of_sample-1,:max_number_of_words-1]

# + colab={} colab_type="code" id="NOhuZMPKKNG4"
temp = temp.replace(['ঘস', 'ফগ', 'ঝবঃ', 'ঋন', 'ঊঘ', '\u09e4', 'ওৎ', 'গথ', 'খঢ', 'ঝ’', ' ং', 'ঔ', 'ডড', 'গঘ'], None)
X = np.zeros((number_of_sample, max_number_of_words, word_vector_size), dtype=K.floatx())
for i in temp.index:
  X[i,:,:] = loaded_model.wv[temp.loc[i,:]]

# + [markdown] colab_type="text" id="0EqTbmFsLDGd"
# ### preparing labels from csv

# + colab={} colab_type="code" id="zbz6a88_KTlW"
bengali_news = pd.read_pickle('/content/gdrive/My Drive/Projects/Bengali Text Classification/40k_bangla_newspaper_article.p')
bengali_news_dataframe = pd.DataFrame(bengali_news)
y = bengali_news_dataframe['category']

# + colab={} colab_type="code" id="jENsUpfoLzj7"
from sklearn import preprocessing
import keras
import numpy as np
le = preprocessing.LabelEncoder()
le.fit(y)
enc = le.transform(y)
y = keras.utils.to_categorical(enc)

# + [markdown] colab_type="text" id="2VSBBww4YleT"
# ### Train, Validation and Test Split

# + colab={} colab_type="code" id="xFtKBeX7USSX"
from sklearn.model_selection import train_test_split
X_train, X_val, y_train, y_val = train_test_split(X, y, shuffle = True, test_size=0.125)
X_val, X_test, y_val, y_test = train_test_split(X_val, y_val, shuffle = True, test_size=0.20)

# + [markdown] colab_type="text" id="3XlcaonuzHJJ"
# ### Training CNN

# + colab={} colab_type="code" id="Xni95IcIzDA8"
from keras.models import Sequential
from keras.layers import Conv1D, Dropout, Dense, Flatten, LSTM, MaxPooling1D, Bidirectional
from keras.optimizers import Adam
from keras.callbacks import EarlyStopping, TensorBoard


model = Sequential()

model.add(Conv1D(32, kernel_size=3, activation='relu', padding='same',
                 input_shape=(max_number_of_words, word_vector_size)))
model.add(Conv1D(32, kernel_size=3, activation='relu', padding='same'))
model.add(Conv1D(32, kernel_size=3, activation='relu', padding='same'))
model.add(MaxPooling1D(pool_size=3))
model.add(Flatten())
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.2))
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.25))
model.add(Dense(256, activation='sigmoid'))
model.add(Dropout(0.25))

model.add(Dense(13, activation='softmax'))

model.compile(loss='categorical_crossentropy', optimizer=Adam(lr=0.0001, decay=1e-6), metrics=['accuracy'])

# + colab={"base_uri": "https://localhost:8080/", "height": 544} colab_type="code" executionInfo={"elapsed": 791, "status": "ok", "timestamp": 1560098458352, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "10838423032874605851"}, "user_tz": -360} id="Cq5g8SmHG97m" outputId="d4c1d563-fc1f-4648-95f4-7a30b9868602"
model.summary()

# + colab={"base_uri": "https://localhost:8080/", "height": 3434} colab_type="code" executionInfo={"elapsed": 63808, "status": "ok", "timestamp": 1560098528074, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "10838423032874605851"}, "user_tz": -360} id="mghobpf7ZmNw" outputId="d68ddbb9-95ee-4847-9f83-128474563ac5"
history = model.fit(X_train, y_train, batch_size= 500, shuffle=True, epochs= 100, validation_data=(X_val, y_val))

# + colab={} colab_type="code" id="vPP36yLOaKbO"
predicts = model.predict(X_test)
import numpy as np
def decode(le, one_hot):
    dec = np.argmax(one_hot, axis=1)
    return le.inverse_transform(dec)
y_test = decode(le, y_test)
y_preds = decode(le, predicts)

# + colab={"base_uri": "https://localhost:8080/", "height": 615} colab_type="code" executionInfo={"elapsed": 760, "status": "ok", "timestamp": 1560098542912, "user": {"displayName": "<NAME>", "photoUrl": "", "userId": "10838423032874605851"}, "user_tz": -360} id="JHNM_Wbh4rpQ" outputId="70df3a2d-2ef7-4ba1-b49f-1f2c9ffc9568"
from sklearn import metrics

print(metrics.accuracy_score(y_test, y_preds))

print(metrics.confusion_matrix(y_test, y_preds))

print(metrics.classification_report(y_test, y_preds))

# + colab={} colab_type="code" id="gg3kjzUjP1XJ"
accuracy, val_accuracy = np.array(history.history["acc"]), np.array(history.history["val_acc"])
accuracy, val_accuracy = accuracy.reshape(100,1), val_accuracy.reshape(100,1)
accuracies = np.concatenate((accuracy,val_accuracy),axis=1)
np.savetxt('/content/gdrive/My Drive/Projects/Bengali Text Classification/temp.csv',accuracies,delimiter=",")

StarcoderdataJupyterScriptsDedupFiltered