{"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-01-29T16:29:12.463195Z","iopub.execute_input":"2023-01-29T16:29:12.464448Z","iopub.status.idle":"2023-01-29T16:29:12.497068Z","shell.execute_reply.started":"2023-01-29T16:29:12.464330Z","shell.execute_reply":"2023-01-29T16:29:12.495809Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport csv\nimport re \nimport string\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:29:12.556902Z","iopub.execute_input":"2023-01-29T16:29:12.557321Z","iopub.status.idle":"2023-01-29T16:29:13.760495Z","shell.execute_reply.started":"2023-01-29T16:29:12.557289Z","shell.execute_reply":"2023-01-29T16:29:13.759205Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n!wget --quiet https://raw.githubusercontent.com/tensorflow/models/master/official/nlp/bert/tokenization.py","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:50:26.153850Z","iopub.execute_input":"2023-01-29T16:50:26.155092Z","iopub.status.idle":"2023-01-29T16:50:27.676653Z","shell.execute_reply.started":"2023-01-29T16:50:26.155004Z","shell.execute_reply":"2023-01-29T16:50:27.674943Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import pandas as pd\nimport csv\nimport re \nimport string\nfrom sklearn.model_selection import train_test_split\nfrom sklearn.feature_extraction.text import CountVectorizer\nfrom sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:51:49.931428Z","iopub.execute_input":"2023-01-29T16:51:49.931896Z","iopub.status.idle":"2023-01-29T16:51:49.939350Z","shell.execute_reply.started":"2023-01-29T16:51:49.931810Z","shell.execute_reply":"2023-01-29T16:51:49.937738Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tokenization","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:52:07.263989Z","iopub.execute_input":"2023-01-29T16:52:07.264436Z","iopub.status.idle":"2023-01-29T16:52:18.809513Z","shell.execute_reply.started":"2023-01-29T16:52:07.264397Z","shell.execute_reply":"2023-01-29T16:52:18.808270Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tokenization\nimport tensorflow as tf\nimport tensorflow_hub as hub\nfrom tensorflow.keras.utils import to_categorical\nfrom sklearn import preprocessing\nfrom sklearn.model_selection import train_test_split","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:53:48.136123Z","iopub.execute_input":"2023-01-29T16:53:48.136593Z","iopub.status.idle":"2023-01-29T16:53:48.144220Z","shell.execute_reply.started":"2023-01-29T16:53:48.136557Z","shell.execute_reply":"2023-01-29T16:53:48.142863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_train.csv\")\ntest = pd.read_csv(\"/kaggle/input/goodreads-books-reviews-290312/goodreads_test.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:34:49.981271Z","iopub.execute_input":"2023-01-29T16:34:49.981713Z","iopub.status.idle":"2023-01-29T16:35:10.395901Z","shell.execute_reply.started":"2023-01-29T16:34:49.981671Z","shell.execute_reply":"2023-01-29T16:35:10.394626Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:10.398063Z","iopub.execute_input":"2023-01-29T16:35:10.398425Z","iopub.status.idle":"2023-01-29T16:35:10.422759Z","shell.execute_reply.started":"2023-01-29T16:35:10.398385Z","shell.execute_reply":"2023-01-29T16:35:10.421585Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.drop(['user_id', 'book_id', 'review_id', 'date_added', 'date_updated', 'n_votes','read_at', 'started_at','n_comments'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:47.608094Z","iopub.execute_input":"2023-01-29T16:35:47.608495Z","iopub.status.idle":"2023-01-29T16:35:47.638222Z","shell.execute_reply.started":"2023-01-29T16:35:47.608466Z","shell.execute_reply":"2023-01-29T16:35:47.637315Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test = test.drop(['user_id', 'book_id', 'review_id', 'date_added', 'date_updated', 'n_votes','read_at', 'started_at','n_comments'], axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:48.418662Z","iopub.execute_input":"2023-01-29T16:35:48.419292Z","iopub.status.idle":"2023-01-29T16:35:48.493190Z","shell.execute_reply.started":"2023-01-29T16:35:48.419256Z","shell.execute_reply":"2023-01-29T16:35:48.491905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:51.892745Z","iopub.execute_input":"2023-01-29T16:35:51.893183Z","iopub.status.idle":"2023-01-29T16:35:51.907480Z","shell.execute_reply.started":"2023-01-29T16:35:51.893147Z","shell.execute_reply":"2023-01-29T16:35:51.906331Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0=train[train['rating']==0]\ntrain1=train[train['rating']==1]\ntrain2=train[train['rating']==2]\ntrain3=train[train['rating']==3]\ntrain4=train[train['rating']==4]\ntrain5=train[train['rating']==5]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:53.377837Z","iopub.execute_input":"2023-01-29T16:35:53.378677Z","iopub.status.idle":"2023-01-29T16:35:53.499883Z","shell.execute_reply.started":"2023-01-29T16:35:53.378631Z","shell.execute_reply":"2023-01-29T16:35:53.498171Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:35:54.671289Z","iopub.execute_input":"2023-01-29T16:35:54.671952Z","iopub.status.idle":"2023-01-29T16:35:54.680922Z","shell.execute_reply.started":"2023-01-29T16:35:54.671918Z","shell.execute_reply":"2023-01-29T16:35:54.679474Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0=train0[:int(train0.shape[0]/1000)]\ntrain1=train1[:int(train1.shape[0]/1000)]\ntrain2=train2[:int(train2.shape[0]/1000)]\ntrain3=train3[:int(train3.shape[0]/1000)]\ntrain4=train4[:int(train4.shape[0]/1000)]\ntrain5=train5[:int(train5.shape[0]/1000)]","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:36:25.434435Z","iopub.execute_input":"2023-01-29T16:36:25.434915Z","iopub.status.idle":"2023-01-29T16:36:25.442795Z","shell.execute_reply.started":"2023-01-29T16:36:25.434879Z","shell.execute_reply":"2023-01-29T16:36:25.441622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train0.shape, train1.shape, train2.shape, train3.shape, train4.shape, train5.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:36:32.634215Z","iopub.execute_input":"2023-01-29T16:36:32.634610Z","iopub.status.idle":"2023-01-29T16:36:32.643905Z","shell.execute_reply.started":"2023-01-29T16:36:32.634582Z","shell.execute_reply":"2023-01-29T16:36:32.642485Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.concat([train0,train1,train2,train3,train4,train5],axis=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:36:44.492508Z","iopub.execute_input":"2023-01-29T16:36:44.492960Z","iopub.status.idle":"2023-01-29T16:36:44.502499Z","shell.execute_reply.started":"2023-01-29T16:36:44.492924Z","shell.execute_reply":"2023-01-29T16:36:44.501370Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:36:56.320691Z","iopub.execute_input":"2023-01-29T16:36:56.321541Z","iopub.status.idle":"2023-01-29T16:36:56.330133Z","shell.execute_reply.started":"2023-01-29T16:36:56.321492Z","shell.execute_reply":"2023-01-29T16:36:56.328538Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.columns","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:37:05.455668Z","iopub.execute_input":"2023-01-29T16:37:05.456144Z","iopub.status.idle":"2023-01-29T16:37:05.463368Z","shell.execute_reply.started":"2023-01-29T16:37:05.456108Z","shell.execute_reply":"2023-01-29T16:37:05.462290Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:37:14.877317Z","iopub.execute_input":"2023-01-29T16:37:14.877710Z","iopub.status.idle":"2023-01-29T16:37:14.888698Z","shell.execute_reply.started":"2023-01-29T16:37:14.877680Z","shell.execute_reply":"2023-01-29T16:37:14.887377Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = train.sample(frac=1)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:37:30.324542Z","iopub.execute_input":"2023-01-29T16:37:30.324961Z","iopub.status.idle":"2023-01-29T16:37:30.331787Z","shell.execute_reply.started":"2023-01-29T16:37:30.324928Z","shell.execute_reply":"2023-01-29T16:37:30.330583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train.head()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:37:38.213708Z","iopub.execute_input":"2023-01-29T16:37:38.214147Z","iopub.status.idle":"2023-01-29T16:37:38.225858Z","shell.execute_reply.started":"2023-01-29T16:37:38.214112Z","shell.execute_reply":"2023-01-29T16:37:38.224650Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = train","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:38:26.139889Z","iopub.execute_input":"2023-01-29T16:38:26.140321Z","iopub.status.idle":"2023-01-29T16:38:26.145905Z","shell.execute_reply.started":"2023-01-29T16:38:26.140285Z","shell.execute_reply":"2023-01-29T16:38:26.144473Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\n# splitting the training and testing part from the data\nX_temp, X_test, y_temp, y_test = train_test_split(train_data['review_text'], train_data['rating'], test_size=0.2, random_state=0)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:38:44.882794Z","iopub.execute_input":"2023-01-29T16:38:44.883223Z","iopub.status.idle":"2023-01-29T16:38:45.172421Z","shell.execute_reply.started":"2023-01-29T16:38:44.883184Z","shell.execute_reply":"2023-01-29T16:38:45.170989Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X = X_temp\ntrain_y = y_temp\ntest_x = X_test\ntest_y = y_test\n\ntrain_X.shape, train_y.shape, test_x.shape , test_y.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:39:11.472737Z","iopub.execute_input":"2023-01-29T16:39:11.473161Z","iopub.status.idle":"2023-01-29T16:39:11.483365Z","shell.execute_reply.started":"2023-01-29T16:39:11.473126Z","shell.execute_reply":"2023-01-29T16:39:11.482113Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install tokenization","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:44:01.362759Z","iopub.execute_input":"2023-01-29T16:44:01.363185Z","iopub.status.idle":"2023-01-29T16:44:15.425360Z","shell.execute_reply.started":"2023-01-29T16:44:01.363153Z","shell.execute_reply":"2023-01-29T16:44:15.423891Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"label = preprocessing.LabelEncoder()\ny = label.fit_transform(train_X)\ny = to_categorical(train_y)\nprint(train_y[:5])","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:53:58.132964Z","iopub.execute_input":"2023-01-29T16:53:58.133416Z","iopub.status.idle":"2023-01-29T16:53:58.146790Z","shell.execute_reply.started":"2023-01-29T16:53:58.133382Z","shell.execute_reply":"2023-01-29T16:53:58.145117Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build a BERT layer","metadata":{}},{"cell_type":"code","source":"m_url = 'https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/2'\nbert_layer = hub.KerasLayer(m_url, trainable=True)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:54:30.917818Z","iopub.execute_input":"2023-01-29T16:54:30.918242Z","iopub.status.idle":"2023-01-29T16:54:47.218432Z","shell.execute_reply.started":"2023-01-29T16:54:30.918209Z","shell.execute_reply":"2023-01-29T16:54:47.216966Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Encoding the text","metadata":{}},{"cell_type":"code","source":"!pip install tensorflow==2.0\nimport tensorflow as tf\nprint(tf.__version__)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:57:32.761432Z","iopub.execute_input":"2023-01-29T16:57:32.761830Z","iopub.status.idle":"2023-01-29T16:58:17.589308Z","shell.execute_reply.started":"2023-01-29T16:57:32.761789Z","shell.execute_reply":"2023-01-29T16:58:17.587669Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import nltk\nnltk.download(\"popular\")\nfrom nltk.tokenize import word_tokenize","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:58:28.862819Z","iopub.execute_input":"2023-01-29T16:58:28.863227Z","iopub.status.idle":"2023-01-29T16:58:28.983527Z","shell.execute_reply.started":"2023-01-29T16:58:28.863193Z","shell.execute_reply":"2023-01-29T16:58:28.982158Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install bert-for-tf2","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:58:37.891142Z","iopub.execute_input":"2023-01-29T16:58:37.891563Z","iopub.status.idle":"2023-01-29T16:58:55.645761Z","shell.execute_reply.started":"2023-01-29T16:58:37.891531Z","shell.execute_reply":"2023-01-29T16:58:55.644278Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from bert import tokenization\nfrom bert.tokenization.bert_tokenization import FullTokenizer","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:58:55.648329Z","iopub.execute_input":"2023-01-29T16:58:55.648699Z","iopub.status.idle":"2023-01-29T16:58:56.033550Z","shell.execute_reply.started":"2023-01-29T16:58:55.648666Z","shell.execute_reply":"2023-01-29T16:58:56.032083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vocab_file = bert_layer.resolved_object.vocab_file.asset_path.numpy()\ndo_lower_case = bert_layer.resolved_object.do_lower_case.numpy()\ntokenizer = FullTokenizer(vocab_file, do_lower_case)\n\ndef bert_encode(texts, tokenizer, max_len=512):\n    all_tokens = []\n    all_masks = []\n    all_segments = []\n    \n    for text in texts:\n        text = tokenizer.tokenize(text)\n        \n        text = text[:max_len-2]\n        input_sequence = [\"[CLS]\"] + text + [\"[SEP]\"]\n        pad_len = max_len-len(input_sequence)\n        \n        tokens = tokenizer.convert_tokens_to_ids(input_sequence) + [0] * pad_len\n        pad_masks = [1] * len(input_sequence) + [0] * pad_len\n        segment_ids = [0] * max_len\n        \n        all_tokens.append(tokens)\n        all_masks.append(pad_masks)\n        all_segments.append(segment_ids)\n        \n    return np.array(all_tokens), np.array(all_masks), np.array(all_segments)\n","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:58:56.034948Z","iopub.execute_input":"2023-01-29T16:58:56.035821Z","iopub.status.idle":"2023-01-29T16:58:56.167775Z","shell.execute_reply.started":"2023-01-29T16:58:56.035781Z","shell.execute_reply":"2023-01-29T16:58:56.166690Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Build the Model","metadata":{}},{"cell_type":"code","source":"def build_model(bert_layer, max_len=512):\n    input_word_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=\"input_word_ids\")\n    input_mask = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=\"input_mask\")\n    segment_ids = tf.keras.Input(shape=(max_len,), dtype=tf.int32, name=\"segment_ids\")\n    \n    pooled_output, sequence_output = bert_layer([input_word_ids, input_mask, segment_ids])\n    \n    clf_output = sequence_output[:, 0, :]\n    \n    lay = tf.keras.layers.Dense(64, activation='relu')(clf_output)\n    lay = tf.keras.layers.Dropout(0.2)(lay)\n    lay = tf.keras.layers.Dense(32, activation='relu')(lay)\n    lay = tf.keras.layers.Dropout(0.2)(lay)\n    out = tf.keras.layers.Dense(6, activation='softmax')(lay)\n    \n    model = tf.keras.models.Model(inputs=[input_word_ids, input_mask, segment_ids], outputs=out)\n    model.compile(tf.keras.optimizers.Adam(lr=2e-5), loss='categorical_crossentropy', metrics=['accuracy'])\n    \n    return model","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:59:15.750877Z","iopub.execute_input":"2023-01-29T16:59:15.751297Z","iopub.status.idle":"2023-01-29T16:59:15.762959Z","shell.execute_reply.started":"2023-01-29T16:59:15.751263Z","shell.execute_reply":"2023-01-29T16:59:15.761593Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max_len = 250\ntrain_input = bert_encode(train_X.values, tokenizer, max_len=max_len)\ntest_input = bert_encode(test_x.values, tokenizer, max_len=max_len)\ntrain_labels = train_y","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:59:21.956898Z","iopub.execute_input":"2023-01-29T16:59:21.957367Z","iopub.status.idle":"2023-01-29T16:59:26.310586Z","shell.execute_reply.started":"2023-01-29T16:59:21.957317Z","shell.execute_reply":"2023-01-29T16:59:26.309384Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"labels = label.classes_\nprint(labels)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:59:29.992915Z","iopub.execute_input":"2023-01-29T16:59:29.993331Z","iopub.status.idle":"2023-01-29T16:59:30.056241Z","shell.execute_reply.started":"2023-01-29T16:59:29.993288Z","shell.execute_reply":"2023-01-29T16:59:30.054588Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = build_model(bert_layer, max_len=max_len)\nmodel.summary()","metadata":{"execution":{"iopub.status.busy":"2023-01-29T16:59:57.561692Z","iopub.execute_input":"2023-01-29T16:59:57.562127Z","iopub.status.idle":"2023-01-29T16:59:58.358154Z","shell.execute_reply.started":"2023-01-29T16:59:57.562093Z","shell.execute_reply":"2023-01-29T16:59:58.356779Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Run Model","metadata":{}},{"cell_type":"code","source":"label = preprocessing.LabelEncoder()\ny = label.fit_transform(train_labels)\ny = to_categorical(y)\nprint(y[:5])","metadata":{"execution":{"iopub.status.busy":"2023-01-29T17:00:15.900240Z","iopub.execute_input":"2023-01-29T17:00:15.900665Z","iopub.status.idle":"2023-01-29T17:00:15.909512Z","shell.execute_reply.started":"2023-01-29T17:00:15.900630Z","shell.execute_reply":"2023-01-29T17:00:15.908218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"checkpoint = tf.keras.callbacks.ModelCheckpoint('model.h5', monitor='val_accuracy', save_best_only=True, verbose=1)\nearlystopping = tf.keras.callbacks.EarlyStopping(monitor='val_accuracy', patience=5, verbose=1)\n\ntrain_sh = model.fit(\n    train_input, y,\n    validation_split=0.2,\n    epochs=3,\n    callbacks=[checkpoint, earlystopping],\n    batch_size=32,\n    verbose=1\n)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T17:00:21.666951Z","iopub.execute_input":"2023-01-29T17:00:21.667375Z","iopub.status.idle":"2023-01-29T17:41:35.901835Z","shell.execute_reply.started":"2023-01-29T17:00:21.667338Z","shell.execute_reply":"2023-01-29T17:41:35.900852Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction = model.predict(test_input)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:21:55.594458Z","iopub.execute_input":"2023-01-29T18:21:55.594899Z","iopub.status.idle":"2023-01-29T18:23:15.524721Z","shell.execute_reply.started":"2023-01-29T18:21:55.594854Z","shell.execute_reply":"2023-01-29T18:23:15.523155Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:23:38.978568Z","iopub.execute_input":"2023-01-29T18:23:38.979065Z","iopub.status.idle":"2023-01-29T18:23:38.987884Z","shell.execute_reply.started":"2023-01-29T18:23:38.979013Z","shell.execute_reply":"2023-01-29T18:23:38.986570Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"prediction.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:29:53.194731Z","iopub.execute_input":"2023-01-29T18:29:53.195312Z","iopub.status.idle":"2023-01-29T18:29:53.203323Z","shell.execute_reply.started":"2023-01-29T18:29:53.195258Z","shell.execute_reply":"2023-01-29T18:29:53.202347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred = np.argmax(prediction, axis = 1)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:29:40.214708Z","iopub.execute_input":"2023-01-29T18:29:40.215116Z","iopub.status.idle":"2023-01-29T18:29:40.220960Z","shell.execute_reply.started":"2023-01-29T18:29:40.215085Z","shell.execute_reply":"2023-01-29T18:29:40.219859Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred.shape","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:29:44.981386Z","iopub.execute_input":"2023-01-29T18:29:44.982058Z","iopub.status.idle":"2023-01-29T18:29:44.987891Z","shell.execute_reply.started":"2023-01-29T18:29:44.981989Z","shell.execute_reply":"2023-01-29T18:29:44.987108Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:29:58.702326Z","iopub.execute_input":"2023-01-29T18:29:58.702759Z","iopub.status.idle":"2023-01-29T18:29:58.711586Z","shell.execute_reply.started":"2023-01-29T18:29:58.702722Z","shell.execute_reply":"2023-01-29T18:29:58.710628Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score\naccuracy_score(test_y, pred)","metadata":{"execution":{"iopub.status.busy":"2023-01-29T18:31:31.809255Z","iopub.execute_input":"2023-01-29T18:31:31.809813Z","iopub.status.idle":"2023-01-29T18:31:31.819529Z","shell.execute_reply.started":"2023-01-29T18:31:31.809769Z","shell.execute_reply":"2023-01-29T18:31:31.818105Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}