{"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-03-13T15:50:29.877393Z","iopub.execute_input":"2023-03-13T15:50:29.877871Z","iopub.status.idle":"2023-03-13T15:50:29.948827Z","shell.execute_reply.started":"2023-03-13T15:50:29.877774Z","shell.execute_reply":"2023-03-13T15:50:29.947801Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"!pip install -q git+https://github.com/tensorflow/docs\n\nimport tensorflow as tf\nimport tensorflow_hub as hub\nimport tensorflow_datasets as tfds\nimport matplotlib.pyplot as plt\nimport tensorflow_docs as tfdocs\nimport tensorflow_docs.modeling\nimport tensorflow_docs.plots","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:50:29.950836Z","iopub.execute_input":"2023-03-13T15:50:29.951263Z","iopub.status.idle":"2023-03-13T15:51:02.398832Z","shell.execute_reply.started":"2023-03-13T15:50:29.951221Z","shell.execute_reply":"2023-03-13T15:51:02.397751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest_set= pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\ntrain_set.shape,test_set.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:02.400746Z","iopub.execute_input":"2023-03-13T15:51:02.401847Z","iopub.status.idle":"2023-03-13T15:51:08.063179Z","shell.execute_reply.started":"2023-03-13T15:51:02.401803Z","shell.execute_reply":"2023-03-13T15:51:08.062112Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_set.head())\nprint(test_set.head())","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:08.066971Z","iopub.execute_input":"2023-03-13T15:51:08.068782Z","iopub.status.idle":"2023-03-13T15:51:08.085662Z","shell.execute_reply.started":"2023-03-13T15:51:08.068740Z","shell.execute_reply":"2023-03-13T15:51:08.084419Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set['target'].plot(kind='hist',title='Target distribution')","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:08.087469Z","iopub.execute_input":"2023-03-13T15:51:08.087897Z","iopub.status.idle":"2023-03-13T15:51:08.507473Z","shell.execute_reply.started":"2023-03-13T15:51:08.087859Z","shell.execute_reply":"2023-03-13T15:51:08.506422Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection  import train_test_split\ntrain_set, remaining = train_test_split(train_set,random_state= 42,train_size=0.01,stratify = train_set.target.values)\nvalid_set , _ = train_test_split(remaining,random_state= 42,train_size=0.001,stratify = remaining.target.values)\ntrain_set.shape, remaining.shape, valid_set.shape, _.shape","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:08.510796Z","iopub.execute_input":"2023-03-13T15:51:08.511108Z","iopub.status.idle":"2023-03-13T15:51:10.012588Z","shell.execute_reply.started":"2023-03-13T15:51:08.511080Z","shell.execute_reply":"2023-03-13T15:51:10.011676Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set.target.head(15).values","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:10.014360Z","iopub.execute_input":"2023-03-13T15:51:10.015432Z","iopub.status.idle":"2023-03-13T15:51:10.022918Z","shell.execute_reply.started":"2023-03-13T15:51:10.015389Z","shell.execute_reply":"2023-03-13T15:51:10.021945Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_set['question_text'].head(15).values","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:10.024569Z","iopub.execute_input":"2023-03-13T15:51:10.025049Z","iopub.status.idle":"2023-03-13T15:51:10.038899Z","shell.execute_reply.started":"2023-03-13T15:51:10.025009Z","shell.execute_reply":"2023-03-13T15:51:10.037681Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"module_url = \"https://tfhub.dev/google/tf2-preview/gnews-swivel-20dim/1\" ","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:10.040993Z","iopub.execute_input":"2023-03-13T15:51:10.041372Z","iopub.status.idle":"2023-03-13T15:51:10.048292Z","shell.execute_reply.started":"2023-03-13T15:51:10.041336Z","shell.execute_reply":"2023-03-13T15:51:10.047298Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"hub_layer = hub.KerasLayer(module_url,input_shape=[],output_shape=[20],dtype=tf.string,trainable=False)\nmodel = tf.keras.Sequential([\n      hub_layer,\n      tf.keras.layers.Dense(256,'relu'),\n      tf.keras.layers.Dense(64,'relu'),\n      tf.keras.layers.Dense(1,'sigmoid')\n  ])\nmodel.compile(optimizer= tf.keras.optimizers.Adam(learning_rate= 0.0001),loss ='binary_crossentropy',metrics =['accuracy'] )\nmodel.summary()\nhistory = model.fit(train_set.question_text, train_set.target,epochs = 100,batch_size=32, validation_data=(valid_set['question_text'],valid_set['target']),\n                      callbacks=tf.keras.callbacks.EarlyStopping(monitor='val_loss',patience=2,mode='min'))\nprint(\"DONE\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:10.052148Z","iopub.execute_input":"2023-03-13T15:51:10.052414Z","iopub.status.idle":"2023-03-13T15:51:39.963595Z","shell.execute_reply.started":"2023-03-13T15:51:10.052388Z","shell.execute_reply":"2023-03-13T15:51:39.961968Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_set.head()","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:51:39.964948Z","iopub.execute_input":"2023-03-13T15:51:39.965363Z","iopub.status.idle":"2023-03-13T15:51:39.978997Z","shell.execute_reply.started":"2023-03-13T15:51:39.965326Z","shell.execute_reply":"2023-03-13T15:51:39.977772Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"preds=model.predict(test_set['question_text'])\npreds","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:58:28.213629Z","iopub.execute_input":"2023-03-13T15:58:28.214224Z","iopub.status.idle":"2023-03-13T15:59:02.826056Z","shell.execute_reply.started":"2023-03-13T15:58:28.214185Z","shell.execute_reply":"2023-03-13T15:59:02.824905Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.DataFrame({\"Predictions\": [np.round(i,2) for i in preds],\"Text\":test_set['question_text']})\ndf","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:52:21.109040Z","iopub.execute_input":"2023-03-13T15:52:21.109343Z","iopub.status.idle":"2023-03-13T15:52:24.763608Z","shell.execute_reply.started":"2023-03-13T15:52:21.109316Z","shell.execute_reply":"2023-03-13T15:52:24.762515Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model.save('/kaggle/working/my_model.h5')","metadata":{"execution":{"iopub.status.busy":"2023-03-13T15:54:16.364335Z","iopub.execute_input":"2023-03-13T15:54:16.365339Z","iopub.status.idle":"2023-03-13T15:54:16.398576Z","shell.execute_reply.started":"2023-03-13T15:54:16.365292Z","shell.execute_reply":"2023-03-13T15:54:16.397569Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_input = ['Who is Illiteratendra Modi? Why does he keep with him a Rs 1 lakh pen?']\ntest_output=[1]\nmodel_load = tf.keras.models.load_model(('/kaggle/working/my_model.h5'),custom_objects={'KerasLayer':hub.KerasLayer})\n\nmodel_load.fit(test_input,test_output)\nmodel_load.predict(test_input)","metadata":{"execution":{"iopub.status.busy":"2023-03-13T16:05:23.687153Z","iopub.execute_input":"2023-03-13T16:05:23.687782Z","iopub.status.idle":"2023-03-13T16:05:24.648841Z","shell.execute_reply.started":"2023-03-13T16:05:23.687735Z","shell.execute_reply":"2023-03-13T16:05:24.647727Z"},"trusted":true},"execution_count":null,"outputs":[]}]}