{"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":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-02-04T13:37:08.806465Z","iopub.execute_input":"2023-02-04T13:37:08.807599Z","iopub.status.idle":"2023-02-04T13:37:08.825433Z","shell.execute_reply.started":"2023-02-04T13:37:08.807483Z","shell.execute_reply":"2023-02-04T13:37:08.824240Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Data Reading and basic text preprocessing ","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:08.828344Z","iopub.execute_input":"2023-02-04T13:37:08.828776Z","iopub.status.idle":"2023-02-04T13:37:24.976054Z","shell.execute_reply.started":"2023-02-04T13:37:08.828741Z","shell.execute_reply":"2023-02-04T13:37:24.974927Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = df[[\"rating\",\"review_text\"]]\ndf","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:24.977495Z","iopub.execute_input":"2023-02-04T13:37:24.977971Z","iopub.status.idle":"2023-02-04T13:37:25.019427Z","shell.execute_reply.started":"2023-02-04T13:37:24.977932Z","shell.execute_reply":"2023-02-04T13:37:25.018363Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.shape","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:25.021212Z","iopub.execute_input":"2023-02-04T13:37:25.021645Z","iopub.status.idle":"2023-02-04T13:37:25.029455Z","shell.execute_reply.started":"2023-02-04T13:37:25.021608Z","shell.execute_reply":"2023-02-04T13:37:25.028344Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:25.033193Z","iopub.execute_input":"2023-02-04T13:37:25.034508Z","iopub.status.idle":"2023-02-04T13:37:25.489998Z","shell.execute_reply.started":"2023-02-04T13:37:25.034472Z","shell.execute_reply":"2023-02-04T13:37:25.488822Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sns.countplot(data=df,x='rating')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:25.491588Z","iopub.execute_input":"2023-02-04T13:37:25.491951Z","iopub.status.idle":"2023-02-04T13:37:25.788846Z","shell.execute_reply.started":"2023-02-04T13:37:25.491913Z","shell.execute_reply":"2023-02-04T13:37:25.787828Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:25.790249Z","iopub.execute_input":"2023-02-04T13:37:25.790954Z","iopub.status.idle":"2023-02-04T13:37:25.807007Z","shell.execute_reply.started":"2023-02-04T13:37:25.790912Z","shell.execute_reply":"2023-02-04T13:37:25.805812Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df['rating'] = df.rating.astype(str)\ndf.dtypes ","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:25.809036Z","iopub.execute_input":"2023-02-04T13:37:25.810071Z","iopub.status.idle":"2023-02-04T13:37:26.284428Z","shell.execute_reply.started":"2023-02-04T13:37:25.810032Z","shell.execute_reply":"2023-02-04T13:37:26.283305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:26.286192Z","iopub.execute_input":"2023-02-04T13:37:26.286872Z","iopub.status.idle":"2023-02-04T13:37:26.440623Z","shell.execute_reply.started":"2023-02-04T13:37:26.286832Z","shell.execute_reply":"2023-02-04T13:37:26.439534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import re\nimport string","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:26.443348Z","iopub.execute_input":"2023-02-04T13:37:26.444163Z","iopub.status.idle":"2023-02-04T13:37:26.448860Z","shell.execute_reply.started":"2023-02-04T13:37:26.444132Z","shell.execute_reply":"2023-02-04T13:37:26.447365Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# remove all characters not number or characters\ndef cleanText(input_string):\n    modified_string = re.sub('[^A-Za-z0-9]+', ' ', input_string)\n    modified_string = re.sub('[0-9]+', ' ', modified_string)\n    modified_string=re.sub(\"[@]\",\"\",modified_string)\n    return(modified_string)\ndf['review_text'] = df.review_text.apply(cleanText)\ndf['review_text'][150]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:37:26.450613Z","iopub.execute_input":"2023-02-04T13:37:26.451053Z","iopub.status.idle":"2023-02-04T13:38:40.210113Z","shell.execute_reply.started":"2023-02-04T13:37:26.451013Z","shell.execute_reply":"2023-02-04T13:38:40.208985Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Remove non printable characters\ndef remove_not_ASCII(text):\n    text = ''.join([word for word in text if word in string.printable])\n    return text\ndf['review_text'] = df.review_text.apply(remove_not_ASCII)\ndf['review_text'][150]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:38:40.211747Z","iopub.execute_input":"2023-02-04T13:38:40.212459Z","iopub.status.idle":"2023-02-04T13:40:26.106555Z","shell.execute_reply.started":"2023-02-04T13:38:40.212417Z","shell.execute_reply":"2023-02-04T13:40:26.105376Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df.sample(10)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:26.108232Z","iopub.execute_input":"2023-02-04T13:40:26.108930Z","iopub.status.idle":"2023-02-04T13:40:26.224011Z","shell.execute_reply.started":"2023-02-04T13:40:26.108890Z","shell.execute_reply":"2023-02-04T13:40:26.222999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#converting to lower case\ndf['review_text']=df['review_text'].str.lower()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:26.229034Z","iopub.execute_input":"2023-02-04T13:40:26.229542Z","iopub.status.idle":"2023-02-04T13:40:27.558054Z","shell.execute_reply.started":"2023-02-04T13:40:26.229511Z","shell.execute_reply":"2023-02-04T13:40:27.556922Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#removing punctuations\ndf['review_text']=df['review_text'].str.translate(str.maketrans('','',string.punctuation))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:27.559670Z","iopub.execute_input":"2023-02-04T13:40:27.560772Z","iopub.status.idle":"2023-02-04T13:40:31.081406Z","shell.execute_reply.started":"2023-02-04T13:40:27.560726Z","shell.execute_reply":"2023-02-04T13:40:31.080355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nltk\nimport re\nimport string\nfrom nltk.corpus import stopwords\nfrom nltk import word_tokenize\nnltk.download('punkt')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:31.083040Z","iopub.execute_input":"2023-02-04T13:40:31.083468Z","iopub.status.idle":"2023-02-04T13:40:31.429974Z","shell.execute_reply.started":"2023-02-04T13:40:31.083429Z","shell.execute_reply":"2023-02-04T13:40:31.428861Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"nltk.download('stopwords')","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:31.431704Z","iopub.execute_input":"2023-02-04T13:40:31.432083Z","iopub.status.idle":"2023-02-04T13:40:31.441458Z","shell.execute_reply.started":"2023-02-04T13:40:31.432045Z","shell.execute_reply":"2023-02-04T13:40:31.440446Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"stopWords=stopwords.words('english')+['the', 'a', 'k','h','g','an', 'i', 'he', 'she', 'they', 'to', 'of', 'it', 'n','from','u','w','p','x','c','r','co','http','https','b','e','v','l','q']\ndef removeStopWords(stopWords, rvw_txt):\n    newtxt = ' '.join([word for word in rvw_txt.split() if word not in stopWords])\n    return newtxt\ndf['review_text'] = [removeStopWords(stopWords,x) for x in df['review_text']]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:40:31.442504Z","iopub.execute_input":"2023-02-04T13:40:31.442852Z","iopub.status.idle":"2023-02-04T13:46:33.266813Z","shell.execute_reply.started":"2023-02-04T13:40:31.442815Z","shell.execute_reply":"2023-02-04T13:46:33.265384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#removing hyperlink\ndf['review_text']=df['review_text'].apply(lambda x:re.sub('https?://\\S+|www\\.\\S+', '', x) )\n#remove words containing numbers\ndf['review_text']=df['review_text'].apply(lambda x:re.sub('\\w*\\d\\w*' , '', x) )\n#removing square brackets\ndf['review_text']=df['review_text'].apply(lambda x:re.sub('\\[.*?\\]', '', x) )\ndf['review_text']=df['review_text'].apply(lambda x:re.sub('<.*?>+', '', x) )","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:46:33.272045Z","iopub.execute_input":"2023-02-04T13:46:33.272869Z","iopub.status.idle":"2023-02-04T13:47:33.999496Z","shell.execute_reply.started":"2023-02-04T13:46:33.272827Z","shell.execute_reply":"2023-02-04T13:47:33.998372Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#splitting text into words\ntokenList=[]\nfor indx in range(len(df)):\n       token=word_tokenize(df['review_text'][indx])\n       tokenList.append(token)\ndf['text_tokens'] = tokenList\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:47:34.000946Z","iopub.execute_input":"2023-02-04T13:47:34.001254Z","iopub.status.idle":"2023-02-04T13:53:17.645174Z","shell.execute_reply.started":"2023-02-04T13:47:34.001225Z","shell.execute_reply":"2023-02-04T13:53:17.644171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenList.clear()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:17.646644Z","iopub.execute_input":"2023-02-04T13:53:17.647240Z","iopub.status.idle":"2023-02-04T13:53:17.661174Z","shell.execute_reply.started":"2023-02-04T13:53:17.647201Z","shell.execute_reply":"2023-02-04T13:53:17.660074Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples = []\nlabels = []\nfor indx in range(len(df)):\n    lines=df['review_text'][indx]\n    samples.append(lines)\n    labels.append(df['rating'][indx])","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:17.662859Z","iopub.execute_input":"2023-02-04T13:53:17.663230Z","iopub.status.idle":"2023-02-04T13:53:28.396514Z","shell.execute_reply.started":"2023-02-04T13:53:17.663193Z","shell.execute_reply":"2023-02-04T13:53:28.395485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples[:250]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:28.397959Z","iopub.execute_input":"2023-02-04T13:53:28.398695Z","iopub.status.idle":"2023-02-04T13:53:28.419218Z","shell.execute_reply.started":"2023-02-04T13:53:28.398654Z","shell.execute_reply":"2023-02-04T13:53:28.418337Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels[:250]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:28.420225Z","iopub.execute_input":"2023-02-04T13:53:28.420582Z","iopub.status.idle":"2023-02-04T13:53:28.432827Z","shell.execute_reply.started":"2023-02-04T13:53:28.420551Z","shell.execute_reply":"2023-02-04T13:53:28.431740Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Creating training and test subsets","metadata":{}},{"cell_type":"code","source":"# shuffle and split the data\nseed = 1337\nrng = np.random.RandomState(seed)\nrng.shuffle(samples)\nrng = np.random.RandomState(seed)\nrng.shuffle(labels)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:28.434265Z","iopub.execute_input":"2023-02-04T13:53:28.435938Z","iopub.status.idle":"2023-02-04T13:53:28.701919Z","shell.execute_reply.started":"2023-02-04T13:53:28.435898Z","shell.execute_reply":"2023-02-04T13:53:28.700917Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# training and validation data\nvalidation_split = 0.2\nnum_validation_samples = int(validation_split * len(samples))\ntrain_samples = samples[:-num_validation_samples]\nval_samples = samples[-num_validation_samples:]\ntrain_labels = labels[:-num_validation_samples]\nval_labels = labels[-num_validation_samples:]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:53:28.704419Z","iopub.execute_input":"2023-02-04T13:53:28.705118Z","iopub.status.idle":"2023-02-04T13:53:28.765650Z","shell.execute_reply.started":"2023-02-04T13:53:28.705076Z","shell.execute_reply":"2023-02-04T13:53:28.764572Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"samples.clear()\nlabels.clear()\ndel df","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:34.727868Z","iopub.execute_input":"2023-02-04T13:57:34.728252Z","iopub.status.idle":"2023-02-04T13:57:34.733149Z","shell.execute_reply.started":"2023-02-04T13:57:34.728219Z","shell.execute_reply":"2023-02-04T13:57:34.732132Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_samples[:10]\n","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:41.294540Z","iopub.execute_input":"2023-02-04T13:57:41.294964Z","iopub.status.idle":"2023-02-04T13:57:41.303858Z","shell.execute_reply.started":"2023-02-04T13:57:41.294931Z","shell.execute_reply":"2023-02-04T13:57:41.302411Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_samples[:10]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:41.682115Z","iopub.execute_input":"2023-02-04T13:57:41.682463Z","iopub.status.idle":"2023-02-04T13:57:41.689596Z","shell.execute_reply.started":"2023-02-04T13:57:41.682433Z","shell.execute_reply":"2023-02-04T13:57:41.688383Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_labels[:10]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:42.122139Z","iopub.execute_input":"2023-02-04T13:57:42.122455Z","iopub.status.idle":"2023-02-04T13:57:42.130541Z","shell.execute_reply.started":"2023-02-04T13:57:42.122426Z","shell.execute_reply":"2023-02-04T13:57:42.129355Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"val_labels[:10]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:42.558425Z","iopub.execute_input":"2023-02-04T13:57:42.558766Z","iopub.status.idle":"2023-02-04T13:57:42.567152Z","shell.execute_reply.started":"2023-02-04T13:57:42.558737Z","shell.execute_reply":"2023-02-04T13:57:42.566112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Word Index and Vocabulary","metadata":{}},{"cell_type":"markdown","source":"#### use the TextVectorization to index the vocabulary found in the dataset. \n","metadata":{}},{"cell_type":"code","source":"import tensorflow as tf\nfrom tensorflow import keras\nfrom tensorflow.keras.layers import TextVectorization","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:44.066231Z","iopub.execute_input":"2023-02-04T13:57:44.066932Z","iopub.status.idle":"2023-02-04T13:57:47.612720Z","shell.execute_reply.started":"2023-02-04T13:57:44.066891Z","shell.execute_reply":"2023-02-04T13:57:47.611697Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = TextVectorization(max_tokens=20000, output_sequence_length=200)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:47.616436Z","iopub.execute_input":"2023-02-04T13:57:47.616964Z","iopub.status.idle":"2023-02-04T13:57:49.083155Z","shell.execute_reply.started":"2023-02-04T13:57:47.616934Z","shell.execute_reply":"2023-02-04T13:57:49.081942Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_ds=tf.data.Dataset.from_tensor_slices(train_samples).batch(128)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:49.084972Z","iopub.execute_input":"2023-02-04T13:57:49.085363Z","iopub.status.idle":"2023-02-04T13:57:54.362364Z","shell.execute_reply.started":"2023-02-04T13:57:49.085325Z","shell.execute_reply":"2023-02-04T13:57:54.361313Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.adapt(text_ds)","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:57:54.365103Z","iopub.execute_input":"2023-02-04T13:57:54.365546Z","iopub.status.idle":"2023-02-04T13:58:35.647929Z","shell.execute_reply.started":"2023-02-04T13:57:54.365493Z","shell.execute_reply":"2023-02-04T13:58:35.646720Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### can retrieve the computed vocabulary used via vectorizer.get_vocabulary()\n#### printing the top 15 words","metadata":{}},{"cell_type":"code","source":"vectorizer.get_vocabulary()[:15]","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:58:35.658942Z","iopub.execute_input":"2023-02-04T13:58:35.659270Z","iopub.status.idle":"2023-02-04T13:58:35.705330Z","shell.execute_reply.started":"2023-02-04T13:58:35.659240Z","shell.execute_reply":"2023-02-04T13:58:35.704305Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"voc = vectorizer.get_vocabulary()\nword_index = dict(zip(voc,range(len(voc))))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:08:02.008310Z","iopub.execute_input":"2023-02-04T14:08:02.009018Z","iopub.status.idle":"2023-02-04T14:08:02.059230Z","shell.execute_reply.started":"2023-02-04T14:08:02.008980Z","shell.execute_reply":"2023-02-04T14:08:02.058125Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Load pre-trained word embeddings Let's download pre-trained GloVe embeddings","metadata":{}},{"cell_type":"code","source":"!wget http://nlp.stanford.edu/data/glove.6B.zip\n!unzip -q glove.6B.zip","metadata":{"execution":{"iopub.status.busy":"2023-02-04T13:58:35.730227Z","iopub.execute_input":"2023-02-04T13:58:35.730922Z","iopub.status.idle":"2023-02-04T14:01:41.290891Z","shell.execute_reply.started":"2023-02-04T13:58:35.730880Z","shell.execute_reply":"2023-02-04T14:01:41.289563Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Let's make a dict mapping words (strings) to their NumPy vector representation:\npath_to_glove_file = \"/kaggle/working/glove.6B.100d.txt\"","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:01:41.295140Z","iopub.execute_input":"2023-02-04T14:01:41.296287Z","iopub.status.idle":"2023-02-04T14:01:41.302256Z","shell.execute_reply.started":"2023-02-04T14:01:41.296244Z","shell.execute_reply":"2023-02-04T14:01:41.301143Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"embeddings_index = {}\nwith open(path_to_glove_file) as f:\n  for line in f:\n    word, coefs = line.split(maxsplit=1)\n    coefs = np.fromstring(coefs,\"f\",sep=\" \")\n    embeddings_index[word] = coefs\n\nprint(\"Found %s word vector.\" % len(embeddings_index))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:01:41.304630Z","iopub.execute_input":"2023-02-04T14:01:41.305087Z","iopub.status.idle":"2023-02-04T14:01:48.558637Z","shell.execute_reply.started":"2023-02-04T14:01:41.305048Z","shell.execute_reply":"2023-02-04T14:01:48.557531Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Now, let's prepare a corresponding embedding matrix that we can use in a Keras Embedding layer. It's a simple NumPy matrix where entry at index i is the pre-trained vector for the word of index i in our vectorizer's vocabulary.","metadata":{}},{"cell_type":"code","source":"#prepare embedding matrix\nnum_tokens = len(voc)+2\nembedding_dim = 100\nhits=0\nmisses=0\nembedding_matrix = np.zeros((num_tokens, embedding_dim))\nfor word,i in word_index.items():\n   embedding_vector = embeddings_index.get(word)\n   if embedding_vector is not None:\n      # words not found in embedding index will be all zeroes\n      # this includes the representation for \"padding\" and \"OOV\"\n      embedding_matrix[i]=embedding_vector\n      hits += 1\n   else:\n      misses += 1\nprint(\"Converted %d words (%d misses)\" % (hits,misses))","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:08:13.842430Z","iopub.execute_input":"2023-02-04T14:08:13.842827Z","iopub.status.idle":"2023-02-04T14:08:13.905583Z","shell.execute_reply.started":"2023-02-04T14:08:13.842793Z","shell.execute_reply":"2023-02-04T14:08:13.904499Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Next, we load the pre-trained word embeddings matrix into an Embedding layer.\n\nNote that we set trainable=False so as to keep the embeddings fixed (we don't want to update them during training).","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras.layers import Embedding\n\nembedding_layer = Embedding(\nnum_tokens,\nembedding_dim,\nembeddings_initializer=keras.initializers.Constant(embedding_matrix),\ntrainable=False\n)\n","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:08:15.319933Z","iopub.execute_input":"2023-02-04T14:08:15.321047Z","iopub.status.idle":"2023-02-04T14:08:15.330797Z","shell.execute_reply.started":"2023-02-04T14:08:15.321000Z","shell.execute_reply":"2023-02-04T14:08:15.329796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Build the model A simple 1D convnet with global max pooling and a classifier at the end.","metadata":{}},{"cell_type":"code","source":"from tensorflow.keras import layers","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:09:22.019146Z","iopub.execute_input":"2023-02-04T14:09:22.019804Z","iopub.status.idle":"2023-02-04T14:09:22.024982Z","shell.execute_reply.started":"2023-02-04T14:09:22.019767Z","shell.execute_reply":"2023-02-04T14:09:22.023963Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"int_sequences_input = keras.Input(shape=(None,), dtype=\"int64\")\nembedded_sequences = embedding_layer(int_sequences_input)\nx = layers.Conv1D(128, 5, activation=\"relu\")(embedded_sequences)\nx = layers.MaxPooling1D(5)(x)\nx = layers.Conv1D(128, 5, activation=\"relu\")(x)\nx = layers.MaxPooling1D(5)(x)\nx = layers.Conv1D(128, 5, activation=\"relu\")(x)\nx = layers.GlobalMaxPooling1D()(x)\nx = layers.Dense(128, activation=\"relu\")(x)\nx = layers.Dropout(0.5)(x)\npreds = layers.Dense(len(train_labels), activation=\"softmax\")(x)\nmodel = keras.Model(int_sequences_input, preds)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-02-04T14:09:22.942773Z","iopub.execute_input":"2023-02-04T14:09:22.943749Z","iopub.status.idle":"2023-02-04T14:09:23.050318Z","shell.execute_reply.started":"2023-02-04T14:09:22.943704Z","shell.execute_reply":"2023-02-04T14:09:23.049258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#### Train the model\nFirst, convert our list-of-strings data to NumPy arrays of integer indices. The arrays are right-padded.","metadata":{}},{"cell_type":"markdown","source":"#### Memory error ####","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"## Reference: https://keras.io/examples/nlp/pretrained_word_embeddings/","metadata":{}},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}