{"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":"2022-07-19T09:56:53.35555Z","iopub.execute_input":"2022-07-19T09:56:53.355976Z","iopub.status.idle":"2022-07-19T09:56:53.370837Z","shell.execute_reply.started":"2022-07-19T09:56:53.355888Z","shell.execute_reply":"2022-07-19T09:56:53.369655Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_train.csv')\ntest_data = pd.read_csv('../input/goodreads-books-reviews-290312/goodreads_test.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:02:56.96929Z","iopub.execute_input":"2022-07-19T10:02:56.969649Z","iopub.status.idle":"2022-07-19T10:03:14.507674Z","shell.execute_reply.started":"2022-07-19T10:02:56.96962Z","shell.execute_reply":"2022-07-19T10:03:14.506583Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.info()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:48:59.924742Z","iopub.execute_input":"2022-07-19T09:48:59.925332Z","iopub.status.idle":"2022-07-19T09:49:00.561337Z","shell.execute_reply.started":"2022-07-19T09:48:59.925292Z","shell.execute_reply":"2022-07-19T09:49:00.560152Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:49:00.562835Z","iopub.execute_input":"2022-07-19T09:49:00.56341Z","iopub.status.idle":"2022-07-19T09:49:00.585441Z","shell.execute_reply.started":"2022-07-19T09:49:00.563371Z","shell.execute_reply":"2022-07-19T09:49:00.584422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:49:00.588004Z","iopub.execute_input":"2022-07-19T09:49:00.588349Z","iopub.status.idle":"2022-07-19T09:49:01.199472Z","shell.execute_reply.started":"2022-07-19T09:49:00.588315Z","shell.execute_reply":"2022-07-19T09:49:01.198526Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_data.isnull().sum()","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:49:01.200752Z","iopub.execute_input":"2022-07-19T09:49:01.201384Z","iopub.status.idle":"2022-07-19T09:49:01.537464Z","shell.execute_reply.started":"2022-07-19T09:49:01.201344Z","shell.execute_reply":"2022-07-19T09:49:01.536438Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"X = train_data.iloc[:,[4]]\ny = train_data.iloc[:,[3]]","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:03:14.509963Z","iopub.execute_input":"2022-07-19T10:03:14.510347Z","iopub.status.idle":"2022-07-19T10:03:14.56033Z","shell.execute_reply.started":"2022-07-19T10:03:14.51031Z","shell.execute_reply":"2022-07-19T10:03:14.559214Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\nX_train,X_test,y_train,y_test = train_test_split(X,y,test_size=0.2)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:03:14.561728Z","iopub.execute_input":"2022-07-19T10:03:14.562337Z","iopub.status.idle":"2022-07-19T10:03:14.81515Z","shell.execute_reply.started":"2022-07-19T10:03:14.562296Z","shell.execute_reply":"2022-07-19T10:03:14.814154Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import tensorflow as tf\nimport tensorflow_hub as hub\nimport tensorflow_text as text","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:57:37.50829Z","iopub.execute_input":"2022-07-19T09:57:37.509214Z","iopub.status.idle":"2022-07-19T09:57:37.514143Z","shell.execute_reply.started":"2022-07-19T09:57:37.509178Z","shell.execute_reply":"2022-07-19T09:57:37.512972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q -U \"tensorflow-text==2.8.*\"","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:57:13.984336Z","iopub.execute_input":"2022-07-19T09:57:13.98468Z","iopub.status.idle":"2022-07-19T09:57:24.592565Z","shell.execute_reply.started":"2022-07-19T09:57:13.984644Z","shell.execute_reply":"2022-07-19T09:57:24.591397Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"bert_preprocess = hub.KerasLayer(\"https://tfhub.dev/tensorflow/bert_en_uncased_preprocess/3\")\nbert_encoder = hub.KerasLayer(\"https://tfhub.dev/tensorflow/bert_en_uncased_L-12_H-768_A-12/4\")","metadata":{"execution":{"iopub.status.busy":"2022-07-19T09:57:41.795094Z","iopub.execute_input":"2022-07-19T09:57:41.795975Z","iopub.status.idle":"2022-07-19T09:58:16.874723Z","shell.execute_reply.started":"2022-07-19T09:57:41.795941Z","shell.execute_reply":"2022-07-19T09:58:16.873734Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Bert layers\ntext_input = tf.keras.layers.Input(shape=(), dtype=tf.string, name='text')\npreprocessed_text = bert_preprocess(text_input)\noutputs = bert_encoder(preprocessed_text)\n\n# Neural network layers\nl = tf.keras.layers.Dropout(0.1, name=\"dropout\")(outputs['pooled_output'])\nl = tf.keras.layers.Dense(1, activation='sigmoid', name=\"output\")(l)\n\n# Use inputs and outputs to construct a final model\nmodel = tf.keras.Model(inputs=[text_input], outputs = [l])","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:02:17.086387Z","iopub.execute_input":"2022-07-19T10:02:17.086803Z","iopub.status.idle":"2022-07-19T10:02:17.82167Z","shell.execute_reply.started":"2022-07-19T10:02:17.086748Z","shell.execute_reply":"2022-07-19T10:02:17.820684Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"METRICS = [\n      tf.keras.metrics.BinaryAccuracy(name='accuracy'),\n      tf.keras.metrics.Precision(name='precision'),\n      tf.keras.metrics.Recall(name='recall')\n]\n\nmodel.compile(optimizer='adam',\n              loss='binary_crossentropy',\n              metrics=METRICS)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:02:21.769994Z","iopub.execute_input":"2022-07-19T10:02:21.770403Z","iopub.status.idle":"2022-07-19T10:02:21.798321Z","shell.execute_reply.started":"2022-07-19T10:02:21.770359Z","shell.execute_reply":"2022-07-19T10:02:21.797388Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.fit(X_train, y_train, epochs=10)","metadata":{"execution":{"iopub.status.busy":"2022-07-19T10:03:14.818399Z","iopub.execute_input":"2022-07-19T10:03:14.818842Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}