{"metadata":{"kernelspec":{"name":"python3","display_name":"Python 3","language":"python"},"language_info":{"name":"python","version":"3.11.11","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"colab":{"provenance":[]},"kaggle":{"accelerator":"none","dataSources":[{"sourceType":"competition","sourceId":10737,"databundleVersionId":290346}],"dockerImageVersionId":31040,"isInternetEnabled":false,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Quora Insincere Question Classification - Deep Learning a Neural Networks\n\nOutline:\n1. Download the data from Kaggle\n2. Prepare the data for modeling using TF-IDF\n3. Train a deep learning model using PyTorch\n\n\n","metadata":{"id":"t5QtpnR223Vy"}},{"cell_type":"markdown","source":"## Download Data from Kaggle\n\nGet the API key from https://kaggle.com/me/account","metadata":{"id":"1Jjoxw6H3bLq"}},{"cell_type":"code","source":"!ls","metadata":{"id":"OuLIOvU93iRQ","outputId":"3b2896a4-1681-49b7-cb31-dc42f8b1ac77","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:02.429634Z","iopub.execute_input":"2025-06-22T04:37:02.430000Z","iopub.status.idle":"2025-06-22T04:37:02.645766Z","shell.execute_reply.started":"2025-06-22T04:37:02.429975Z","shell.execute_reply":"2025-06-22T04:37:02.644486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"!pwd","metadata":{"id":"rfghwLU3OkrE","outputId":"31e01f06-34eb-4e48-f10b-987fb511303d","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:02.647978Z","iopub.execute_input":"2025-06-22T04:37:02.648398Z","iopub.status.idle":"2025-06-22T04:37:02.899012Z","shell.execute_reply.started":"2025-06-22T04:37:02.648327Z","shell.execute_reply":"2025-06-22T04:37:02.897931Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os","metadata":{"id":"iaUvJwn0OnKd","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:02.900359Z","iopub.execute_input":"2025-06-22T04:37:02.900676Z","iopub.status.idle":"2025-06-22T04:37:02.905914Z","shell.execute_reply.started":"2025-06-22T04:37:02.900647Z","shell.execute_reply":"2025-06-22T04:37:02.904905Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:02.908483Z","iopub.execute_input":"2025-06-22T04:37:02.908754Z","iopub.status.idle":"2025-06-22T04:37:02.924438Z","shell.execute_reply.started":"2025-06-22T04:37:02.908730Z","shell.execute_reply":"2025-06-22T04:37:02.923284Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/train.csv')\ntest_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\nsub_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/sample_submission.csv')\n","metadata":{"id":"_NM8aGr83ht1","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:02.925589Z","iopub.execute_input":"2025-06-22T04:37:02.925873Z","iopub.status.idle":"2025-06-22T04:37:07.363565Z","shell.execute_reply.started":"2025-06-22T04:37:02.925837Z","shell.execute_reply":"2025-06-22T04:37:07.362723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"print(\"Train shape:\", train_df.shape)\nprint(\"Test shape:\", test_df.shape)\nprint(\"Sample submission:\", sub_df.shape)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:07.364505Z","iopub.execute_input":"2025-06-22T04:37:07.364784Z","iopub.status.idle":"2025-06-22T04:37:07.369632Z","shell.execute_reply.started":"2025-06-22T04:37:07.364759Z","shell.execute_reply":"2025-06-22T04:37:07.368675Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(os.listdir('/kaggle/input/quora-insincere-questions-classification'))\n\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:07.370413Z","iopub.execute_input":"2025-06-22T04:37:07.370649Z","iopub.status.idle":"2025-06-22T04:37:07.393523Z","shell.execute_reply.started":"2025-06-22T04:37:07.370631Z","shell.execute_reply":"2025-06-22T04:37:07.392667Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd","metadata":{"id":"v_Wo7gt22nvz","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:07.394602Z","iopub.execute_input":"2025-06-22T04:37:07.395066Z","iopub.status.idle":"2025-06-22T04:37:07.400167Z","shell.execute_reply.started":"2025-06-22T04:37:07.395033Z","shell.execute_reply":"2025-06-22T04:37:07.399006Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"data_dir = '../input/quora-insincere-questions-classification'\ntrain_fname = f'{data_dir}/train.csv'\ntest_fname = f'{data_dir}/test.csv'\nsub_fname = f'{data_dir}/sample_submission.csv'\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:07.401201Z","iopub.execute_input":"2025-06-22T04:37:07.401484Z","iopub.status.idle":"2025-06-22T04:37:07.419919Z","shell.execute_reply.started":"2025-06-22T04:37:07.401463Z","shell.execute_reply":"2025-06-22T04:37:07.418997Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_df = pd.read_csv(train_fname)\ntest_df = pd.read_csv(test_fname)\nsub_df = pd.read_csv(sub_fname)\n","metadata":{"id":"ytL1f1W6PeS9","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:07.423524Z","iopub.execute_input":"2025-06-22T04:37:07.423822Z","iopub.status.idle":"2025-06-22T04:37:11.591812Z","shell.execute_reply.started":"2025-06-22T04:37:07.423798Z","shell.execute_reply":"2025-06-22T04:37:11.590824Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_df","metadata":{"id":"rQAd20L2SwP6","outputId":"facd2550-031f-4ec7-aead-185cadd4730c","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.592910Z","iopub.execute_input":"2025-06-22T04:37:11.593267Z","iopub.status.idle":"2025-06-22T04:37:11.604911Z","shell.execute_reply.started":"2025-06-22T04:37:11.593240Z","shell.execute_reply":"2025-06-22T04:37:11.604009Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_df.sample(10)","metadata":{"id":"81nPufEVS2hz","outputId":"aae27546-11bd-43ab-f6d1-74fb3980c5e4","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.605857Z","iopub.execute_input":"2025-06-22T04:37:11.606232Z","iopub.status.idle":"2025-06-22T04:37:11.684225Z","shell.execute_reply.started":"2025-06-22T04:37:11.606201Z","shell.execute_reply":"2025-06-22T04:37:11.683383Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_df","metadata":{"id":"Y1Hmld4GS2ZP","outputId":"27711149-c963-4adf-e9db-38d7dca7d1fb","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.685057Z","iopub.execute_input":"2025-06-22T04:37:11.685389Z","iopub.status.idle":"2025-06-22T04:37:11.695322Z","shell.execute_reply.started":"2025-06-22T04:37:11.685349Z","shell.execute_reply":"2025-06-22T04:37:11.694494Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df","metadata":{"id":"46luA0WYS0Ff","outputId":"b85004b4-aa86-43de-adb6-549a89e30440","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.696274Z","iopub.execute_input":"2025-06-22T04:37:11.696640Z","iopub.status.idle":"2025-06-22T04:37:11.716183Z","shell.execute_reply.started":"2025-06-22T04:37:11.696610Z","shell.execute_reply":"2025-06-22T04:37:11.715254Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"SAMPLE_SIZE = 100_000\nsample_df = raw_df.sample(100_000)","metadata":{"id":"F7tx64YaTAvw","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.717476Z","iopub.execute_input":"2025-06-22T04:37:11.717777Z","iopub.status.idle":"2025-06-22T04:37:11.803315Z","shell.execute_reply.started":"2025-06-22T04:37:11.717755Z","shell.execute_reply":"2025-06-22T04:37:11.802580Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df","metadata":{"id":"lVI3BbIgTRzJ","outputId":"ccea0af4-32a2-4ed9-c8d1-9d8f5131f816","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.804302Z","iopub.execute_input":"2025-06-22T04:37:11.804596Z","iopub.status.idle":"2025-06-22T04:37:11.815408Z","shell.execute_reply.started":"2025-06-22T04:37:11.804576Z","shell.execute_reply":"2025-06-22T04:37:11.814547Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df.target.value_counts(normalize=True)","metadata":{"id":"Rp5vcK8xTXJn","outputId":"9c65f153-1db5-4ad3-bc59-8d8de3ddce5f","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.816432Z","iopub.execute_input":"2025-06-22T04:37:11.816722Z","iopub.status.idle":"2025-06-22T04:37:11.837437Z","shell.execute_reply.started":"2025-06-22T04:37:11.816695Z","shell.execute_reply":"2025-06-22T04:37:11.836462Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Prepare Data for Training\n\n- Convert text to TF-IDF vectors\n- Convert vectors to PyTorch tensors\n- Create PyTorch Data Loaders","metadata":{"id":"Li3Rc_tF3jHz"}},{"cell_type":"markdown","source":"### Convert to TF-IDF Vectors","metadata":{"id":"iOhQJAXOT0H1"}},{"cell_type":"code","source":"import nltk\nfrom nltk.tokenize import word_tokenize\nfrom nltk.stem import SnowballStemmer\nfrom nltk.corpus import stopwords\nnltk.download('punkt')\nnltk.download('punkt_tab')\nnltk.download('stopwords')","metadata":{"id":"1CIF9_NLUjPS","outputId":"0f6dd0ab-4197-4829-ab0d-45369ee0383d","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:37:11.838439Z","iopub.execute_input":"2025-06-22T04:37:11.838725Z","iopub.status.idle":"2025-06-22T04:38:47.890055Z","shell.execute_reply.started":"2025-06-22T04:37:11.838698Z","shell.execute_reply":"2025-06-22T04:38:47.889176Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"id":"vlRrldVUTzoT","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.891199Z","iopub.execute_input":"2025-06-22T04:38:47.891480Z","iopub.status.idle":"2025-06-22T04:38:47.895859Z","shell.execute_reply.started":"2025-06-22T04:38:47.891459Z","shell.execute_reply":"2025-06-22T04:38:47.894873Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"stemmer = SnowballStemmer(language='english')","metadata":{"id":"qaLpkdcqVex0","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.896758Z","iopub.execute_input":"2025-06-22T04:38:47.897035Z","iopub.status.idle":"2025-06-22T04:38:47.916922Z","shell.execute_reply.started":"2025-06-22T04:38:47.897015Z","shell.execute_reply":"2025-06-22T04:38:47.915958Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"english_stopwords = stopwords.words('english')","metadata":{"id":"SmlvfMU2W1kp","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.918452Z","iopub.execute_input":"2025-06-22T04:38:47.918790Z","iopub.status.idle":"2025-06-22T04:38:47.933515Z","shell.execute_reply.started":"2025-06-22T04:38:47.918760Z","shell.execute_reply":"2025-06-22T04:38:47.932386Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\", \".join(english_stopwords)","metadata":{"id":"8MVdb9fSXEbA","outputId":"9758ee30-4d3b-4dd3-a046-c5736327177d","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.934541Z","iopub.execute_input":"2025-06-22T04:38:47.934916Z","iopub.status.idle":"2025-06-22T04:38:47.950746Z","shell.execute_reply.started":"2025-06-22T04:38:47.934891Z","shell.execute_reply":"2025-06-22T04:38:47.949783Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def tokenize(text):\n  return [stemmer.stem(token) for token in word_tokenize(text)]","metadata":{"id":"cQmn5REaV2Hs","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.951883Z","iopub.execute_input":"2025-06-22T04:38:47.952236Z","iopub.status.idle":"2025-06-22T04:38:47.970058Z","shell.execute_reply.started":"2025-06-22T04:38:47.952207Z","shell.execute_reply":"2025-06-22T04:38:47.969094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(tokenizer = tokenize, stop_words=english_stopwords, max_features=1000)","metadata":{"id":"cBq0OkFq3o0M","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.971270Z","iopub.execute_input":"2025-06-22T04:38:47.971612Z","iopub.status.idle":"2025-06-22T04:38:47.987267Z","shell.execute_reply.started":"2025-06-22T04:38:47.971582Z","shell.execute_reply":"2025-06-22T04:38:47.986236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sample_df.question_text","metadata":{"id":"swX_2Ych3opG","outputId":"c31cd4cc-8d4e-4724-f42f-4ded960e9e01","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:47.988289Z","iopub.execute_input":"2025-06-22T04:38:47.988583Z","iopub.status.idle":"2025-06-22T04:38:48.008772Z","shell.execute_reply.started":"2025-06-22T04:38:47.988562Z","shell.execute_reply":"2025-06-22T04:38:48.008039Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\nvectorizer.fit(sample_df.question_text)","metadata":{"id":"aUalp0cl3ogH","outputId":"14c351d6-86ae-46bc-dd41-940e5287a14b","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:38:48.010298Z","iopub.execute_input":"2025-06-22T04:38:48.010674Z","iopub.status.idle":"2025-06-22T04:39:10.651137Z","shell.execute_reply.started":"2025-06-22T04:38:48.010643Z","shell.execute_reply":"2025-06-22T04:39:10.650258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"vectorizer.get_feature_names_out()[:100]","metadata":{"id":"YG1SAAsN3nqw","outputId":"8a9f78d2-186f-4caa-ee80-45abbee33216","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:10.652169Z","iopub.execute_input":"2025-06-22T04:39:10.652466Z","iopub.status.idle":"2025-06-22T04:39:10.660766Z","shell.execute_reply.started":"2025-06-22T04:39:10.652442Z","shell.execute_reply":"2025-06-22T04:39:10.659793Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"Transform the questions into vectors\n","metadata":{"id":"s9YJIRFLoQJ0"}},{"cell_type":"code","source":"%%time\ninputs = vectorizer.transform(sample_df.question_text)","metadata":{"id":"DYxPhiQToA14","outputId":"67c721f6-1091-42f2-e4b4-a4af0279ee79","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:10.667646Z","iopub.execute_input":"2025-06-22T04:39:10.667937Z","iopub.status.idle":"2025-06-22T04:39:33.402381Z","shell.execute_reply.started":"2025-06-22T04:39:10.667916Z","shell.execute_reply":"2025-06-22T04:39:33.401210Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs.shape","metadata":{"id":"1ienrh8pom-z","outputId":"78c5c81f-7d1b-482f-fa25-66d53a5c8f15","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:33.403272Z","iopub.execute_input":"2025-06-22T04:39:33.403602Z","iopub.status.idle":"2025-06-22T04:39:33.409787Z","shell.execute_reply.started":"2025-06-22T04:39:33.403577Z","shell.execute_reply":"2025-06-22T04:39:33.408753Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs","metadata":{"id":"VwtHGaFWopfb","outputId":"000f3e03-39c2-4072-c548-e0335a51f1ee","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:33.410897Z","iopub.execute_input":"2025-06-22T04:39:33.411204Z","iopub.status.idle":"2025-06-22T04:39:33.431974Z","shell.execute_reply.started":"2025-06-22T04:39:33.411176Z","shell.execute_reply":"2025-06-22T04:39:33.431151Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"inputs[0].toarray()[:50]","metadata":{"id":"PvibudJ_ospm","outputId":"cb8b0f23-2fe0-4cd0-b269-4265e55834c8","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:33.432861Z","iopub.execute_input":"2025-06-22T04:39:33.433185Z","iopub.status.idle":"2025-06-22T04:39:33.455565Z","shell.execute_reply.started":"2025-06-22T04:39:33.433157Z","shell.execute_reply":"2025-06-22T04:39:33.454488Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"%%time\ntest_inputs = vectorizer.transform(test_df.question_text)","metadata":{"id":"r0Nwf61toAyU","outputId":"75e77af2-f91c-4082-f5f2-a22b48892a27","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:39:33.456808Z","iopub.execute_input":"2025-06-22T04:39:33.457232Z","iopub.status.idle":"2025-06-22T04:40:58.488213Z","shell.execute_reply.started":"2025-06-22T04:39:33.457203Z","shell.execute_reply":"2025-06-22T04:40:58.487394Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_inputs.shape","metadata":{"id":"5DBNOZEWoAuv","outputId":"09bba1c3-0274-4bbd-a039-d7db20665dcd","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.489109Z","iopub.execute_input":"2025-06-22T04:40:58.489387Z","iopub.status.idle":"2025-06-22T04:40:58.495257Z","shell.execute_reply.started":"2025-06-22T04:40:58.489341Z","shell.execute_reply":"2025-06-22T04:40:58.494403Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Split the Training and Validation Set","metadata":{"id":"A6dRDemrpO5s"}},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split","metadata":{"id":"h6dn6FCGpCcb","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.496291Z","iopub.execute_input":"2025-06-22T04:40:58.497334Z","iopub.status.idle":"2025-06-22T04:40:58.509614Z","shell.execute_reply.started":"2025-06-22T04:40:58.497304Z","shell.execute_reply":"2025-06-22T04:40:58.508633Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"targets = sample_df.target","metadata":{"id":"sCavePHlwslR","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.510608Z","iopub.execute_input":"2025-06-22T04:40:58.510944Z","iopub.status.idle":"2025-06-22T04:40:58.528488Z","shell.execute_reply.started":"2025-06-22T04:40:58.510914Z","shell.execute_reply":"2025-06-22T04:40:58.527513Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_inputs, val_inputs, train_targets, val_targets = train_test_split(inputs, targets, test_size = 0.3)","metadata":{"id":"NEgfaKgFwsZ3","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.529559Z","iopub.execute_input":"2025-06-22T04:40:58.529828Z","iopub.status.idle":"2025-06-22T04:40:58.560263Z","shell.execute_reply.started":"2025-06-22T04:40:58.529797Z","shell.execute_reply":"2025-06-22T04:40:58.559355Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_inputs.shape","metadata":{"id":"UXu8uXHh0NBt","outputId":"22b12a23-9ef1-4db8-9287-a52873c22ffe","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.561325Z","iopub.execute_input":"2025-06-22T04:40:58.561606Z","iopub.status.idle":"2025-06-22T04:40:58.567182Z","shell.execute_reply.started":"2025-06-22T04:40:58.561586Z","shell.execute_reply":"2025-06-22T04:40:58.566453Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_inputs.shape","metadata":{"id":"uJbtS9GC0MpR","outputId":"5f721a50-d2bd-430d-8a4b-fe4bd664b972","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.568124Z","iopub.execute_input":"2025-06-22T04:40:58.568402Z","iopub.status.idle":"2025-06-22T04:40:58.584664Z","shell.execute_reply.started":"2025-06-22T04:40:58.568356Z","shell.execute_reply":"2025-06-22T04:40:58.583763Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_targets","metadata":{"id":"lT77aXAn0YgK","outputId":"89ad3b77-707e-4e07-9c98-d13ff4fb4465","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.585766Z","iopub.execute_input":"2025-06-22T04:40:58.586002Z","iopub.status.idle":"2025-06-22T04:40:58.600236Z","shell.execute_reply.started":"2025-06-22T04:40:58.585983Z","shell.execute_reply":"2025-06-22T04:40:58.599163Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_targets","metadata":{"id":"B7_7-y-A0YHw","outputId":"a08ceb02-5a62-4f05-aa31-7a44b20825fe","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.601170Z","iopub.execute_input":"2025-06-22T04:40:58.601501Z","iopub.status.idle":"2025-06-22T04:40:58.617777Z","shell.execute_reply.started":"2025-06-22T04:40:58.601474Z","shell.execute_reply":"2025-06-22T04:40:58.616961Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Convert to PyTorch Tensors","metadata":{"id":"lIwelzI50hE6"}},{"cell_type":"code","source":"import torch","metadata":{"id":"O-DbkrQ70pUu","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.618947Z","iopub.execute_input":"2025-06-22T04:40:58.619281Z","iopub.status.idle":"2025-06-22T04:40:58.634668Z","shell.execute_reply.started":"2025-06-22T04:40:58.619257Z","shell.execute_reply":"2025-06-22T04:40:58.633616Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_input_tensors = torch.tensor(train_inputs.toarray()).float()\nval_input_tensors = torch.tensor(val_inputs.toarray()).float()","metadata":{"id":"iMLuHNQV0pRO","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:40:58.635758Z","iopub.execute_input":"2025-06-22T04:40:58.636071Z","iopub.status.idle":"2025-06-22T04:41:00.556817Z","shell.execute_reply.started":"2025-06-22T04:40:58.636047Z","shell.execute_reply":"2025-06-22T04:41:00.555876Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_input_tensors.shape","metadata":{"id":"W0Esl5ZW1Mq0","outputId":"6aab9df1-964c-4fd8-9a97-ddfb0cbdcd83","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:00.557847Z","iopub.execute_input":"2025-06-22T04:41:00.558110Z","iopub.status.idle":"2025-06-22T04:41:00.563708Z","shell.execute_reply.started":"2025-06-22T04:41:00.558090Z","shell.execute_reply":"2025-06-22T04:41:00.563055Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"val_input_tensors.shape","metadata":{"id":"POncnQKX1MdX","outputId":"bab1bcd6-c1c7-4087-bec0-8fb32d71614f","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:00.564546Z","iopub.execute_input":"2025-06-22T04:41:00.564889Z","iopub.status.idle":"2025-06-22T04:41:00.582000Z","shell.execute_reply.started":"2025-06-22T04:41:00.564860Z","shell.execute_reply":"2025-06-22T04:41:00.581128Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"type(train_targets)","metadata":{"id":"uZuxUdLY19f2","outputId":"4065748b-e641-4872-a578-d4fbb5ff9ab2","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:00.583077Z","iopub.execute_input":"2025-06-22T04:41:00.583649Z","iopub.status.idle":"2025-06-22T04:41:00.598508Z","shell.execute_reply.started":"2025-06-22T04:41:00.583626Z","shell.execute_reply":"2025-06-22T04:41:00.597764Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_target_tensors = torch.tensor(train_targets.values).float()\nval_target_tensors = torch.tensor(val_targets.values).float()","metadata":{"id":"1cvkgD_K1yb-","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:00.599287Z","iopub.execute_input":"2025-06-22T04:41:00.599575Z","iopub.status.idle":"2025-06-22T04:41:00.614244Z","shell.execute_reply.started":"2025-06-22T04:41:00.599556Z","shell.execute_reply":"2025-06-22T04:41:00.613271Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_input_tensors = torch.tensor(test_inputs.toarray()).float()","metadata":{"id":"UeI5zeQ63bHC","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:00.615335Z","iopub.execute_input":"2025-06-22T04:41:00.615699Z","iopub.status.idle":"2025-06-22T04:41:07.807691Z","shell.execute_reply.started":"2025-06-22T04:41:00.615669Z","shell.execute_reply":"2025-06-22T04:41:07.806826Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Create PyTorch Data Loaders","metadata":{"id":"LqLuE0K-3YW0"}},{"cell_type":"code","source":"from torch.utils.data import TensorDataset, DataLoader","metadata":{"id":"13_h9lHt1L9x","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:07.808584Z","iopub.execute_input":"2025-06-22T04:41:07.808850Z","iopub.status.idle":"2025-06-22T04:41:07.813160Z","shell.execute_reply.started":"2025-06-22T04:41:07.808830Z","shell.execute_reply":"2025-06-22T04:41:07.812421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_ds = TensorDataset(train_input_tensors, train_target_tensors)\nval_ds = TensorDataset(val_input_tensors, val_target_tensors)\ntest_ds = TensorDataset(test_input_tensors)","metadata":{"id":"fTWAX7Sp0gsw","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:07.814053Z","iopub.execute_input":"2025-06-22T04:41:07.814322Z","iopub.status.idle":"2025-06-22T04:41:07.837883Z","shell.execute_reply.started":"2025-06-22T04:41:07.814303Z","shell.execute_reply":"2025-06-22T04:41:07.836828Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"BATCH_SIZE = 128","metadata":{"id":"BKO2kPbR6kbX","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:07.839062Z","iopub.execute_input":"2025-06-22T04:41:07.839472Z","iopub.status.idle":"2025-06-22T04:41:07.857641Z","shell.execute_reply.started":"2025-06-22T04:41:07.839441Z","shell.execute_reply":"2025-06-22T04:41:07.856723Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dl = DataLoader(train_ds, batch_size = BATCH_SIZE, shuffle=True)\nval_dl = DataLoader(val_ds, batch_size = BATCH_SIZE)\ntest_dl = DataLoader(test_ds, batch_size = BATCH_SIZE)","metadata":{"id":"nUU70zF96dyJ","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:07.858611Z","iopub.execute_input":"2025-06-22T04:41:07.858859Z","iopub.status.idle":"2025-06-22T04:41:08.010091Z","shell.execute_reply.started":"2025-06-22T04:41:07.858839Z","shell.execute_reply":"2025-06-22T04:41:08.009289Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for batch in train_dl:\n  batch_inputs = batch[0]\n  batch_targets = batch[1]\n  print('batch_inputs.shape', batch_inputs.shape)\n  print('batch_targets.shape', batch_targets.shape)\n  break","metadata":{"id":"IcudP44w6_T7","outputId":"75ad057e-0a82-4d4f-ff51-9d4d7e21d7b9","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.011236Z","iopub.execute_input":"2025-06-22T04:41:08.011515Z","iopub.status.idle":"2025-06-22T04:41:08.027657Z","shell.execute_reply.started":"2025-06-22T04:41:08.011494Z","shell.execute_reply":"2025-06-22T04:41:08.026648Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(train_dl)","metadata":{"id":"uyXjpnLk6-N1","outputId":"7f9b2e92-3044-45ff-e221-99be4bc1bf83","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.028961Z","iopub.execute_input":"2025-06-22T04:41:08.029279Z","iopub.status.idle":"2025-06-22T04:41:08.038172Z","shell.execute_reply.started":"2025-06-22T04:41:08.029256Z","shell.execute_reply":"2025-06-22T04:41:08.037299Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"547*128","metadata":{"id":"mAuK4tGr6dMu","outputId":"98b70fbe-2519-472f-d7b7-de6ea4acebae","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.039016Z","iopub.execute_input":"2025-06-22T04:41:08.039241Z","iopub.status.idle":"2025-06-22T04:41:08.057285Z","shell.execute_reply.started":"2025-06-22T04:41:08.039223Z","shell.execute_reply":"2025-06-22T04:41:08.056327Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Train Deep Learning Model","metadata":{"id":"XX9urHzA3pbx"}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F","metadata":{"id":"-u_d5wM93u-x","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.058288Z","iopub.execute_input":"2025-06-22T04:41:08.058644Z","iopub.status.idle":"2025-06-22T04:41:08.072442Z","shell.execute_reply.started":"2025-06-22T04:41:08.058616Z","shell.execute_reply":"2025-06-22T04:41:08.071429Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class QuoraNet(nn.Module):\n  def __init__(self):\n    super().__init__()\n    self.layer1 = nn.Linear(1000, 512)\n    self.layer2 = nn.Linear(512, 256)\n    self.layer3 = nn.Linear(256, 128)\n    self.layer4 = nn.Linear(128, 1)\n    pass\n\n  def forward(self, inputs):\n    out = self.layer1(inputs)\n    out = F.relu(out)\n    out = self.layer2(out)\n    out = F.relu(out)\n    out = self.layer3(out)\n    out = F.relu(out)\n    out = self.layer4(out)\n    return out\n","metadata":{"id":"przoalL_3vnP","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.073425Z","iopub.execute_input":"2025-06-22T04:41:08.073741Z","iopub.status.idle":"2025-06-22T04:41:08.091201Z","shell.execute_reply.started":"2025-06-22T04:41:08.073714Z","shell.execute_reply":"2025-06-22T04:41:08.090147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = QuoraNet()","metadata":{"id":"Hf9P0PS83vaO","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.092180Z","iopub.execute_input":"2025-06-22T04:41:08.092535Z","iopub.status.idle":"2025-06-22T04:41:08.115587Z","shell.execute_reply.started":"2025-06-22T04:41:08.092475Z","shell.execute_reply":"2025-06-22T04:41:08.114629Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score","metadata":{"id":"u45ZX_WrC-vd","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.116598Z","iopub.execute_input":"2025-06-22T04:41:08.116890Z","iopub.status.idle":"2025-06-22T04:41:08.124223Z","shell.execute_reply.started":"2025-06-22T04:41:08.116869Z","shell.execute_reply":"2025-06-22T04:41:08.122868Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for batch in train_dl:\n  bi, bt = batch\n  print('inputs.shape', bi.shape)\n  print('targets.shape', bt.shape)\n\n  bo = model(bi)\n  print('bo.shape', bo.shape)\n\n  # Convert outputs to probabilities\n  probs = torch.sigmoid(bo[:, 0])\n  print('probs', probs[:10])\n\n  #Convert probs to predictions\n  preds = (probs > 0.5).int()\n  print('preds', preds[:10])\n  print('targets', bt[:10])\n\n  # Check metrics\n  print('accuracy', accuracy_score(bt, preds))\n  print('f1_score', f1_score(bt, preds))\n\n  #Loss\n  print('loss', F.binary_cross_entropy(preds.float(), bt))\n\n  break","metadata":{"id":"Q8RHKU1VA3qo","outputId":"e6139148-19de-4ee9-8ba1-1faeaab24cf8","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.125269Z","iopub.execute_input":"2025-06-22T04:41:08.125560Z","iopub.status.idle":"2025-06-22T04:41:08.158590Z","shell.execute_reply.started":"2025-06-22T04:41:08.125538Z","shell.execute_reply":"2025-06-22T04:41:08.157639Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bi[:10]","metadata":{"id":"MvPZle1EA3nA","outputId":"33cbb80a-5c04-46e4-cabc-c618fe5943ac","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.159733Z","iopub.execute_input":"2025-06-22T04:41:08.160092Z","iopub.status.idle":"2025-06-22T04:41:08.168699Z","shell.execute_reply.started":"2025-06-22T04:41:08.160065Z","shell.execute_reply":"2025-06-22T04:41:08.167666Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bt[:10]","metadata":{"id":"LYrEq_VjA3j-","outputId":"158f1e81-817e-484b-b9e1-bc71f81ebb4f","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.169684Z","iopub.execute_input":"2025-06-22T04:41:08.169992Z","iopub.status.idle":"2025-06-22T04:41:08.186540Z","shell.execute_reply.started":"2025-06-22T04:41:08.169963Z","shell.execute_reply":"2025-06-22T04:41:08.185792Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"bo[:10]","metadata":{"id":"GSyDJN_BA3gx","outputId":"3acd3c12-4d5e-48f1-ba25-16abe56159c6","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.187471Z","iopub.execute_input":"2025-06-22T04:41:08.187879Z","iopub.status.idle":"2025-06-22T04:41:08.203834Z","shell.execute_reply.started":"2025-06-22T04:41:08.187857Z","shell.execute_reply":"2025-06-22T04:41:08.203040Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Evaluate model performance\ndef evaluate(model, dl):\n  losses, accs, f1s = [], [], []\n  # Loop over batches\n  for batch in dl:\n    #Get inputs through model\n    inputs, targets = batch\n\n    # Pass inputs through model\n    outputs = model(inputs)\n\n    # Convert to probabilities\n    probs = torch.sigmoid(outputs[:, 0])\n\n    # Compute loss\n    loss = F.binary_cross_entropy(probs, targets, weight=torch.tensor(20))\n\n    # Compute preds\n    preds = (probs > 0.5).int()\n\n    # Compute accuracy\n    acc = accuracy_score(targets, preds)\n\n    # Compute f1 score\n    f1 = f1_score(targets, preds)\n\n    losses.append(loss.item())\n    accs.append(acc)\n    f1s.append(f1)\n\n  return (torch.mean(torch.tensor(losses)).item(),\n          torch.mean(torch.tensor(accs)).item(),\n          torch.mean(torch.tensor(f1s)).item())","metadata":{"id":"-KtCvjw63wHS","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.204780Z","iopub.execute_input":"2025-06-22T04:41:08.205108Z","iopub.status.idle":"2025-06-22T04:41:08.220779Z","shell.execute_reply.started":"2025-06-22T04:41:08.205087Z","shell.execute_reply":"2025-06-22T04:41:08.219892Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluate(model, train_dl)","metadata":{"id":"Gqi5IqE7Fr1R","outputId":"5ea6b437-c843-42d0-96bd-5b3a19e9e4ef","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:08.222166Z","iopub.execute_input":"2025-06-22T04:41:08.222480Z","iopub.status.idle":"2025-06-22T04:41:11.787515Z","shell.execute_reply.started":"2025-06-22T04:41:08.222456Z","shell.execute_reply":"2025-06-22T04:41:11.786341Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"evaluate(model, val_dl)","metadata":{"id":"9eEaQMQDFtQL","outputId":"6cc35556-58c9-4910-cc52-d6ff57dc15fa","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:11.788943Z","iopub.execute_input":"2025-06-22T04:41:11.789280Z","iopub.status.idle":"2025-06-22T04:41:13.367716Z","shell.execute_reply.started":"2025-06-22T04:41:11.789242Z","shell.execute_reply":"2025-06-22T04:41:13.366808Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# Train the model batch by batch\ndef fit(epochs, lr, model, train_dl, val_dl ):\n  history = []\n  optimizer = torch.optim.Adam(model.parameters(), lr, weight_decay=1e-5)\n\n  for epoch in range(epochs):\n    #Training phase\n    for batch in train_dl:\n      #Get inputs and targets\n      inputs, targets = batch\n\n      #GEt model outputs\n      outputs = model(inputs)\n\n      #Get probabilities\n      probs = torch.sigmoid(outputs[:, 0])\n\n      #Compute loss\n      loss = F.binary_cross_entropy(probs, targets, weight=torch.tensor(20))\n\n      #Perform the optimization\n      loss.backward()\n      optimizer.step()\n      optimizer.zero_grad()\n\n   # Evaluation phase\n    loss, acc, f1 = evaluate(model, val_dl)\n    print('Epoch {}; Loss: {:.4f}; Accuracy: {:.4f}; F1 Score: {:.4f}'.format(\n         epoch+1, loss, acc, f1))\n    history.append([loss, acc, f1])\n  return history\n\n\n","metadata":{"id":"jXuOyq46FrqI","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:13.368727Z","iopub.execute_input":"2025-06-22T04:41:13.368974Z","iopub.status.idle":"2025-06-22T04:41:13.376201Z","shell.execute_reply.started":"2025-06-22T04:41:13.368954Z","shell.execute_reply":"2025-06-22T04:41:13.375236Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = QuoraNet()","metadata":{"id":"IacJHVcUXywJ","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:13.377099Z","iopub.execute_input":"2025-06-22T04:41:13.377400Z","iopub.status.idle":"2025-06-22T04:41:13.405829Z","shell.execute_reply.started":"2025-06-22T04:41:13.377354Z","shell.execute_reply":"2025-06-22T04:41:13.404977Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history = []","metadata":{"id":"1ntoMUSsXysy","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:13.407347Z","iopub.execute_input":"2025-06-22T04:41:13.407642Z","iopub.status.idle":"2025-06-22T04:41:13.412429Z","shell.execute_reply.started":"2025-06-22T04:41:13.407618Z","shell.execute_reply":"2025-06-22T04:41:13.411466Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history.append(evaluate(model, train_dl))","metadata":{"id":"_RIoowKEX7px","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:13.413480Z","iopub.execute_input":"2025-06-22T04:41:13.413760Z","iopub.status.idle":"2025-06-22T04:41:17.285991Z","shell.execute_reply.started":"2025-06-22T04:41:13.413733Z","shell.execute_reply":"2025-06-22T04:41:17.285010Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history","metadata":{"id":"BYEZIEHSXykD","outputId":"8e14b129-5410-4ade-a01e-eaedbd015377","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:17.287053Z","iopub.execute_input":"2025-06-22T04:41:17.287416Z","iopub.status.idle":"2025-06-22T04:41:17.294988Z","shell.execute_reply.started":"2025-06-22T04:41:17.287382Z","shell.execute_reply":"2025-06-22T04:41:17.293506Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history += fit(5, 0.0001, model, train_dl, val_dl)","metadata":{"id":"pd9CuHP2FrQP","outputId":"a134ec2d-8ccb-4795-f683-1a9f43f5aaaf","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:17.296210Z","iopub.execute_input":"2025-06-22T04:41:17.296509Z","iopub.status.idle":"2025-06-22T04:41:53.153402Z","shell.execute_reply.started":"2025-06-22T04:41:17.296487Z","shell.execute_reply":"2025-06-22T04:41:53.152407Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"history","metadata":{"id":"3HR-2bzZWNhE","outputId":"c448da78-1592-48a5-94db-806746b72b2b","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.154458Z","iopub.execute_input":"2025-06-22T04:41:53.154786Z","iopub.status.idle":"2025-06-22T04:41:53.160621Z","shell.execute_reply.started":"2025-06-22T04:41:53.154759Z","shell.execute_reply":"2025-06-22T04:41:53.159662Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"losses = [item[0] for item in history]\naccs = [item[1] for item in history]\nf1s = [item[2] for item in history]","metadata":{"id":"mvwgoL-eZcYh","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.161592Z","iopub.execute_input":"2025-06-22T04:41:53.161832Z","iopub.status.idle":"2025-06-22T04:41:53.179684Z","shell.execute_reply.started":"2025-06-22T04:41:53.161811Z","shell.execute_reply":"2025-06-22T04:41:53.178757Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import matplotlib.pyplot as plt","metadata":{"id":"g9JfuAGrZXPj","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.180615Z","iopub.execute_input":"2025-06-22T04:41:53.180941Z","iopub.status.idle":"2025-06-22T04:41:53.203398Z","shell.execute_reply.started":"2025-06-22T04:41:53.180914Z","shell.execute_reply":"2025-06-22T04:41:53.202444Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.title('Loss')\nplt.plot(losses)","metadata":{"id":"qwMvsVHgZ2s9","outputId":"a46aec18-d88e-48a1-bc4f-9eddb487b246","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.204421Z","iopub.execute_input":"2025-06-22T04:41:53.204729Z","iopub.status.idle":"2025-06-22T04:41:53.407435Z","shell.execute_reply.started":"2025-06-22T04:41:53.204701Z","shell.execute_reply":"2025-06-22T04:41:53.406573Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.title('Accuracy')\nplt.plot(accs)","metadata":{"id":"8iYuvqYuZ7rK","outputId":"646bc23b-cbed-4a5b-9f1f-b0aa3edfd212","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.408399Z","iopub.execute_input":"2025-06-22T04:41:53.408647Z","iopub.status.idle":"2025-06-22T04:41:53.611930Z","shell.execute_reply.started":"2025-06-22T04:41:53.408627Z","shell.execute_reply":"2025-06-22T04:41:53.611021Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"plt.title('F1 Score')\nplt.plot(f1s)","metadata":{"id":"W2zEnbwra1aM","outputId":"740d0c14-71a3-4111-89ae-5b5e69e41e4c","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.612936Z","iopub.execute_input":"2025-06-22T04:41:53.613190Z","iopub.status.idle":"2025-06-22T04:41:53.792682Z","shell.execute_reply.started":"2025-06-22T04:41:53.613171Z","shell.execute_reply":"2025-06-22T04:41:53.791730Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import os\nprint(os.listdir())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.793908Z","iopub.execute_input":"2025-06-22T04:41:53.794219Z","iopub.status.idle":"2025-06-22T04:41:53.799671Z","shell.execute_reply.started":"2025-06-22T04:41:53.794190Z","shell.execute_reply":"2025-06-22T04:41:53.798766Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"### Make predictions on example data","metadata":{"id":"CX8QzkBKbIR4"}},{"cell_type":"code","source":"small_df = raw_df.sample(20)","metadata":{"id":"0QRlyf0NbHrE","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.800591Z","iopub.execute_input":"2025-06-22T04:41:53.801018Z","iopub.status.idle":"2025-06-22T04:41:53.872635Z","shell.execute_reply.started":"2025-06-22T04:41:53.800992Z","shell.execute_reply":"2025-06-22T04:41:53.871796Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"small_df","metadata":{"id":"T0Xbd4SSbHcl","outputId":"f37b93a6-9f2c-4f93-a0bb-99093a8cdb49","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.873573Z","iopub.execute_input":"2025-06-22T04:41:53.873899Z","iopub.status.idle":"2025-06-22T04:41:53.884691Z","shell.execute_reply.started":"2025-06-22T04:41:53.873869Z","shell.execute_reply":"2025-06-22T04:41:53.883760Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"\ndef predict_df(df):\n  inputs = vectorizer.transform(df.question_text)\n  inputs_tensors = torch.tensor(inputs.toarray()).float()\n  outputs = model(inputs_tensors)\n  probs = torch.sigmoid(outputs[:, 0])\n  preds = (probs > 0.5).int()\n  return preds","metadata":{"id":"OfnZra37bDSb","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.885793Z","iopub.execute_input":"2025-06-22T04:41:53.886263Z","iopub.status.idle":"2025-06-22T04:41:53.899380Z","shell.execute_reply.started":"2025-06-22T04:41:53.886240Z","shell.execute_reply":"2025-06-22T04:41:53.898351Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"small_df.target.values","metadata":{"id":"UYyGRG1Qcsel","outputId":"a9dacc9e-ff2d-48c4-dcb9-3e51a8cb1612","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.908803Z","iopub.execute_input":"2025-06-22T04:41:53.909908Z","iopub.status.idle":"2025-06-22T04:41:53.917996Z","shell.execute_reply.started":"2025-06-22T04:41:53.909850Z","shell.execute_reply":"2025-06-22T04:41:53.917094Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_df(small_df)","metadata":{"id":"s-Sh8RZkcGur","outputId":"c0dfe6b2-d3e3-488b-f337-2e78dbad51ca","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.918947Z","iopub.execute_input":"2025-06-22T04:41:53.919222Z","iopub.status.idle":"2025-06-22T04:41:53.945512Z","shell.execute_reply.started":"2025-06-22T04:41:53.919200Z","shell.execute_reply":"2025-06-22T04:41:53.944638Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"small_df.question_text.values","metadata":{"id":"9RuaMwn-cN6O","outputId":"749314fe-4d00-49e4-c161-d6f8fc5ba0f5","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.946524Z","iopub.execute_input":"2025-06-22T04:41:53.946861Z","iopub.status.idle":"2025-06-22T04:41:53.960999Z","shell.execute_reply.started":"2025-06-22T04:41:53.946821Z","shell.execute_reply":"2025-06-22T04:41:53.960142Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def predict_text(text):\n  df = pd.DataFrame({'question_text': [text]})\n  inputs = vectorizer.transform(df.question_text)\n  inputs_tensors = torch.tensor(inputs.toarray()).float()\n  outputs = model(inputs_tensors)\n  probs = torch.sigmoid(outputs[:, 0])\n  preds = (probs > 0.5).int()\n  return preds","metadata":{"id":"6E4KnAkxc5mH","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.962096Z","iopub.execute_input":"2025-06-22T04:41:53.962442Z","iopub.status.idle":"2025-06-22T04:41:53.976959Z","shell.execute_reply.started":"2025-06-22T04:41:53.962411Z","shell.execute_reply":"2025-06-22T04:41:53.976020Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_text(\"What is the function of a plasma cell?\")","metadata":{"id":"6ZA6u7hld_KW","outputId":"0af5b9e1-2954-41bb-96d6-20a98a44804d","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.978039Z","iopub.execute_input":"2025-06-22T04:41:53.978344Z","iopub.status.idle":"2025-06-22T04:41:53.997956Z","shell.execute_reply.started":"2025-06-22T04:41:53.978323Z","shell.execute_reply":"2025-06-22T04:41:53.996954Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"predict_text(\"Why can't liberals realize that they're stupid?\")","metadata":{"id":"3JFK6UTWeFdH","outputId":"b7b6de71-1a54-49b4-d3bc-763d222900f8","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:53.999001Z","iopub.execute_input":"2025-06-22T04:41:53.999313Z","iopub.status.idle":"2025-06-22T04:41:54.016560Z","shell.execute_reply.started":"2025-06-22T04:41:53.999291Z","shell.execute_reply":"2025-06-22T04:41:54.015456Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"## Generate Predictions for Test Set","metadata":{"id":"RuhrRgTxeYSs"}},{"cell_type":"code","source":"test_inputs","metadata":{"id":"SAwP5tP3eOkO","outputId":"6ec96c58-95d3-4211-dead-dfbc65cc2f3c","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:54.017688Z","iopub.execute_input":"2025-06-22T04:41:54.017959Z","iopub.status.idle":"2025-06-22T04:41:54.033451Z","shell.execute_reply.started":"2025-06-22T04:41:54.017931Z","shell.execute_reply":"2025-06-22T04:41:54.032521Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import numpy as np\ndef make_preds(dl):\n  all_preds = []\n  for batch in dl:\n    inputs = batch[0]\n    outputs = model(inputs)\n    probs = torch.sigmoid(outputs[:, 0])\n    preds = (probs > 0.5).int()\n    all_preds.append(preds.detach().numpy())\n\n  return np.concatenate(all_preds)","metadata":{"id":"xt0Z-LPGeeqj","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:54.034435Z","iopub.execute_input":"2025-06-22T04:41:54.034972Z","iopub.status.idle":"2025-06-22T04:41:54.049488Z","shell.execute_reply.started":"2025-06-22T04:41:54.034947Z","shell.execute_reply":"2025-06-22T04:41:54.048447Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"for batch in test_dl:\n  print(batch[0][0])\n  break","metadata":{"id":"lkLRHiceh-mf","outputId":"a4b18f74-7f66-4d42-fff2-fb07e8f391fe","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:54.050615Z","iopub.execute_input":"2025-06-22T04:41:54.050887Z","iopub.status.idle":"2025-06-22T04:41:54.068355Z","shell.execute_reply.started":"2025-06-22T04:41:54.050867Z","shell.execute_reply":"2025-06-22T04:41:54.067412Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds = make_preds(test_dl)","metadata":{"id":"JLev7enBfEfP","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:41:54.069278Z","iopub.execute_input":"2025-06-22T04:41:54.069571Z","iopub.status.idle":"2025-06-22T04:42:05.290388Z","shell.execute_reply.started":"2025-06-22T04:41:54.069550Z","shell.execute_reply":"2025-06-22T04:42:05.289489Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"len(test_preds)","metadata":{"id":"9tnQfGmIfIXa","outputId":"00f94284-86d4-4ad5-9e5f-d7125d7e08e0","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.291553Z","iopub.execute_input":"2025-06-22T04:42:05.291892Z","iopub.status.idle":"2025-06-22T04:42:05.298238Z","shell.execute_reply.started":"2025-06-22T04:42:05.291852Z","shell.execute_reply":"2025-06-22T04:42:05.297192Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_preds","metadata":{"id":"A48q6QMWgAdF","outputId":"ef23907e-2afa-496e-f99b-aaaf1971b53f","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.299162Z","iopub.execute_input":"2025-06-22T04:42:05.299495Z","iopub.status.idle":"2025-06-22T04:42:05.318571Z","shell.execute_reply.started":"2025-06-22T04:42:05.299474Z","shell.execute_reply":"2025-06-22T04:42:05.317665Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df","metadata":{"id":"aCJ2hQU8hxqN","outputId":"7932beab-ba74-4cea-8a60-2dbcaccf21eb","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.319465Z","iopub.execute_input":"2025-06-22T04:42:05.319709Z","iopub.status.idle":"2025-06-22T04:42:05.340787Z","shell.execute_reply.started":"2025-06-22T04:42:05.319691Z","shell.execute_reply":"2025-06-22T04:42:05.339722Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df['prediction'] = test_preds\n","metadata":{"id":"SufffiP_iRQl","outputId":"9e364b71-7af3-446c-ce91-bd0fef5ab6aa","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.341755Z","iopub.execute_input":"2025-06-22T04:42:05.342067Z","iopub.status.idle":"2025-06-22T04:42:05.358114Z","shell.execute_reply.started":"2025-06-22T04:42:05.342039Z","shell.execute_reply":"2025-06-22T04:42:05.357190Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"pd.Series(test_preds).value_counts()","metadata":{"id":"xweyICBKhhE9","outputId":"40cb89b3-2ae5-461e-a7c3-7b29f8adabc3","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.359098Z","iopub.execute_input":"2025-06-22T04:42:05.359410Z","iopub.status.idle":"2025-06-22T04:42:05.379294Z","shell.execute_reply.started":"2025-06-22T04:42:05.359360Z","shell.execute_reply":"2025-06-22T04:42:05.378434Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df.prediction.value_counts()","metadata":{"id":"705fh6CdiXZP","outputId":"3ef23df6-efd7-4dcf-c241-537467d37740","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.380448Z","iopub.execute_input":"2025-06-22T04:42:05.380766Z","iopub.status.idle":"2025-06-22T04:42:05.400508Z","shell.execute_reply.started":"2025-06-22T04:42:05.380741Z","shell.execute_reply":"2025-06-22T04:42:05.399618Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"sub_df.to_csv('submission.csv', index=False)\n","metadata":{"id":"KBAwAY_PiXC1","trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.401461Z","iopub.execute_input":"2025-06-22T04:42:05.401708Z","iopub.status.idle":"2025-06-22T04:42:05.876781Z","shell.execute_reply.started":"2025-06-22T04:42:05.401691Z","shell.execute_reply":"2025-06-22T04:42:05.875695Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\ndf = pd.read_csv('submission.csv')\nprint(\"Submission shape:\", df.shape)\nprint(\"Columns:\", df.columns)\nprint(df.head())\nprint(df['prediction'].value_counts())\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:05.877876Z","iopub.execute_input":"2025-06-22T04:42:05.878212Z","iopub.status.idle":"2025-06-22T04:42:06.152258Z","shell.execute_reply.started":"2025-06-22T04:42:05.878180Z","shell.execute_reply":"2025-06-22T04:42:06.151226Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pandas as pd\n\n# Load test data\ntest_df = pd.read_csv('/kaggle/input/quora-insincere-questions-classification/test.csv')\n\n# Create the correct submission file\nsubmission = pd.DataFrame({\n    'qid': test_df['qid'],\n    'prediction': test_preds  # this must be a list/array of 0s and 1s\n})\n\n# Save to CSV\nsubmission.to_csv('submission.csv', index=False)\n","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-06-22T04:42:44.588249Z","iopub.execute_input":"2025-06-22T04:42:44.588587Z","iopub.status.idle":"2025-06-22T04:42:46.075766Z","shell.execute_reply.started":"2025-06-22T04:42:44.588567Z","shell.execute_reply":"2025-06-22T04:42:46.074733Z"}},"outputs":[],"execution_count":null}]}