{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"nvidiaTeslaT4","dataSources":[{"sourceId":10737,"databundleVersionId":290346,"sourceType":"competition"}],"dockerImageVersionId":30746,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport seaborn as sns\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-08-01T19:19:31.198128Z","iopub.execute_input":"2024-08-01T19:19:31.198752Z","iopub.status.idle":"2024-08-01T19:19:32.069907Z","shell.execute_reply.started":"2024-08-01T19:19:31.198712Z","shell.execute_reply":"2024-08-01T19:19:32.068805Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# !unzip /kaggle/input/quora-insincere-questions-classification/embeddings.zip","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:34.283537Z","iopub.execute_input":"2024-08-01T19:19:34.284044Z","iopub.status.idle":"2024-08-01T19:19:34.288215Z","shell.execute_reply.started":"2024-08-01T19:19:34.284011Z","shell.execute_reply":"2024-08-01T19:19:34.287191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/train.csv\")\ntrain_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:34.953380Z","iopub.execute_input":"2024-08-01T19:19:34.954034Z","iopub.status.idle":"2024-08-01T19:19:37.703341Z","shell.execute_reply.started":"2024-08-01T19:19:34.953979Z","shell.execute_reply":"2024-08-01T19:19:37.702394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.memory_usage(deep = True).sum()/1024**2","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:37.704934Z","iopub.execute_input":"2024-08-01T19:19:37.705191Z","iopub.status.idle":"2024-08-01T19:19:38.400303Z","shell.execute_reply.started":"2024-08-01T19:19:37.705169Z","shell.execute_reply":"2024-08-01T19:19:38.399201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch \nfrom sklearn.model_selection import train_test_split\nfrom sklearn import metrics","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:38.401619Z","iopub.execute_input":"2024-08-01T19:19:38.401947Z","iopub.status.idle":"2024-08-01T19:19:40.427150Z","shell.execute_reply.started":"2024-08-01T19:19:38.401918Z","shell.execute_reply":"2024-08-01T19:19:40.426358Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/test.csv\")\nprint(\"Train shape : \",train_df.shape)\nprint(\"Test shape : \",test_df.shape)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:40.429201Z","iopub.execute_input":"2024-08-01T19:19:40.429660Z","iopub.status.idle":"2024-08-01T19:19:41.201809Z","shell.execute_reply.started":"2024-08-01T19:19:40.429632Z","shell.execute_reply":"2024-08-01T19:19:41.200863Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tokenize = {'oov' : 0}\n# counter = 1\n# def tokenizer_class(text):\n#     global counter\n#     text_split = text.split(' ')\n#     for txt in text_split:\n#         txt = txt.lower()\n#         try:\n#             print(tokenize[txt])\n#         except:\n#             tokenize[txt] = counter\n#             counter += 1\n# # tokenizer_class(\"Hello this is my tokenizer function\")","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.202946Z","iopub.execute_input":"2024-08-01T19:19:41.203268Z","iopub.status.idle":"2024-08-01T19:19:41.207544Z","shell.execute_reply.started":"2024-08-01T19:19:41.203242Z","shell.execute_reply":"2024-08-01T19:19:41.206632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from collections import Counter\nclass Tokenizer():\n    def __init__(self , top_n):\n        self.top_n = top_n\n        self.counter = 1\n        self.tokenizer = {'oov':0}\n        self.token_freq = Counter()\n    \n    def tokenized_text(self,text):\n        '''\n        Method to converts single text in vocabulary\n        '''\n        text = text.split()\n        for tx in text:\n            tx = tx.lower()\n            self.token_freq[tx] += 1\n        \n    def fit_on_texts(self):\n        '''\n        TO build vocabulary\n        '''\n        \n        common_token = [token for token , _ in self.token_freq.most_common(self.top_n)]\n        \n        self.tokenizer = {'oov':0}\n        self.counter = 1\n        for com in common_token:\n            self.tokenizer[com] = self.counter\n            self.counter += 1\n            \n    def texts_to_sequences(self, text):\n        '''\n        Method that convert text to tokens\n        '''\n        encoded_text = []\n        text = text.split()\n        for tx in text:\n            tx = tx.lower()\n            encoded_text.append(self.tokenizer.get(tx , 0))\n        \n        \n        return encoded_text\n    \n    def pad_sequences(self ,encoded_text, length):\n        '''\n        Method that handles truncation and padding to max length\n        '''\n        if(len(encoded_text) < length):\n            encoded_text = encoded_text + [0] * (length - len(encoded_text))\n        else:\n            encoded_text = encoded_text[:length]\n        return encoded_text\n            \n        \n        \n        ","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.208725Z","iopub.execute_input":"2024-08-01T19:19:41.208987Z","iopub.status.idle":"2024-08-01T19:19:41.221609Z","shell.execute_reply.started":"2024-08-01T19:19:41.208963Z","shell.execute_reply":"2024-08-01T19:19:41.220667Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tokenizefit_on_textsr = Tokenizer(top_n = 8)\n# texts = ['this is my text' , 'i wanted to be Athlete']\n# for text in texts:\n#     tokenizer.tokenized_text(text)\n# tokenizer.fit_on_texts()\n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.222756Z","iopub.execute_input":"2024-08-01T19:19:41.223070Z","iopub.status.idle":"2024-08-01T19:19:41.233969Z","shell.execute_reply.started":"2024-08-01T19:19:41.223046Z","shell.execute_reply":"2024-08-01T19:19:41.233321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# text = 'i want to be text in boy'\n# enc = tokenizer.texts_to_sequences(text)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.235285Z","iopub.execute_input":"2024-08-01T19:19:41.235851Z","iopub.status.idle":"2024-08-01T19:19:41.243625Z","shell.execute_reply.started":"2024-08-01T19:19:41.235817Z","shell.execute_reply":"2024-08-01T19:19:41.242758Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tokenizer.pad_sequences(enc , 20)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.244722Z","iopub.execute_input":"2024-08-01T19:19:41.246228Z","iopub.status.idle":"2024-08-01T19:19:41.253441Z","shell.execute_reply.started":"2024-08-01T19:19:41.246203Z","shell.execute_reply":"2024-08-01T19:19:41.252663Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer = Tokenizer(top_n = 50000)\nfor text in train_df['question_text']:\n    tokenizer.tokenized_text(text)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:41.256214Z","iopub.execute_input":"2024-08-01T19:19:41.256464Z","iopub.status.idle":"2024-08-01T19:19:51.135689Z","shell.execute_reply.started":"2024-08-01T19:19:41.256436Z","shell.execute_reply":"2024-08-01T19:19:51.134796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"tokenizer.fit_on_texts()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:51.136959Z","iopub.execute_input":"2024-08-01T19:19:51.137288Z","iopub.status.idle":"2024-08-01T19:19:51.369271Z","shell.execute_reply.started":"2024-08-01T19:19:51.137256Z","shell.execute_reply":"2024-08-01T19:19:51.368237Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# tokenizer.tokenizer()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:51.370475Z","iopub.execute_input":"2024-08-01T19:19:51.370790Z","iopub.status.idle":"2024-08-01T19:19:51.374661Z","shell.execute_reply.started":"2024-08-01T19:19:51.370762Z","shell.execute_reply":"2024-08-01T19:19:51.373783Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text = 'i want to be text in boy'\nenc = tokenizer.texts_to_sequences(text)\nenc= tokenizer.pad_sequences(enc , 100)\nprint(enc)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:51.375960Z","iopub.execute_input":"2024-08-01T19:19:51.376535Z","iopub.status.idle":"2024-08-01T19:19:51.385320Z","shell.execute_reply.started":"2024-08-01T19:19:51.376503Z","shell.execute_reply":"2024-08-01T19:19:51.384464Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_X =[]\nfor text in train_df['question_text']:\n    enc = tokenizer.texts_to_sequences(text)\n    enc= tokenizer.pad_sequences(enc , 100)\n    train_X.append(enc)\ntrain_X = np.array(train_X)\ntrain_X.shape\n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:19:51.386656Z","iopub.execute_input":"2024-08-01T19:19:51.386938Z","iopub.status.idle":"2024-08-01T19:20:20.457162Z","shell.execute_reply.started":"2024-08-01T19:19:51.386914Z","shell.execute_reply":"2024-08-01T19:20:20.456192Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_y = train_df['target'].values\nlen(train_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:20.458460Z","iopub.execute_input":"2024-08-01T19:20:20.458826Z","iopub.status.idle":"2024-08-01T19:20:20.466735Z","shell.execute_reply.started":"2024-08-01T19:20:20.458792Z","shell.execute_reply":"2024-08-01T19:20:20.465692Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print('er')","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:20.467919Z","iopub.execute_input":"2024-08-01T19:20:20.468529Z","iopub.status.idle":"2024-08-01T19:20:20.476287Z","shell.execute_reply.started":"2024-08-01T19:20:20.468496Z","shell.execute_reply":"2024-08-01T19:20:20.475234Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(train_X[0])","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:20.477470Z","iopub.execute_input":"2024-08-01T19:20:20.478204Z","iopub.status.idle":"2024-08-01T19:20:20.488423Z","shell.execute_reply.started":"2024-08-01T19:20:20.478170Z","shell.execute_reply":"2024-08-01T19:20:20.487577Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(len(train_X)):\n    if max(train_X[i]) >= 50000:\n        print(train_X[i])\n        break","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:20.489424Z","iopub.execute_input":"2024-08-01T19:20:20.489679Z","iopub.status.idle":"2024-08-01T19:20:27.197776Z","shell.execute_reply.started":"2024-08-01T19:20:20.489656Z","shell.execute_reply":"2024-08-01T19:20:27.196767Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(train_X) , type(train_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:27.199098Z","iopub.execute_input":"2024-08-01T19:20:27.199377Z","iopub.status.idle":"2024-08-01T19:20:27.206253Z","shell.execute_reply.started":"2024-08-01T19:20:27.199336Z","shell.execute_reply":"2024-08-01T19:20:27.205394Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"****Without Pretrained Embeddings****","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.functional as F\nclass Model(torch.nn.Module):\n    def __init__(self , embed_max_fe = 50000 , embed_size = 100):\n        super(Model , self).__init__()\n        self.embeddings = nn.Embedding(embed_max_fe+1 , embed_size)\n        self.gru = nn.GRU(embed_size ,64,bidirectional = True , batch_first = True) #batch_first is true so that output comes out in dimension (batch_first , seq_length , input_size)\n        self.pooling = nn.AdaptiveMaxPool1d(1)\n        self.liner1 = nn.Linear(64*2 ,16) # 64 * 2 for bidirectional GRU\n        self.dropout = nn.Dropout(0.1)\n        self.liner2 = nn.Linear(16 , 1)\n        self.sigmoid = nn.Sigmoid()\n        \n    def forward(self , sentence):\n        embed = self.embeddings(sentence)\n        gru_out , _ = self.gru(embed) #_ is the hidden state , which is not used\n        #We need to permute the output to fit the pooling layer input\n        gru_out = gru_out.permute(0,2,1)\n        pool = self.pooling(gru_out)\n        pool = pool.squeeze(2) #Remove the last dimesion which is 1\n        layer1 = self.liner1(pool)\n        act = nn.ReLU()(layer1)\n        drop = self.dropout(act)\n        layer2 = self.liner2(drop)\n        out = self.sigmoid(layer2)\n        return out\n    \nmodel = Model()\ncount_paramters_trainable = 0\ntotal_paramter = 0\nfor param in model.parameters():\n    if param.requires_grad:\n        count_paramters_trainable += param.numel()\n    total_paramter += param.numel()\nprint(f\"Total Trainable Parameters is {count_paramters_trainable}\")\nprint(f\"Total Parameters is {total_paramter}\")","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:27.211120Z","iopub.execute_input":"2024-08-01T19:20:27.211428Z","iopub.status.idle":"2024-08-01T19:20:27.266979Z","shell.execute_reply.started":"2024-08-01T19:20:27.211404Z","shell.execute_reply":"2024-08-01T19:20:27.266131Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\ndevice","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:27.267927Z","iopub.execute_input":"2024-08-01T19:20:27.268202Z","iopub.status.idle":"2024-08-01T19:20:27.333372Z","shell.execute_reply.started":"2024-08-01T19:20:27.268179Z","shell.execute_reply":"2024-08-01T19:20:27.332488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model = model.to(device)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:27.334917Z","iopub.execute_input":"2024-08-01T19:20:27.335221Z","iopub.status.idle":"2024-08-01T19:20:27.510173Z","shell.execute_reply.started":"2024-08-01T19:20:27.335197Z","shell.execute_reply":"2024-08-01T19:20:27.509379Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch.optim as optim\ncirterion = nn.BCELoss()\noptimizer = optim.Adam(model.parameters() , lr = 0.001)\n","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:27.511362Z","iopub.execute_input":"2024-08-01T19:20:27.511664Z","iopub.status.idle":"2024-08-01T19:20:28.283750Z","shell.execute_reply.started":"2024-08-01T19:20:27.511638Z","shell.execute_reply":"2024-08-01T19:20:28.282811Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nos.environ[\"CUDA_LAUNCH_BLOCKING\"] = \"1\"\n\nbatch_size = 512\nfor epoch in range(2):\n    running_loss = 0\n    train_loss = 0\n    print(\"Epoch stated\")\n    for batch in range(0 , len(train_X) , batch_size):\n        end_batch = min(batch+512 , len(train_X))\n        inputs = train_X[batch:end_batch]\n        labels = train_y[batch:end_batch]\n        inputs = torch.from_numpy(inputs)\n        labels = torch.from_numpy(labels)\n        labels = labels.type(torch.float32)\n        inputs = inputs.to(device)\n        labels = labels.to(device)\n        if inputs.max().item() > 50000:\n            print(f\"Inputs: {inputs}\")\n            print(f\"Max index: {inputs.max().item()}\")\n            print(\"Index out of range!\")\n            # You may want to raise an AssertionError or handle the error accordingly\n            raise AssertionError(\"Index out of range!\")\n        optimizer.zero_grad()\n        outputs = model(inputs)\n        labels = labels.reshape(-1,1)\n        loss = cirterion(outputs , labels)\n        if(outputs.shape != labels.shape):\n            print(labels.shape , outputs.shape)\n        try:\n            loss.backward()\n        except Exception as e:\n            print(f\"{e}\")\n            print(outputs.shape)\n            print(labels.shape)\n            print(inputs.shape)\n        optimizer.step()\n        running_loss += loss.item() * (end_batch - batch)\n        train_loss += loss.item() * (end_batch - batch)\n        if(batch%(batch_size*1000)==0):\n            print(f\"running loss of each batch {running_loss}\")\n            running_loss = 0\n    print(f\"After one epoch the output is {train_loss/len(train_X)}\")\nprint(\"Fininsed Training\")","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:20:28.284806Z","iopub.execute_input":"2024-08-01T19:20:28.285207Z","iopub.status.idle":"2024-08-01T19:21:57.932885Z","shell.execute_reply.started":"2024-08-01T19:20:28.285182Z","shell.execute_reply":"2024-08-01T19:21:57.931847Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"max(train_y), min(train_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:21:57.934111Z","iopub.execute_input":"2024-08-01T19:21:57.934383Z","iopub.status.idle":"2024-08-01T19:21:58.283504Z","shell.execute_reply.started":"2024-08-01T19:21:57.934359Z","shell.execute_reply":"2024-08-01T19:21:58.282537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\ntorch.cuda.empty_cache()","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:21:58.284761Z","iopub.execute_input":"2024-08-01T19:21:58.285104Z","iopub.status.idle":"2024-08-01T19:21:58.295207Z","shell.execute_reply.started":"2024-08-01T19:21:58.285072Z","shell.execute_reply":"2024-08-01T19:21:58.294433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df = pd.read_csv(\"/kaggle/input/quora-insincere-questions-classification/test.csv\")\ntest_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:21:58.296184Z","iopub.execute_input":"2024-08-01T19:21:58.296424Z","iopub.status.idle":"2024-08-01T19:21:59.042449Z","shell.execute_reply.started":"2024-08-01T19:21:58.296403Z","shell.execute_reply":"2024-08-01T19:21:59.041390Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_X =[]\nfor text in test_df['question_text']:\n    enc = tokenizer.texts_to_sequences(text)\n    enc= tokenizer.pad_sequences(enc , 100)\n    test_X.append(enc)\ntest_X = np.array(test_X)\ntest_X.shape\n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:21:59.043470Z","iopub.execute_input":"2024-08-01T19:21:59.043754Z","iopub.status.idle":"2024-08-01T19:22:06.504655Z","shell.execute_reply.started":"2024-08-01T19:21:59.043730Z","shell.execute_reply":"2024-08-01T19:22:06.503778Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y= []\nwith torch.no_grad():\n    for batch in range(0 , len(test_X) , 512):\n        inputs = test_X[batch:batch+512]\n        inputs = torch.from_numpy(inputs)\n        inputs = inputs.to(device)\n        outputs = model(inputs)\n        outputs = outputs.cpu().detach().numpy() # cpu takes it to cpu from cuda and detach removes it from computation graph of torch so that it doesnot take extra space\n        outputs = outputs.reshape(-1)\n        test_y.extend(outputs)\nprint(len(test_y))\n    \n    ","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:30:24.313750Z","iopub.execute_input":"2024-08-01T19:30:24.314375Z","iopub.status.idle":"2024-08-01T19:30:28.580098Z","shell.execute_reply.started":"2024-08-01T19:30:24.314339Z","shell.execute_reply":"2024-08-01T19:30:28.579121Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y[0]","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:30:50.003564Z","iopub.execute_input":"2024-08-01T19:30:50.004182Z","iopub.status.idle":"2024-08-01T19:30:50.009984Z","shell.execute_reply.started":"2024-08-01T19:30:50.004147Z","shell.execute_reply":"2024-08-01T19:30:50.008970Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"type(test_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:32:21.013985Z","iopub.execute_input":"2024-08-01T19:32:21.014936Z","iopub.status.idle":"2024-08-01T19:32:21.021417Z","shell.execute_reply.started":"2024-08-01T19:32:21.014889Z","shell.execute_reply":"2024-08-01T19:32:21.020307Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_y = np.array(test_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:32:35.879111Z","iopub.execute_input":"2024-08-01T19:32:35.879494Z","iopub.status.idle":"2024-08-01T19:32:35.908429Z","shell.execute_reply.started":"2024-08-01T19:32:35.879464Z","shell.execute_reply":"2024-08-01T19:32:35.907258Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"pred_test_y = (test_y>0.35).astype(int)\nlen(pred_test_y)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:32:38.373649Z","iopub.execute_input":"2024-08-01T19:32:38.374486Z","iopub.status.idle":"2024-08-01T19:32:38.381488Z","shell.execute_reply.started":"2024-08-01T19:32:38.374450Z","shell.execute_reply":"2024-08-01T19:32:38.380437Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_df = pd.DataFrame({\"qid\":test_df[\"qid\"].values})\nout_df['prediction'] = pred_test_y\nout_df.to_csv(\"submission.csv\", index=False)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:33:11.423656Z","iopub.execute_input":"2024-08-01T19:33:11.424031Z","iopub.status.idle":"2024-08-01T19:33:12.064501Z","shell.execute_reply.started":"2024-08-01T19:33:11.423988Z","shell.execute_reply":"2024-08-01T19:33:12.063500Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"out_df.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-08-01T19:33:19.188716Z","iopub.execute_input":"2024-08-01T19:33:19.189104Z","iopub.status.idle":"2024-08-01T19:33:19.199827Z","shell.execute_reply.started":"2024-08-01T19:33:19.189071Z","shell.execute_reply":"2024-08-01T19:33:19.198699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import torch\nimport torch.nn as nn\nimport torch.optim as optim\n\nclass SimpleModel(nn.Module):\n    def __init__(self):\n        super(SimpleModel, self).__init__()\n        self.fc = nn.Linear(10, 1)\n    \n    def forward(self, x):\n        return self.fc(x)\n\nmodel = SimpleModel().to('cuda')\ncriterion = nn.BCEWithLogitsLoss()\noptimizer = optim.SGD(model.parameters(), lr=0.01)\n\ninputs = torch.randn(10, 10).to('cuda')\nlabels = torch.randint(0, 2, (10, 1)).to('cuda')\n\noptimizer.zero_grad()\noutputs = model(inputs)\nloss = criterion(outputs, labels.float())\nloss.backward()\noptimizer.step()\n\nprint(\"Simple test passed successfully.\")\n","metadata":{"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}