{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":19018,"databundleVersionId":2703900,"isSourceIdPinned":false,"sourceType":"competition"},{"sourceId":11650,"sourceType":"datasetVersion","datasetId":8327}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\n\nfrom tqdm import tqdm\nfrom sklearn.model_selection import train_test_split\nimport tensorflow as tf\nfrom keras.models import Sequential\nfrom keras.layers import Activation, Dropout\n\nfrom keras.layers import BatchNormalization\n\n# from keras.utils import np_utils\nfrom sklearn import preprocessing, decomposition, model_selection,metrics, pipeline\n\n# from keras.layers import GlobalMaxPooling1d, Conv1D, MaxPooling1D, Flatten, Bidirectional, SpatialDropout1D\n\n# from keras.preprocessing import sequence, text\nfrom keras.callbacks import EarlyStopping\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\n%matplotlib inline\nfrom plotly import graph_objs as go\nimport plotly.express as px\nimport plotly.figure_factory as ff\n\nimport warnings\nwarnings.filterwarnings('ignore')\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:35:54.405874Z","iopub.execute_input":"2025-08-30T12:35:54.406574Z","iopub.status.idle":"2025-08-30T12:35:54.413857Z","shell.execute_reply.started":"2025-08-30T12:35:54.406546Z","shell.execute_reply":"2025-08-30T12:35:54.412974Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver()\n    print('Running on TPU ',tpu.master())\n\nexcept ValueError:\n    tpu = None\n\nif tpu:\n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initalize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu)\n\nelse:\n    strategy = tf.distribute.get_strategy()\n\n\nprint('REPLICAS: ',strategy.num_replicas_in_sync)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:15.619587Z","iopub.execute_input":"2025-08-30T12:37:15.619861Z","iopub.status.idle":"2025-08-30T12:37:15.626843Z","shell.execute_reply.started":"2025-08-30T12:37:15.619836Z","shell.execute_reply":"2025-08-30T12:37:15.625951Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/jigsaw-toxic-comment-train.csv')\nvalidation = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/validation.csv')\ntest = pd.read_csv('/kaggle/input/jigsaw-multilingual-toxic-comment-classification/test.csv')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:16.586187Z","iopub.execute_input":"2025-08-30T12:37:16.586453Z","iopub.status.idle":"2025-08-30T12:37:20.012109Z","shell.execute_reply.started":"2025-08-30T12:37:16.586433Z","shell.execute_reply":"2025-08-30T12:37:20.011370Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:22.118210Z","iopub.execute_input":"2025-08-30T12:37:22.118750Z","iopub.status.idle":"2025-08-30T12:37:22.125865Z","shell.execute_reply.started":"2025-08-30T12:37:22.118724Z","shell.execute_reply":"2025-08-30T12:37:22.125073Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.drop(['severe_toxic', 'obscene', 'threat',\n       'insult', 'identity_hate'],axis = 1, inplace = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:22.339617Z","iopub.execute_input":"2025-08-30T12:37:22.340292Z","iopub.status.idle":"2025-08-30T12:37:22.368703Z","shell.execute_reply.started":"2025-08-30T12:37:22.340261Z","shell.execute_reply":"2025-08-30T12:37:22.367938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.columns","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:22.507823Z","iopub.execute_input":"2025-08-30T12:37:22.508459Z","iopub.status.idle":"2025-08-30T12:37:22.513674Z","shell.execute_reply.started":"2025-08-30T12:37:22.508431Z","shell.execute_reply":"2025-08-30T12:37:22.512829Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:22.649513Z","iopub.execute_input":"2025-08-30T12:37:22.649811Z","iopub.status.idle":"2025-08-30T12:37:22.655260Z","shell.execute_reply.started":"2025-08-30T12:37:22.649788Z","shell.execute_reply":"2025-08-30T12:37:22.654308Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Reduce the training dataset size to reduce the computation","metadata":{}},{"cell_type":"code","source":"train = train.loc[:12000,:]\ntrain.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:22.950963Z","iopub.execute_input":"2025-08-30T12:37:22.951748Z","iopub.status.idle":"2025-08-30T12:37:22.957302Z","shell.execute_reply.started":"2025-08-30T12:37:22.951710Z","shell.execute_reply":"2025-08-30T12:37:22.956407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train['comment_text'].apply(lambda x: len(str(x).split())).max()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:23.090937Z","iopub.execute_input":"2025-08-30T12:37:23.091244Z","iopub.status.idle":"2025-08-30T12:37:23.150681Z","shell.execute_reply.started":"2025-08-30T12:37:23.091221Z","shell.execute_reply":"2025-08-30T12:37:23.149955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def roc_auc(prediction, traget):\n    fpr,tpr,thresholds = metrics.roc_curve(target,predictions)\n    roc_auc=metrics.auc(fpr,tpr)\n    return roc_auc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:23.249331Z","iopub.execute_input":"2025-08-30T12:37:23.249613Z","iopub.status.idle":"2025-08-30T12:37:23.254214Z","shell.execute_reply.started":"2025-08-30T12:37:23.249591Z","shell.execute_reply":"2025-08-30T12:37:23.252967Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xtrain,xvalid,ytrain,yvalid=train_test_split(train.comment_text.values,train.toxic.values, random_state=42,test_size=0.2,shuffle = True)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:25.939407Z","iopub.execute_input":"2025-08-30T12:37:25.939695Z","iopub.status.idle":"2025-08-30T12:37:25.946188Z","shell.execute_reply.started":"2025-08-30T12:37:25.939673Z","shell.execute_reply":"2025-08-30T12:37:25.945325Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yvalid.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:26.522282Z","iopub.execute_input":"2025-08-30T12:37:26.523174Z","iopub.status.idle":"2025-08-30T12:37:26.528155Z","shell.execute_reply.started":"2025-08-30T12:37:26.523141Z","shell.execute_reply":"2025-08-30T12:37:26.527422Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# Simple RNN","metadata":{}},{"cell_type":"code","source":"token = tf.keras.preprocessing.text.Tokenizer(num_words = None)\nmax_len = 1500\n\ntoken.fit_on_texts(list(xtrain) +list(xvalid))\nxtrain_seq = token.texts_to_sequences(xtrain)\nxvalid_seq = token.texts_to_sequences(xvalid)\n\nxtrain_pad = tf.keras.preprocessing.sequence.pad_sequences(xtrain_seq,maxlen=max_len)\n\nxvalid_pad = tf.keras.preprocessing.sequence.pad_sequences(xvalid_seq,maxlen=max_len)\n\nword_index = token.word_index","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:28.759648Z","iopub.execute_input":"2025-08-30T12:37:28.759944Z","iopub.status.idle":"2025-08-30T12:37:30.079085Z","shell.execute_reply.started":"2025-08-30T12:37:28.759919Z","shell.execute_reply":"2025-08-30T12:37:30.078224Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(token.fit_on_texts)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:30.442072Z","iopub.execute_input":"2025-08-30T12:37:30.442388Z","iopub.status.idle":"2025-08-30T12:37:30.447824Z","shell.execute_reply.started":"2025-08-30T12:37:30.442366Z","shell.execute_reply":"2025-08-30T12:37:30.446963Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(xtrain_seq)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:34.871155Z","iopub.execute_input":"2025-08-30T12:37:34.871454Z","iopub.status.idle":"2025-08-30T12:37:34.877288Z","shell.execute_reply.started":"2025-08-30T12:37:34.871432Z","shell.execute_reply":"2025-08-30T12:37:34.876431Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(xtrain_pad)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:37.031189Z","iopub.execute_input":"2025-08-30T12:37:37.031781Z","iopub.status.idle":"2025-08-30T12:37:37.036460Z","shell.execute_reply.started":"2025-08-30T12:37:37.031756Z","shell.execute_reply":"2025-08-30T12:37:37.035736Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(xtrain_seq[1])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:37.394531Z","iopub.execute_input":"2025-08-30T12:37:37.394796Z","iopub.status.idle":"2025-08-30T12:37:37.399957Z","shell.execute_reply.started":"2025-08-30T12:37:37.394778Z","shell.execute_reply":"2025-08-30T12:37:37.399080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xtrain_pad[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:38.489917Z","iopub.execute_input":"2025-08-30T12:37:38.490208Z","iopub.status.idle":"2025-08-30T12:37:38.496076Z","shell.execute_reply.started":"2025-08-30T12:37:38.490187Z","shell.execute_reply":"2025-08-30T12:37:38.495192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(word_index)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:38.977613Z","iopub.execute_input":"2025-08-30T12:37:38.977979Z","iopub.status.idle":"2025-08-30T12:37:38.983759Z","shell.execute_reply.started":"2025-08-30T12:37:38.977953Z","shell.execute_reply":"2025-08-30T12:37:38.983028Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\nmodel = Sequential([tf.keras.layers.Embedding(len(word_index)+1,300,input_shape = (1500,)),\n                     tf.keras.layers.SimpleRNN(100),\n                     tf.keras.layers.Dense(1,activation='sigmoid')])\n\nmodel.compile(loss='binary_crossentropy',optimizer = 'adam',metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:39.682345Z","iopub.execute_input":"2025-08-30T12:37:39.682607Z","iopub.status.idle":"2025-08-30T12:37:39.928411Z","shell.execute_reply.started":"2025-08-30T12:37:39.682589Z","shell.execute_reply":"2025-08-30T12:37:39.927443Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:40.432459Z","iopub.execute_input":"2025-08-30T12:37:40.433411Z","iopub.status.idle":"2025-08-30T12:37:40.449247Z","shell.execute_reply.started":"2025-08-30T12:37:40.433375Z","shell.execute_reply":"2025-08-30T12:37:40.448539Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.fit(xtrain_pad,ytrain,epochs = 5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T12:37:42.677186Z","iopub.execute_input":"2025-08-30T12:37:42.677447Z","iopub.status.idle":"2025-08-30T12:57:09.070513Z","shell.execute_reply.started":"2025-08-30T12:37:42.677429Z","shell.execute_reply":"2025-08-30T12:57:09.069685Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score = model.predict(xvalid_pad)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:13:27.972211Z","iopub.execute_input":"2025-08-30T15:13:27.972525Z","iopub.status.idle":"2025-08-30T15:13:40.335779Z","shell.execute_reply.started":"2025-08-30T15:13:27.972499Z","shell.execute_reply":"2025-08-30T15:13:40.334899Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:00.991630Z","iopub.execute_input":"2025-08-30T13:10:00.992503Z","iopub.status.idle":"2025-08-30T13:10:00.998002Z","shell.execute_reply.started":"2025-08-30T13:10:00.992470Z","shell.execute_reply":"2025-08-30T13:10:00.997080Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"yvalid[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:01.243620Z","iopub.execute_input":"2025-08-30T13:10:01.243959Z","iopub.status.idle":"2025-08-30T13:10:01.249468Z","shell.execute_reply.started":"2025-08-30T13:10:01.243934Z","shell.execute_reply":"2025-08-30T13:10:01.248549Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xvalid[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:01.480335Z","iopub.execute_input":"2025-08-30T13:10:01.481026Z","iopub.status.idle":"2025-08-30T13:10:01.486319Z","shell.execute_reply.started":"2025-08-30T13:10:01.480994Z","shell.execute_reply":"2025-08-30T13:10:01.485324Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fpr, tpr, thresholds = metrics.roc_curve( yvalid,score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:01.628881Z","iopub.execute_input":"2025-08-30T13:10:01.629209Z","iopub.status.idle":"2025-08-30T13:10:01.635416Z","shell.execute_reply.started":"2025-08-30T13:10:01.629185Z","shell.execute_reply":"2025-08-30T13:10:01.634522Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_auc = metrics.roc_auc_score(yvalid,score)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:17:12.049696Z","iopub.execute_input":"2025-08-30T15:17:12.050074Z","iopub.status.idle":"2025-08-30T15:17:12.058585Z","shell.execute_reply.started":"2025-08-30T15:17:12.050048Z","shell.execute_reply":"2025-08-30T15:17:12.057788Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_auc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:17:59.174009Z","iopub.execute_input":"2025-08-30T15:17:59.174332Z","iopub.status.idle":"2025-08-30T15:17:59.180569Z","shell.execute_reply.started":"2025-08-30T15:17:59.174308Z","shell.execute_reply":"2025-08-30T15:17:59.179601Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"metrics.auc(fpr,tpr)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:02.080621Z","iopub.execute_input":"2025-08-30T13:10:02.081302Z","iopub.status.idle":"2025-08-30T13:10:02.087198Z","shell.execute_reply.started":"2025-08-30T13:10:02.081274Z","shell.execute_reply":"2025-08-30T13:10:02.086250Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.figure(figsize = (15,10))\nplt.ylabel('True Positive Rate')\nplt.xlabel('False Positive Rate')\nplt.plot(fpr,tpr,color = 'darkorange',label = f'ROC curve (area = {roc_auc:.2f}')\nplt.title('The ROC Cruve')\nplt.legend(loc='lower right')\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:04.579914Z","iopub.execute_input":"2025-08-30T13:10:04.580827Z","iopub.status.idle":"2025-08-30T13:10:04.931507Z","shell.execute_reply.started":"2025-08-30T13:10:04.580789Z","shell.execute_reply":"2025-08-30T13:10:04.930565Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Lets store the scores from different version of rnn**","metadata":{}},{"cell_type":"code","source":"score_dict = {}","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:12:37.339582Z","iopub.execute_input":"2025-08-30T15:12:37.339957Z","iopub.status.idle":"2025-08-30T15:12:37.344503Z","shell.execute_reply.started":"2025-08-30T15:12:37.339930Z","shell.execute_reply":"2025-08-30T15:12:37.343563Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict['Simple_RNN'] = roc_auc","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:18:14.578264Z","iopub.execute_input":"2025-08-30T15:18:14.578539Z","iopub.status.idle":"2025-08-30T15:18:14.582749Z","shell.execute_reply.started":"2025-08-30T15:18:14.578520Z","shell.execute_reply":"2025-08-30T15:18:14.581729Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:18:15.661007Z","iopub.execute_input":"2025-08-30T15:18:15.661297Z","iopub.status.idle":"2025-08-30T15:18:15.666643Z","shell.execute_reply.started":"2025-08-30T15:18:15.661277Z","shell.execute_reply":"2025-08-30T15:18:15.665918Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**The roc curve is more inclined toward y-axis that mean your model is doing good to predict the outcomes and if the curve is other way around, then model is failing.**","metadata":{}},{"cell_type":"markdown","source":"# Word Embeddings","metadata":{}},{"cell_type":"code","source":"embeddings_index = {}\nf = open('/kaggle/input/glove840b300dtxt/glove.840B.300d.txt','r',encoding = 'utf-8')\n\nfor line in tqdm(f):\n    values = line.split(' ')\n    word = values[0]\n    coefs = np.asarray([float(val) for val in values[1:]])\n    embeddings_index[word] = coefs\n\nf.close()\n\nprint('Found %s word vectors. '% len(embeddings_index))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:10:08.695850Z","iopub.execute_input":"2025-08-30T13:10:08.696453Z","iopub.status.idle":"2025-08-30T13:13:33.210915Z","shell.execute_reply.started":"2025-08-30T13:10:08.696426Z","shell.execute_reply":"2025-08-30T13:13:33.209865Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Code implementation of LSTM(Long-Short-Term-Memory)**","metadata":{}},{"cell_type":"code","source":"embedding_matrix = np.zeros((len(word_index)+1,300))\n\nfor word, i in tqdm(word_index.items()):\n    embedding_vector = embeddings_index.get(word)\n    if embedding_vector is not None:\n        embedding_matrix[i] = embedding_vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:13:33.213068Z","iopub.execute_input":"2025-08-30T13:13:33.213427Z","iopub.status.idle":"2025-08-30T13:13:33.884551Z","shell.execute_reply.started":"2025-08-30T13:13:33.213395Z","shell.execute_reply":"2025-08-30T13:13:33.883793Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"k  = np.zeros((len(word_index)+1,300))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:32.213104Z","iopub.execute_input":"2025-08-30T13:20:32.213492Z","iopub.status.idle":"2025-08-30T13:20:32.218063Z","shell.execute_reply.started":"2025-08-30T13:20:32.213463Z","shell.execute_reply":"2025-08-30T13:20:32.217066Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"k.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T07:09:23.348952Z","iopub.execute_input":"2025-08-30T07:09:23.349354Z","iopub.status.idle":"2025-08-30T07:09:23.359167Z","shell.execute_reply.started":"2025-08-30T07:09:23.349311Z","shell.execute_reply":"2025-08-30T07:09:23.358053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"xtrain[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:43.203347Z","iopub.execute_input":"2025-08-30T13:20:43.203941Z","iopub.status.idle":"2025-08-30T13:20:43.210011Z","shell.execute_reply.started":"2025-08-30T13:20:43.203906Z","shell.execute_reply":"2025-08-30T13:20:43.209221Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"word_index['you']","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:44.342154Z","iopub.execute_input":"2025-08-30T13:20:44.342466Z","iopub.status.idle":"2025-08-30T13:20:44.348292Z","shell.execute_reply.started":"2025-08-30T13:20:44.342441Z","shell.execute_reply":"2025-08-30T13:20:44.347366Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights = [embedding_matrix]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:44.638734Z","iopub.execute_input":"2025-08-30T13:20:44.639319Z","iopub.status.idle":"2025-08-30T13:20:44.643320Z","shell.execute_reply.started":"2025-08-30T13:20:44.639291Z","shell.execute_reply":"2025-08-30T13:20:44.642432Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(weights)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:44.862063Z","iopub.execute_input":"2025-08-30T13:20:44.862340Z","iopub.status.idle":"2025-08-30T13:20:44.867980Z","shell.execute_reply.started":"2025-08-30T13:20:44.862319Z","shell.execute_reply":"2025-08-30T13:20:44.867169Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"k =10\nfor key,i in word_index.items():\n    print(i,'-',key)\n    if k < 21:\n        k += 1\n        continue\n    else:\n        break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:45.024263Z","iopub.execute_input":"2025-08-30T13:20:45.025041Z","iopub.status.idle":"2025-08-30T13:20:45.030563Z","shell.execute_reply.started":"2025-08-30T13:20:45.025012Z","shell.execute_reply":"2025-08-30T13:20:45.029664Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"embedding_matrix[1]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:45.166596Z","iopub.execute_input":"2025-08-30T13:20:45.167468Z","iopub.status.idle":"2025-08-30T13:20:45.182100Z","shell.execute_reply.started":"2025-08-30T13:20:45.167435Z","shell.execute_reply":"2025-08-30T13:20:45.180979Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vec = embeddings_index.get('you')","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:45.375747Z","iopub.execute_input":"2025-08-30T13:20:45.376541Z","iopub.status.idle":"2025-08-30T13:20:45.380217Z","shell.execute_reply.started":"2025-08-30T13:20:45.376513Z","shell.execute_reply":"2025-08-30T13:20:45.379293Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type([embedding_matrix])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:48.179133Z","iopub.execute_input":"2025-08-30T13:20:48.179730Z","iopub.status.idle":"2025-08-30T13:20:48.185350Z","shell.execute_reply.started":"2025-08-30T13:20:48.179706Z","shell.execute_reply":"2025-08-30T13:20:48.184693Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vec.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:48.382485Z","iopub.execute_input":"2025-08-30T13:20:48.382760Z","iopub.status.idle":"2025-08-30T13:20:48.389204Z","shell.execute_reply.started":"2025-08-30T13:20:48.382741Z","shell.execute_reply":"2025-08-30T13:20:48.388410Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model1 = tf.keras.Sequential([tf.keras.layers.Embedding(len(word_index)+1,300,weights = [embedding_matrix],input_shape=(1500,),trainable=False),\n                             tf.keras.layers.LSTM(100,dropout=0.3),\n                             tf.keras.layers.Dense(1,activation = 'sigmoid')])\n\nmodel1.compile(loss = 'binary_crossentropy',optimizer = 'adam',metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:48.550007Z","iopub.execute_input":"2025-08-30T13:20:48.550543Z","iopub.status.idle":"2025-08-30T13:20:49.010030Z","shell.execute_reply.started":"2025-08-30T13:20:48.550519Z","shell.execute_reply":"2025-08-30T13:20:49.009297Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model1.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:49.011194Z","iopub.execute_input":"2025-08-30T13:20:49.011460Z","iopub.status.idle":"2025-08-30T13:20:49.032454Z","shell.execute_reply.started":"2025-08-30T13:20:49.011433Z","shell.execute_reply":"2025-08-30T13:20:49.031622Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model1.fit(xtrain_pad,ytrain,epochs = 5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:20:49.033390Z","iopub.execute_input":"2025-08-30T13:20:49.033661Z","iopub.status.idle":"2025-08-30T13:45:19.270476Z","shell.execute_reply.started":"2025-08-30T13:20:49.033635Z","shell.execute_reply":"2025-08-30T13:45:19.269491Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score1 = model1.predict(xvalid_pad)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:08:22.653529Z","iopub.execute_input":"2025-08-30T15:08:22.654176Z","iopub.status.idle":"2025-08-30T15:08:53.466727Z","shell.execute_reply.started":"2025-08-30T15:08:22.654131Z","shell.execute_reply":"2025-08-30T15:08:53.465966Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"fpr,tpr,thresholds = metrics.roc_curve(yvalid,score1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:10:31.566663Z","iopub.execute_input":"2025-08-30T15:10:31.567022Z","iopub.status.idle":"2025-08-30T15:10:31.575814Z","shell.execute_reply.started":"2025-08-30T15:10:31.566997Z","shell.execute_reply":"2025-08-30T15:10:31.574961Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_score1 = metrics.roc_auc_score(yvalid,score1)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:10:45.798335Z","iopub.execute_input":"2025-08-30T15:10:45.798665Z","iopub.status.idle":"2025-08-30T15:10:45.806507Z","shell.execute_reply.started":"2025-08-30T15:10:45.798640Z","shell.execute_reply":"2025-08-30T15:10:45.805824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_score1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:10:48.532253Z","iopub.execute_input":"2025-08-30T15:10:48.532548Z","iopub.status.idle":"2025-08-30T15:10:48.538462Z","shell.execute_reply.started":"2025-08-30T15:10:48.532526Z","shell.execute_reply":"2025-08-30T15:10:48.537571Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.plot(fpr,tpr)\nplt.show()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:49:14.523204Z","iopub.execute_input":"2025-08-30T13:49:14.524020Z","iopub.status.idle":"2025-08-30T13:49:14.656741Z","shell.execute_reply.started":"2025-08-30T13:49:14.523989Z","shell.execute_reply":"2025-08-30T13:49:14.655913Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict['LSTM']=roc_score1","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:19:10.543771Z","iopub.execute_input":"2025-08-30T15:19:10.544102Z","iopub.status.idle":"2025-08-30T15:19:10.549093Z","shell.execute_reply.started":"2025-08-30T15:19:10.544078Z","shell.execute_reply":"2025-08-30T15:19:10.547710Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:19:23.607645Z","iopub.execute_input":"2025-08-30T15:19:23.607983Z","iopub.status.idle":"2025-08-30T15:19:23.613911Z","shell.execute_reply.started":"2025-08-30T15:19:23.607955Z","shell.execute_reply":"2025-08-30T15:19:23.612986Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"# GRU( Grated Recurrent Unit) ","metadata":{}},{"cell_type":"code","source":"model3 = tf.keras.Sequential([\n    tf.keras.layers.Embedding(len(word_index)+1,300,weights = [embedding_matrix],input_shape=(1500,),trainable = False),\n    tf.keras.layers.SpatialDropout1D(0.3),\n    tf.keras.layers.GRU(300),\n    tf.keras.layers.Dense(1,activation = 'sigmoid')\n])\nmodel3.compile(loss = 'binary_crossentropy',optimizer = 'adam',metrics = ['accuracy'])","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:49:22.496785Z","iopub.execute_input":"2025-08-30T13:49:22.497104Z","iopub.status.idle":"2025-08-30T13:49:22.839404Z","shell.execute_reply.started":"2025-08-30T13:49:22.497081Z","shell.execute_reply":"2025-08-30T13:49:22.838726Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model3.summary()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:49:26.818654Z","iopub.execute_input":"2025-08-30T13:49:26.819080Z","iopub.status.idle":"2025-08-30T13:49:26.835668Z","shell.execute_reply.started":"2025-08-30T13:49:26.819045Z","shell.execute_reply":"2025-08-30T13:49:26.834938Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model3.fit(xtrain_pad,ytrain,epochs = 5)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T13:49:40.463961Z","iopub.execute_input":"2025-08-30T13:49:40.464285Z","iopub.status.idle":"2025-08-30T15:01:15.212773Z","shell.execute_reply.started":"2025-08-30T13:49:40.464261Z","shell.execute_reply":"2025-08-30T15:01:15.211649Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score3 = model3.predict(xvalid_pad)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:20:23.978564Z","iopub.execute_input":"2025-08-30T15:20:23.978877Z","iopub.status.idle":"2025-08-30T15:21:52.779282Z","shell.execute_reply.started":"2025-08-30T15:20:23.978852Z","shell.execute_reply":"2025-08-30T15:21:52.778383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_score3 = metrics.roc_auc_score(yvalid,score3)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:22:29.233487Z","iopub.execute_input":"2025-08-30T15:22:29.233777Z","iopub.status.idle":"2025-08-30T15:22:29.241873Z","shell.execute_reply.started":"2025-08-30T15:22:29.233747Z","shell.execute_reply":"2025-08-30T15:22:29.240874Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"roc_score3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:22:57.036667Z","iopub.execute_input":"2025-08-30T15:22:57.037394Z","iopub.status.idle":"2025-08-30T15:22:57.043184Z","shell.execute_reply.started":"2025-08-30T15:22:57.037361Z","shell.execute_reply":"2025-08-30T15:22:57.042323Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict['GRU(RNN)'] = roc_score3","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:25:31.944250Z","iopub.execute_input":"2025-08-30T15:25:31.944855Z","iopub.status.idle":"2025-08-30T15:25:31.949007Z","shell.execute_reply.started":"2025-08-30T15:25:31.944828Z","shell.execute_reply":"2025-08-30T15:25:31.948141Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"score_dict","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-08-30T15:25:42.213410Z","iopub.execute_input":"2025-08-30T15:25:42.213688Z","iopub.status.idle":"2025-08-30T15:25:42.219090Z","shell.execute_reply.started":"2025-08-30T15:25:42.213666Z","shell.execute_reply":"2025-08-30T15:25:42.218380Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"","metadata":{"trusted":true},"outputs":[],"execution_count":null}]}