{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sn","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:17:32.971855Z","iopub.execute_input":"2023-03-31T05:17:32.972360Z","iopub.status.idle":"2023-03-31T05:17:33.985815Z","shell.execute_reply.started":"2023-03-31T05:17:32.972253Z","shell.execute_reply":"2023-03-31T05:17:33.984559Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df=pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\", usecols=['review_id','rating','review_text'])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:17:33.988246Z","iopub.execute_input":"2023-03-31T05:17:33.988701Z","iopub.status.idle":"2023-03-31T05:18:00.217129Z","shell.execute_reply.started":"2023-03-31T05:17:33.988658Z","shell.execute_reply":"2023-03-31T05:18:00.216055Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:00.218685Z","iopub.execute_input":"2023-03-31T05:18:00.219144Z","iopub.status.idle":"2023-03-31T05:18:00.241938Z","shell.execute_reply.started":"2023-03-31T05:18:00.219103Z","shell.execute_reply":"2023-03-31T05:18:00.240622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df=pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv\",usecols=['review_id','review_text'])\ntest_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:00.243630Z","iopub.execute_input":"2023-03-31T05:18:00.244497Z","iopub.status.idle":"2023-03-31T05:18:13.975700Z","shell.execute_reply.started":"2023-03-31T05:18:00.244446Z","shell.execute_reply":"2023-03-31T05:18:13.974576Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(f'Shape of train set:{df.shape}')\nprint(f'Shape of test set:{test_df.shape}')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:13.978702Z","iopub.execute_input":"2023-03-31T05:18:13.979103Z","iopub.status.idle":"2023-03-31T05:18:13.985341Z","shell.execute_reply.started":"2023-03-31T05:18:13.979069Z","shell.execute_reply":"2023-03-31T05:18:13.984058Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Checking the counts of ratings from 0-5","metadata":{}},{"cell_type":"code","source":"ax=sn.countplot(data=df,x=df.rating,color='lightpink')\nfor p in ax.patches:\n    ax.annotate('{:.1f}'.format(p.get_height()),(p.get_x()+0.1,p.get_height()+1))\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:13.986727Z","iopub.execute_input":"2023-03-31T05:18:13.987054Z","iopub.status.idle":"2023-03-31T05:18:14.653354Z","shell.execute_reply.started":"2023-03-31T05:18:13.987025Z","shell.execute_reply":"2023-03-31T05:18:14.652261Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"> Clearing the data removing stopwords performing lematizations, urls, numbers etcetra to provide more processed data to the model","metadata":{}},{"cell_type":"code","source":"df_concat = pd.concat([df.review_text,test_df.review_text]).reset_index(drop=True) #for dropping the current index and provides a fresh inc indexes\ndf_concat","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:14.655023Z","iopub.execute_input":"2023-03-31T05:18:14.655380Z","iopub.status.idle":"2023-03-31T05:18:14.752637Z","shell.execute_reply.started":"2023-03-31T05:18:14.655349Z","shell.execute_reply":"2023-03-31T05:18:14.751299Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def to_lower(text):\n    return text.lower()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:14.754314Z","iopub.execute_input":"2023-03-31T05:18:14.755000Z","iopub.status.idle":"2023-03-31T05:18:14.759714Z","shell.execute_reply.started":"2023-03-31T05:18:14.754964Z","shell.execute_reply":"2023-03-31T05:18:14.758578Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from nltk.corpus import stopwords\nstop= set(stopwords.words('english'))\ndef remove_stopwords(text):\n    \n    return ' '.join([word for word in str(text).split(' ') if word not in stop])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:14.761014Z","iopub.execute_input":"2023-03-31T05:18:14.761375Z","iopub.status.idle":"2023-03-31T05:18:15.493702Z","shell.execute_reply.started":"2023-03-31T05:18:14.761343Z","shell.execute_reply":"2023-03-31T05:18:15.492458Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\ndef remove_urls(text):\n    url = re.compile(r'https\\S+')\n    return url.sub(r' ',text)\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.495406Z","iopub.execute_input":"2023-03-31T05:18:15.496176Z","iopub.status.idle":"2023-03-31T05:18:15.502546Z","shell.execute_reply.started":"2023-03-31T05:18:15.496130Z","shell.execute_reply":"2023-03-31T05:18:15.501134Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_non_alpha(text):\n    alpha=re.compile(r'[^a-zA-Z]')\n    return alpha.sub(r' ',text)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.503982Z","iopub.execute_input":"2023-03-31T05:18:15.504424Z","iopub.status.idle":"2023-03-31T05:18:15.513697Z","shell.execute_reply.started":"2023-03-31T05:18:15.504383Z","shell.execute_reply":"2023-03-31T05:18:15.512726Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove all punctuations expect fullstop\".\"\nimport string\nremove = string.punctuation\n\nperiod = '.'\nremove = remove.replace(period, '')\ndef remove_punctuations(text):\n    pattern = re.compile(r\"[{}]\".format(re.escape(remove)))\n    \n    res = []\n    for word in text.split():\n        \n        # remove all punctiations except periods\n        new_word = pattern.sub(r' ', word)\n        new_word = new_word.strip(period)\n        \n        # keep all numbers including float ones in text\n        try:\n            float(new_word)\n        except:\n            new_word = new_word.replace(period, ' ')\n            \n        res.append(new_word)\n    \n    return ' '.join(res)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.514800Z","iopub.execute_input":"2023-03-31T05:18:15.515294Z","iopub.status.idle":"2023-03-31T05:18:15.526384Z","shell.execute_reply.started":"2023-03-31T05:18:15.515238Z","shell.execute_reply":"2023-03-31T05:18:15.525292Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def remove_spoiler_alert(text):\n    return re.sub(r'(\\(view spoiler\\).*?\\(hide spoiler\\))','',text)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.527647Z","iopub.execute_input":"2023-03-31T05:18:15.528652Z","iopub.status.idle":"2023-03-31T05:18:15.538053Z","shell.execute_reply.started":"2023-03-31T05:18:15.528610Z","shell.execute_reply":"2023-03-31T05:18:15.536573Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"MX_LEN =100\ndef processed_text(text):\n    text = to_lower(text)\n    text=remove_stopwords(text)\n    text=remove_urls(text)\n    text = remove_punctuations(text)\n    text=remove_non_alpha(text)\n    text=remove_spoiler_alert(text)\n    \n    text = ' '.join(text.split()[:MX_LEN])\n    return text","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.543026Z","iopub.execute_input":"2023-03-31T05:18:15.543435Z","iopub.status.idle":"2023-03-31T05:18:15.550776Z","shell.execute_reply.started":"2023-03-31T05:18:15.543401Z","shell.execute_reply":"2023-03-31T05:18:15.549365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_concat = df_concat.apply(processed_text)\ndf_concat","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:18:15.551917Z","iopub.execute_input":"2023-03-31T05:18:15.552302Z","iopub.status.idle":"2023-03-31T05:24:04.989803Z","shell.execute_reply.started":"2023-03-31T05:18:15.552270Z","shell.execute_reply":"2023-03-31T05:24:04.988752Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_concat[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:24:04.991291Z","iopub.execute_input":"2023-03-31T05:24:04.991728Z","iopub.status.idle":"2023-03-31T05:24:04.999400Z","shell.execute_reply.started":"2023-03-31T05:24:04.991684Z","shell.execute_reply":"2023-03-31T05:24:04.998294Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"    from nltk.stem import WordNetLemmatizer\n    wordnet_lemmatizer=WordNetLemmatizer()\n    def lemmatization(text):\n        return ' '.join(wordnet_lemmatizer.lemmatize(word) for word in text.split())\n ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:24:05.000772Z","iopub.execute_input":"2023-03-31T05:24:05.001781Z","iopub.status.idle":"2023-03-31T05:24:05.009613Z","shell.execute_reply.started":"2023-03-31T05:24:05.001738Z","shell.execute_reply":"2023-03-31T05:24:05.008466Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df_concat=df_concat.apply(lemmatization)\ndf_concat[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:24:05.011091Z","iopub.execute_input":"2023-03-31T05:24:05.012189Z","iopub.status.idle":"2023-03-31T05:30:44.958939Z","shell.execute_reply.started":"2023-03-31T05:24:05.012146Z","shell.execute_reply":"2023-03-31T05:30:44.957562Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:44.960731Z","iopub.execute_input":"2023-03-31T05:30:44.961104Z","iopub.status.idle":"2023-03-31T05:30:44.973542Z","shell.execute_reply.started":"2023-03-31T05:30:44.961061Z","shell.execute_reply":"2023-03-31T05:30:44.972223Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:44.975081Z","iopub.execute_input":"2023-03-31T05:30:44.975531Z","iopub.status.idle":"2023-03-31T05:30:44.987479Z","shell.execute_reply.started":"2023-03-31T05:30:44.975490Z","shell.execute_reply":"2023-03-31T05:30:44.986248Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow.keras.utils import to_categorical\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:44.988943Z","iopub.execute_input":"2023-03-31T05:30:44.989358Z","iopub.status.idle":"2023-03-31T05:30:52.738513Z","shell.execute_reply.started":"2023-03-31T05:30:44.989323Z","shell.execute_reply":"2023-03-31T05:30:52.737330Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y = tf.keras.utils.to_categorical(df['rating'])\nY.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:52.739986Z","iopub.execute_input":"2023-03-31T05:30:52.741325Z","iopub.status.idle":"2023-03-31T05:30:52.783561Z","shell.execute_reply.started":"2023-03-31T05:30:52.741285Z","shell.execute_reply":"2023-03-31T05:30:52.782196Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Y=df['rating']\n# Y.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:52.784851Z","iopub.execute_input":"2023-03-31T05:30:52.785177Z","iopub.status.idle":"2023-03-31T05:30:52.790453Z","shell.execute_reply.started":"2023-03-31T05:30:52.785149Z","shell.execute_reply":"2023-03-31T05:30:52.788858Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X =df_concat.iloc[:Y.shape[0]]\nX_test = df_concat.iloc[Y.shape[0]] ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:52.792297Z","iopub.execute_input":"2023-03-31T05:30:52.792972Z","iopub.status.idle":"2023-03-31T05:30:52.799127Z","shell.execute_reply.started":"2023-03-31T05:30:52.792930Z","shell.execute_reply":"2023-03-31T05:30:52.797976Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_val,Y_train,Y_val=train_test_split(X,Y,test_size=0.15, random_state=42)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:52.800258Z","iopub.execute_input":"2023-03-31T05:30:52.800561Z","iopub.status.idle":"2023-03-31T05:30:53.050600Z","shell.execute_reply.started":"2023-03-31T05:30:52.800533Z","shell.execute_reply":"2023-03-31T05:30:53.049442Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:53.051923Z","iopub.execute_input":"2023-03-31T05:30:53.052277Z","iopub.status.idle":"2023-03-31T05:30:53.059399Z","shell.execute_reply.started":"2023-03-31T05:30:53.052242Z","shell.execute_reply":"2023-03-31T05:30:53.058266Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"Y_train.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:53.061125Z","iopub.execute_input":"2023-03-31T05:30:53.061513Z","iopub.status.idle":"2023-03-31T05:30:53.070294Z","shell.execute_reply.started":"2023-03-31T05:30:53.061464Z","shell.execute_reply":"2023-03-31T05:30:53.069092Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from tensorflow.keras.preprocessing.text import Tokenizer\nfrom tensorflow.keras.preprocessing.sequence import pad_sequences","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:53.072780Z","iopub.execute_input":"2023-03-31T05:30:53.073115Z","iopub.status.idle":"2023-03-31T05:30:53.079456Z","shell.execute_reply.started":"2023-03-31T05:30:53.073085Z","shell.execute_reply":"2023-03-31T05:30:53.078408Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = Tokenizer()\ntokenizer.fit_on_texts(X_train) # fit tokenizer only on train data\n\nvocab_size = len(tokenizer.word_index)\nvocab_size","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:30:53.080745Z","iopub.execute_input":"2023-03-31T05:30:53.081081Z","iopub.status.idle":"2023-03-31T05:31:40.221267Z","shell.execute_reply.started":"2023-03-31T05:30:53.081045Z","shell.execute_reply":"2023-03-31T05:31:40.220082Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# TPU SetUP","metadata":{}},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver() \n    tf.config.experimental_connect_to_cluster(tpu)\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    \n    strategy = tf.distribute.experimental.TPUStrategy\nexcept ValueError:\n    strategy = tf.distribute.get_strategy() \n    print('Number of replicas:', strategy.num_replicas_in_sync) ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:31:40.222915Z","iopub.execute_input":"2023-03-31T05:31:40.223313Z","iopub.status.idle":"2023-03-31T05:31:40.243319Z","shell.execute_reply.started":"2023-03-31T05:31:40.223278Z","shell.execute_reply":"2023-03-31T05:31:40.242434Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"try:\n    tpu = tf.distribute.cluster_resolver.TPUClusterResolver() # TPU detection\nexcept ValueError:\n    tpu = None\n    gpus = tf.config.experimental.list_logical_devices(\"GPU\")\n    \nif tpu:\n    tf.tpu.experimental.initialize_tpu_system(tpu)\n    strategy = tf.distribute.experimental.TPUStrategy(tpu,) \n    print('Running on TPU ', tpu.cluster_spec().as_dict()['worker'])\nelif len(gpus) > 1:\n    strategy = tf.distribute.MirroredStrategy([gpu.name for gpu in gpus])\n    print('Running on multiple GPUs ', [gpu.name for gpu in gpus])\nelif len(gpus) == 1:\n    strategy = tf.distribute.get_strategy() \n    print('Running on single GPU ', gpus[0].name)\nelse:\n    strategy = tf.distribute.get_strategy() \n    print('Running on CPU')\nprint(\"Number of accelerators: \", strategy.num_replicas_in_sync)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:31:40.244784Z","iopub.execute_input":"2023-03-31T05:31:40.245363Z","iopub.status.idle":"2023-03-31T05:31:40.272883Z","shell.execute_reply.started":"2023-03-31T05:31:40.245329Z","shell.execute_reply":"2023-03-31T05:31:40.271288Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequence = tokenizer.texts_to_sequences(X_train)\ntrain_sequence = pad_sequences(train_sequence, maxlen = MX_LEN, padding = 'post')\n\nval_sequence = tokenizer.texts_to_sequences(X_val)\nval_sequence = pad_sequences(val_sequence, maxlen = MX_LEN, padding = 'post')\n\ntest_sequence = tokenizer.texts_to_sequences(X_test)\ntest_sequence = pad_sequences(test_sequence, maxlen = MX_LEN, padding = 'post')","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:31:40.275397Z","iopub.execute_input":"2023-03-31T05:31:40.275894Z","iopub.status.idle":"2023-03-31T05:32:25.712586Z","shell.execute_reply.started":"2023-03-31T05:31:40.275848Z","shell.execute_reply":"2023-03-31T05:32:25.711512Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_sequence[0]","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:25.714039Z","iopub.execute_input":"2023-03-31T05:32:25.714405Z","iopub.status.idle":"2023-03-31T05:32:25.723667Z","shell.execute_reply.started":"2023-03-31T05:32:25.714374Z","shell.execute_reply":"2023-03-31T05:32:25.722275Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_dict = {}\nwith open('../input/glove-global-vectors-for-word-representation/glove.6B.50d.txt','r') as f:\n    for line in f:\n        values = line.split()\n        word = values[0]\n        vectors = np.asarray(values[1:], 'float32')\n        embedding_dict[word] = vectors \n        ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:25.725222Z","iopub.execute_input":"2023-03-31T05:32:25.725571Z","iopub.status.idle":"2023-03-31T05:32:32.001965Z","shell.execute_reply.started":"2023-03-31T05:32:25.725540Z","shell.execute_reply":"2023-03-31T05:32:32.000589Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_dict['good']","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.003536Z","iopub.execute_input":"2023-03-31T05:32:32.004518Z","iopub.status.idle":"2023-03-31T05:32:32.014885Z","shell.execute_reply.started":"2023-03-31T05:32:32.004370Z","shell.execute_reply":"2023-03-31T05:32:32.013245Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"From the above code each word has its embedding index as a vector later create embedding matrix for each word in the code below","metadata":{}},{"cell_type":"code","source":"vocab_size_glove = vocab_size + 1\nembedding_matrix = np.zeros((vocab_size_glove, 50)) # glove.6B.100d.txt\n\nfor word, i in tokenizer.word_index.items():\n    if i > vocab_size_glove:\n        continue\n    \n    emb_vec = embedding_dict.get(word)\n    if emb_vec is not None:\n        embedding_matrix[i] = emb_vec","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.016782Z","iopub.execute_input":"2023-03-31T05:32:32.017693Z","iopub.status.idle":"2023-03-31T05:32:32.297659Z","shell.execute_reply.started":"2023-03-31T05:32:32.017655Z","shell.execute_reply":"2023-03-31T05:32:32.296440Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embedding_matrix.shape # it has matrix for all the words in the vocab","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.299451Z","iopub.execute_input":"2023-03-31T05:32:32.300327Z","iopub.status.idle":"2023-03-31T05:32:32.307508Z","shell.execute_reply.started":"2023-03-31T05:32:32.300286Z","shell.execute_reply":"2023-03-31T05:32:32.306039Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# The Model Architecture","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import Sequential\nfrom tensorflow.keras.layers import Dense, Dropout, Conv1D, MaxPooling1D, Embedding, GlobalMaxPooling1D, Flatten\nfrom tensorflow.keras.initializers import Constant\nfrom tensorflow.keras.optimizers import Adam\nfrom tensorflow.keras import regularizers\n\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.309019Z","iopub.execute_input":"2023-03-31T05:32:32.309415Z","iopub.status.idle":"2023-03-31T05:32:32.319228Z","shell.execute_reply.started":"2023-03-31T05:32:32.309381Z","shell.execute_reply":"2023-03-31T05:32:32.318165Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model=Sequential()\nmodel.add(Embedding(vocab_size_glove,\n                   50, input_length=MX_LEN,\n                   embeddings_initializer=Constant(embedding_matrix),trainable=False))\nmodel.add(Conv1D(64, 3, activation='relu', padding='same'))\nmodel.add(Conv1D(64,3,activation='relu',padding='same'))\nmodel.add(MaxPooling1D(2))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv1D(128, 5, activation='relu', padding='same'))\nmodel.add(Conv1D(128,5,activation='relu',padding='same'))\nmodel.add(MaxPooling1D(3))\nmodel.add(Dropout(0.3))\n\nmodel.add(Conv1D(128, 5, activation='relu', padding='same'))\nmodel.add(Conv1D(128,5,activation='relu',padding='same'))\nmodel.add(MaxPooling1D(3))\nmodel.add(Dropout(0.3))\n\nmodel.add(GlobalMaxPooling1D())\nmodel.add(Dense(32, activation='relu'))\nmodel.add(Dropout(0.2))\nmodel.add(Dense(Y.shape[1], activation = 'softmax'))\n\nmodel.compile(Adam(learning_rate = 1e-3),\n              loss = 'categorical_crossentropy', \n              metrics=['accuracy'])\n\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.320593Z","iopub.execute_input":"2023-03-31T05:32:32.320932Z","iopub.status.idle":"2023-03-31T05:32:32.688440Z","shell.execute_reply.started":"2023-03-31T05:32:32.320902Z","shell.execute_reply":"2023-03-31T05:32:32.687131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Call Backs**","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.callbacks import EarlyStopping, ReduceLROnPlateau\nearly_stop = EarlyStopping(patience = 5, restore_best_weights = True, verbose = 1)\nreduce_lr = ReduceLROnPlateau(factor = 0.1, patience = 2, mode = 'min', verbose = 1)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.690134Z","iopub.execute_input":"2023-03-31T05:32:32.690489Z","iopub.status.idle":"2023-03-31T05:32:32.697062Z","shell.execute_reply.started":"2023-03-31T05:32:32.690458Z","shell.execute_reply":"2023-03-31T05:32:32.695737Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**Fitting the model**","metadata":{}},{"cell_type":"code","source":"history = model.fit(train_sequence,\n                   Y_train,\n                   batch_size=128,\n                   epochs=20,\n                   verbose=1,\n                   validation_data=(val_sequence,Y_val),\n                   callbacks=[reduce_lr, early_stop])","metadata":{"execution":{"iopub.status.busy":"2023-03-31T05:32:32.707047Z","iopub.execute_input":"2023-03-31T05:32:32.707583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"loss = history.history['loss']\nacc = history.history['accuracy']\nval_loss = history.history['val_loss']\nval_acc = history.history['val_accuracy']\n\nepochs = range(1, len(loss) + 1)\n\nplt.figure(figsize=(16, 5))\n\n#accuracy\nplt.subplot(1,2,1)\nplt.plot(epochs, acc, 'bo', label = 'Training accuracy')\nplt.plot(epochs, val_acc, 'r', label = 'Validation accuracy')\nplt.legend()\n\n#loss\nplt.subplot(1,2,2)\nplt.plot(epochs, loss, 'bo', label = 'Trainig loss')\nplt.plot(epochs, val_loss, 'r', label = 'Validation loss')\nplt.legend()\n\nplt.show()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission=pd.DataFrame()\nsubmission['review_id']=test_df['review_id']\nsubmission['rating']=pd.Series(np.argmax(i) for i in model.predict(test_sequence))\nsubmission.apply(lambda col: col.drop_duplicates().reset_index(drop=True))\nsubmission.head()","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"submission.to_csv(\"Submission.csv\",index=False)","metadata":{"trusted":true},"execution_count":null,"outputs":[]}]}