{"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":"import warnings\nwarnings.filterwarnings('ignore')\n\nimport torch\ntorch.cuda.get_device_name()\n\nimport pandas as pd\nimport matplotlib.pyplot as plt\nimport seaborn as sns","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:08.436586Z","iopub.execute_input":"2023-06-12T07:30:08.436996Z","iopub.status.idle":"2023-06-12T07:30:13.611380Z","shell.execute_reply.started":"2023-06-12T07:30:08.436969Z","shell.execute_reply":"2023-06-12T07:30:13.610301Z"},"trusted":true},"execution_count":null,"outputs":[]},{"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')\ntrain_df.head()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:13.617093Z","iopub.execute_input":"2023-06-12T07:30:13.619773Z","iopub.status.idle":"2023-06-12T07:30:18.580477Z","shell.execute_reply.started":"2023-06-12T07:30:13.619736Z","shell.execute_reply":"2023-06-12T07:30:18.579532Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df.columns","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:18.581869Z","iopub.execute_input":"2023-06-12T07:30:18.582203Z","iopub.status.idle":"2023-06-12T07:30:18.592597Z","shell.execute_reply.started":"2023-06-12T07:30:18.582172Z","shell.execute_reply":"2023-06-12T07:30:18.591615Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_df['target'].value_counts()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:18.595490Z","iopub.execute_input":"2023-06-12T07:30:18.596097Z","iopub.status.idle":"2023-06-12T07:30:18.618484Z","shell.execute_reply.started":"2023-06-12T07:30:18.596059Z","shell.execute_reply":"2023-06-12T07:30:18.617603Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_df.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:18.619959Z","iopub.execute_input":"2023-06-12T07:30:18.620424Z","iopub.status.idle":"2023-06-12T07:30:18.627061Z","shell.execute_reply.started":"2023-06-12T07:30:18.620391Z","shell.execute_reply":"2023-06-12T07:30:18.626060Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = sns.countplot(data=train_df, x='target')\nfor i in ax.containers:\n    ax.bar_label(i)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:18.628680Z","iopub.execute_input":"2023-06-12T07:30:18.629370Z","iopub.status.idle":"2023-06-12T07:30:18.965385Z","shell.execute_reply.started":"2023-06-12T07:30:18.629339Z","shell.execute_reply":"2023-06-12T07:30:18.964468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# creating a small subset of the dataset to work with -> This will work as the training and validation.\n\nsample_df = train_df.sample(200_000)\nsample_df","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:18.966784Z","iopub.execute_input":"2023-06-12T07:30:18.967214Z","iopub.status.idle":"2023-06-12T07:30:19.060408Z","shell.execute_reply.started":"2023-06-12T07:30:18.967181Z","shell.execute_reply":"2023-06-12T07:30:19.059300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sample_df['target'].value_counts(normalize=True)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.062037Z","iopub.execute_input":"2023-06-12T07:30:19.062497Z","iopub.status.idle":"2023-06-12T07:30:19.073604Z","shell.execute_reply.started":"2023-06-12T07:30:19.062462Z","shell.execute_reply":"2023-06-12T07:30:19.072443Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Data preparation","metadata":{}},{"cell_type":"code","source":"import nltk\nnltk.download('punkt')\nfrom nltk.tokenize import word_tokenize\nfrom nltk.stem import SnowballStemmer\n\nfrom nltk.corpus import stopwords\nnltk.download('stopwords')\n\nfrom sklearn.feature_extraction.text import TfidfVectorizer","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.075374Z","iopub.execute_input":"2023-06-12T07:30:19.075867Z","iopub.status.idle":"2023-06-12T07:30:19.927418Z","shell.execute_reply.started":"2023-06-12T07:30:19.075833Z","shell.execute_reply":"2023-06-12T07:30:19.926400Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Stemmer takes words and return the original form\nstemmer = SnowballStemmer('english')","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.931604Z","iopub.execute_input":"2023-06-12T07:30:19.932253Z","iopub.status.idle":"2023-06-12T07:30:19.937116Z","shell.execute_reply.started":"2023-06-12T07:30:19.932218Z","shell.execute_reply":"2023-06-12T07:30:19.936063Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# stopwords\nenglish_stopwords = stopwords.words('english')\n\", \".join(english_stopwords)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.938745Z","iopub.execute_input":"2023-06-12T07:30:19.939077Z","iopub.status.idle":"2023-06-12T07:30:19.950137Z","shell.execute_reply.started":"2023-06-12T07:30:19.939047Z","shell.execute_reply":"2023-06-12T07:30:19.949144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def stem_tokenizer(sentance):\n    \n    # final_tokens = []\n    # tokens = word_tokenize(sentance)\n    # for pos,word in enumerate(tokens):\n    #     final_tokens.append(stemmer.stem(word))\n    # return final_tokens\n        \n    return [stemmer.stem(token) for token in word_tokenize(sentance)]\n        \n   \n\nstem_tokenizer(\"Looks like it worked fine\")","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.951671Z","iopub.execute_input":"2023-06-12T07:30:19.952134Z","iopub.status.idle":"2023-06-12T07:30:19.972053Z","shell.execute_reply.started":"2023-06-12T07:30:19.952094Z","shell.execute_reply":"2023-06-12T07:30:19.971279Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer = TfidfVectorizer(\n    tokenizer = stem_tokenizer,\n    stop_words = english_stopwords,\n    max_features = 1500\n)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.973369Z","iopub.execute_input":"2023-06-12T07:30:19.973907Z","iopub.status.idle":"2023-06-12T07:30:19.978509Z","shell.execute_reply.started":"2023-06-12T07:30:19.973877Z","shell.execute_reply":"2023-06-12T07:30:19.977321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.fit(sample_df['question_text'])      # to create the vocabulary","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:30:19.980175Z","iopub.execute_input":"2023-06-12T07:30:19.980571Z","iopub.status.idle":"2023-06-12T07:31:40.231238Z","shell.execute_reply.started":"2023-06-12T07:30:19.980522Z","shell.execute_reply":"2023-06-12T07:31:40.230097Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"vectorizer.get_feature_names_out()[:100]    # viewing 100 items from the vocabulary list.","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:31:40.233056Z","iopub.execute_input":"2023-06-12T07:31:40.233461Z","iopub.status.idle":"2023-06-12T07:31:40.244591Z","shell.execute_reply.started":"2023-06-12T07:31:40.233426Z","shell.execute_reply":"2023-06-12T07:31:40.243622Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Input Set","metadata":{}},{"cell_type":"code","source":"# Transforming the questions into vectors\ninputs = vectorizer.transform(sample_df['question_text'])\ninputs.shape","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:31:40.246444Z","iopub.execute_input":"2023-06-12T07:31:40.247167Z","iopub.status.idle":"2023-06-12T07:33:03.232198Z","shell.execute_reply.started":"2023-06-12T07:31:40.247134Z","shell.execute_reply":"2023-06-12T07:33:03.231249Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"targets = sample_df['target']\ntargets","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:33:03.233717Z","iopub.execute_input":"2023-06-12T07:33:03.234075Z","iopub.status.idle":"2023-06-12T07:33:03.242359Z","shell.execute_reply.started":"2023-06-12T07:33:03.234042Z","shell.execute_reply":"2023-06-12T07:33:03.241213Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_inputs = vectorizer.transform(test_df['question_text'])","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:33:03.243896Z","iopub.execute_input":"2023-06-12T07:33:03.244228Z","iopub.status.idle":"2023-06-12T07:35:44.860124Z","shell.execute_reply.started":"2023-06-12T07:33:03.244197Z","shell.execute_reply":"2023-06-12T07:35:44.858972Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.model_selection import train_test_split\n\nX_train,y_train, X_test, y_test = train_test_split(inputs, targets, train_size=.75, random_state=23)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:35:44.861715Z","iopub.execute_input":"2023-06-12T07:35:44.862098Z","iopub.status.idle":"2023-06-12T07:35:44.896287Z","shell.execute_reply.started":"2023-06-12T07:35:44.862061Z","shell.execute_reply":"2023-06-12T07:35:44.895423Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model training in Pytorch","metadata":{}},{"cell_type":"code","source":"import torch\nfrom torch.utils.data import TensorDataset, DataLoader\n\ndevice = 'cuda' if torch.cuda.is_available() else 'cpu'\ndevice","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:35:44.897813Z","iopub.execute_input":"2023-06-12T07:35:44.898171Z","iopub.status.idle":"2023-06-12T07:35:44.905997Z","shell.execute_reply.started":"2023-06-12T07:35:44.898138Z","shell.execute_reply":"2023-06-12T07:35:44.904957Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"- `Torch` requires tensors as input. \n- `X_train` and `y_train` are `scipy.sparse._csr.csr_matrix`, So, these are converted into `np.arrays` and then into `tensors`.\n- `X_test` and `y_test` are `pandas.core.series.Series` . So the values are taken and converted into tensors.","metadata":{}},{"cell_type":"code","source":"train_input_tensor = torch.tensor(X_train.toarray()).float().to(device)\nvalidation_input_tensor = torch.tensor(y_train.toarray()).float().to(device)\n\ntrain_target_tensor = torch.tensor(X_test.values).float().to(device)\nvalidation_target_tensor = torch.tensor(y_test.values).float().to(device)\n\ntest_tensor = torch.tensor(test_inputs.toarray()).float().to(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:35:44.907672Z","iopub.execute_input":"2023-06-12T07:35:44.908277Z","iopub.status.idle":"2023-06-12T07:36:03.980294Z","shell.execute_reply.started":"2023-06-12T07:35:44.908246Z","shell.execute_reply":"2023-06-12T07:36:03.979170Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"print(train_target_tensor, test_tensor)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:03.985484Z","iopub.execute_input":"2023-06-12T07:36:03.987929Z","iopub.status.idle":"2023-06-12T07:36:04.068254Z","shell.execute_reply.started":"2023-06-12T07:36:03.987893Z","shell.execute_reply":"2023-06-12T07:36:04.067618Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"### Creating `tensordataset` from the input and target tensors.","metadata":{}},{"cell_type":"code","source":"train_tensor_dataset = TensorDataset(train_input_tensor, train_target_tensor)\nvalidation_tensor_dataset = TensorDataset(validation_input_tensor, validation_target_tensor)\ntest_tensor_dataset = TensorDataset(test_tensor)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.069315Z","iopub.execute_input":"2023-06-12T07:36:04.069856Z","iopub.status.idle":"2023-06-12T07:36:04.074625Z","shell.execute_reply.started":"2023-06-12T07:36:04.069824Z","shell.execute_reply":"2023-06-12T07:36:04.073777Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_tensor_dataset[:5]","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.075789Z","iopub.execute_input":"2023-06-12T07:36:04.076682Z","iopub.status.idle":"2023-06-12T07:36:04.088207Z","shell.execute_reply.started":"2023-06-12T07:36:04.076646Z","shell.execute_reply":"2023-06-12T07:36:04.087274Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"batch_size = 128\nlearning_rate = 0.001","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.089754Z","iopub.execute_input":"2023-06-12T07:36:04.090385Z","iopub.status.idle":"2023-06-12T07:36:04.094850Z","shell.execute_reply.started":"2023-06-12T07:36:04.090349Z","shell.execute_reply":"2023-06-12T07:36:04.093888Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train_loader = DataLoader(train_tensor_dataset, batch_size=batch_size)\nval_loader = DataLoader(validation_tensor_dataset, batch_size=batch_size)\ntest_loader = DataLoader(test_tensor_dataset, batch_size=batch_size)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.096162Z","iopub.execute_input":"2023-06-12T07:36:04.097123Z","iopub.status.idle":"2023-06-12T07:36:04.104513Z","shell.execute_reply.started":"2023-06-12T07:36:04.097089Z","shell.execute_reply":"2023-06-12T07:36:04.103534Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i,j in train_loader:\n    print(i.shape)  # batch input shape => single file=> (1,1000)\n    print(j.shape)  # batch target shape\n    break","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.106231Z","iopub.execute_input":"2023-06-12T07:36:04.106881Z","iopub.status.idle":"2023-06-12T07:36:04.123166Z","shell.execute_reply.started":"2023-06-12T07:36:04.106851Z","shell.execute_reply":"2023-06-12T07:36:04.122300Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Neural Network","metadata":{}},{"cell_type":"code","source":"import torch.nn as nn\nimport torch.nn.functional as F","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.130745Z","iopub.execute_input":"2023-06-12T07:36:04.131342Z","iopub.status.idle":"2023-06-12T07:36:04.136201Z","shell.execute_reply.started":"2023-06-12T07:36:04.131310Z","shell.execute_reply":"2023-06-12T07:36:04.135296Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# deep neural model\n\nclass QuoraNet(nn.Module):\n    def __init__(self):\n        super().__init__()\n        self.layer1 = nn.Linear(1500, 512)\n        self.layer2 = nn.Linear(512, 256)\n        self.layer3 = nn.Linear(256, 64)\n        self.layer4 = nn.Linear(64, 1)\n        \n    def forward(self, inputs):\n        out = self.layer1(inputs)\n        out = F.relu(out)       # introducing non linearity\n        \n        out = self.layer2(out)\n        out = F.relu(out)\n        \n        out = self.layer3(out)\n        out = F.relu(out)\n        \n        out = self.layer4(out)\n\n        \n        return out\n\n\nmodel = QuoraNet()\nmodel.to(device)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:47.187117Z","iopub.execute_input":"2023-06-12T07:36:47.187469Z","iopub.status.idle":"2023-06-12T07:36:47.209396Z","shell.execute_reply.started":"2023-06-12T07:36:47.187439Z","shell.execute_reply":"2023-06-12T07:36:47.208521Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from torchinfo import summary\n\nsummary(model=QuoraNet())","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:53.971392Z","iopub.execute_input":"2023-06-12T07:36:53.972384Z","iopub.status.idle":"2023-06-12T07:36:53.987586Z","shell.execute_reply.started":"2023-06-12T07:36:53.972339Z","shell.execute_reply":"2023-06-12T07:36:53.986623Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"from sklearn.metrics import accuracy_score, f1_score, classification_report","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:36:04.196892Z","iopub.execute_input":"2023-06-12T07:36:04.197494Z","iopub.status.idle":"2023-06-12T07:36:04.201785Z","shell.execute_reply.started":"2023-06-12T07:36:04.197463Z","shell.execute_reply":"2023-06-12T07:36:04.200836Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for batch in train_loader:\n    batch_in, batch_targets = batch\n    \n    probablities = torch.relu(model(batch_in))\n    \n    pred = (probablities>.5).int()\n    \n    print(probablities[:10])\n          \n    print(pred[:10])\n    batch_targets = batch_targets.cpu()\n    pred = pred.cpu()\n\n    print(classification_report(batch_targets, pred))\n    break","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:01.839035Z","iopub.execute_input":"2023-06-12T07:37:01.839389Z","iopub.status.idle":"2023-06-12T07:37:03.508026Z","shell.execute_reply.started":"2023-06-12T07:37:01.839360Z","shell.execute_reply":"2023-06-12T07:37:03.507065Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Training and Evaluation","metadata":{}},{"cell_type":"code","source":"# Evaluating the model\ndef evaluation(model, dataLoader):\n    \n    f1_s, accs, losses = [], [], []\n    \n    for batch in dataLoader:\n        batch_in, batch_targets = batch\n        batch_out = model(batch_in)\n        \n        probablities = torch.sigmoid(batch_out[:,0])        # batch_out is represented like this [0]. To remove the brackets, [:,0] is used.\n        predicted = (probablities>0.5).int()\n        \n        batch_targets = batch_targets.cpu()\n        predicted = predicted.cpu()\n        probablities = probablities.cpu()\n        \n        f1 = f1_score(batch_targets, predicted)\n        loss = F.binary_cross_entropy(probablities, batch_targets)\n        acc = accuracy_score(batch_targets, predicted)\n        \n        f1_s.append(f1)\n        losses.append(loss)\n        accs.append(acc)\n        \n    return torch.mean(torch.tensor(f1_s)).item(), torch.mean(torch.tensor(accs)).item(), torch.mean(torch.tensor(losses)).item()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:24.376479Z","iopub.execute_input":"2023-06-12T07:37:24.376853Z","iopub.status.idle":"2023-06-12T07:37:24.385616Z","shell.execute_reply.started":"2023-06-12T07:37:24.376823Z","shell.execute_reply":"2023-06-12T07:37:24.384611Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"evaluation(model, train_loader)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:28.621260Z","iopub.execute_input":"2023-06-12T07:37:28.621628Z","iopub.status.idle":"2023-06-12T07:37:33.562026Z","shell.execute_reply.started":"2023-06-12T07:37:28.621597Z","shell.execute_reply":"2023-06-12T07:37:33.560974Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Optimizer\nADAM_optimizer = torch.optim.Adam(model.parameters(), lr = learning_rate)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:38.153013Z","iopub.execute_input":"2023-06-12T07:37:38.153396Z","iopub.status.idle":"2023-06-12T07:37:38.159011Z","shell.execute_reply.started":"2023-06-12T07:37:38.153365Z","shell.execute_reply":"2023-06-12T07:37:38.158062Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"\n# training / fitting the model\nhistory = []\n\ndef model_fit(epochs, model, train_dataLoader,validation_dataLoader, optimizer):\n\n    for epoch in range(epochs):\n        for batch in train_dataLoader:\n            batch_in, batch_targets = batch\n            \n            # calculate predicted outputs\n            batch_out = model(batch_in)\n            probablities = torch.sigmoid(batch_out[:,0])        # batch_out is represented like this [0]. To remove the brackets, [:,0] is used.\n            predicted = (probablities>0.5).int()\n            \n            # calculate loss\n            loss = F.binary_cross_entropy(probablities, batch_targets)\n            \n            # back propagration\n            loss.backward()\n            optimizer.step()\n            optimizer.zero_grad()\n            \n        \n        # evaluation part \n        f1, acc, loss = evaluation(model, validation_dataLoader)\n        print(f\"Epoch {epoch+1} | Loss {loss:.4f} | Accuracy {acc:.4f} | F-1 Score {f1:.4f}\")\n        history.append([acc, f1, loss])\n    \n    return history","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:47.423497Z","iopub.execute_input":"2023-06-12T07:37:47.424271Z","iopub.status.idle":"2023-06-12T07:37:47.432343Z","shell.execute_reply.started":"2023-06-12T07:37:47.424235Z","shell.execute_reply":"2023-06-12T07:37:47.431457Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"model_fit(20, model, train_loader, val_loader, ADAM_optimizer)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:37:52.996359Z","iopub.execute_input":"2023-06-12T07:37:52.996731Z","iopub.status.idle":"2023-06-12T07:39:28.062252Z","shell.execute_reply.started":"2023-06-12T07:37:52.996700Z","shell.execute_reply":"2023-06-12T07:39:28.061218Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"losses = [item[2] for item in history]\naccs = [item[0] for item in history]\nf1s = [item[1] for item in history]","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:31.082592Z","iopub.execute_input":"2023-06-12T07:39:31.083187Z","iopub.status.idle":"2023-06-12T07:39:31.088790Z","shell.execute_reply.started":"2023-06-12T07:39:31.083152Z","shell.execute_reply":"2023-06-12T07:39:31.087786Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Loss Curve')\nplt.plot(losses)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:35.872169Z","iopub.execute_input":"2023-06-12T07:39:35.872522Z","iopub.status.idle":"2023-06-12T07:39:36.161424Z","shell.execute_reply.started":"2023-06-12T07:39:35.872492Z","shell.execute_reply":"2023-06-12T07:39:36.160469Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('Accuracy Curve')\nplt.plot(accs)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:38.553179Z","iopub.execute_input":"2023-06-12T07:39:38.553532Z","iopub.status.idle":"2023-06-12T07:39:38.854310Z","shell.execute_reply.started":"2023-06-12T07:39:38.553503Z","shell.execute_reply":"2023-06-12T07:39:38.853445Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.title('F1 Curve')\nplt.plot(f1s)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:40.566845Z","iopub.execute_input":"2023-06-12T07:39:40.567191Z","iopub.status.idle":"2023-06-12T07:39:40.852958Z","shell.execute_reply.started":"2023-06-12T07:39:40.567163Z","shell.execute_reply":"2023-06-12T07:39:40.851102Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Model Testing","metadata":{}},{"cell_type":"code","source":"TEST = test_df\nTEST","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:45.907910Z","iopub.execute_input":"2023-06-12T07:39:45.908268Z","iopub.status.idle":"2023-06-12T07:39:45.920407Z","shell.execute_reply.started":"2023-06-12T07:39:45.908238Z","shell.execute_reply":"2023-06-12T07:39:45.919323Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def predictionOutput(df):\n    inputs = vectorizer.transform(df['question_text'])\n    input_tensors = torch.tensor(inputs.toarray()).float().to(device)\n    output = model(input_tensors)\n    probablities = torch.sigmoid(output)[:,0]\n    prediction = (probablities>0.5).int()\n    return prediction","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:48.103424Z","iopub.execute_input":"2023-06-12T07:39:48.104496Z","iopub.status.idle":"2023-06-12T07:39:48.110803Z","shell.execute_reply.started":"2023-06-12T07:39:48.104454Z","shell.execute_reply":"2023-06-12T07:39:48.109606Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"test_predicted = predictionOutput(TEST)\ntest_predicted = test_predicted.cpu().numpy()","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:39:50.305147Z","iopub.execute_input":"2023-06-12T07:39:50.305502Z","iopub.status.idle":"2023-06-12T07:42:32.630180Z","shell.execute_reply.started":"2023-06-12T07:39:50.305473Z","shell.execute_reply":"2023-06-12T07:42:32.629201Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"sub_df","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:43:37.170607Z","iopub.execute_input":"2023-06-12T07:43:37.170968Z","iopub.status.idle":"2023-06-12T07:43:37.183337Z","shell.execute_reply.started":"2023-06-12T07:43:37.170940Z","shell.execute_reply":"2023-06-12T07:43:37.182381Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# submission to kaggle\n\nsub_df.prediction = test_predicted\nsub_df.to_csv('submission.csv',index=None)","metadata":{"execution":{"iopub.status.busy":"2023-06-12T07:43:42.113570Z","iopub.execute_input":"2023-06-12T07:43:42.114241Z","iopub.status.idle":"2023-06-12T07:43:43.011511Z","shell.execute_reply.started":"2023-06-12T07:43:42.114208Z","shell.execute_reply":"2023-06-12T07:43:43.010598Z"},"scrolled":true,"trusted":true},"execution_count":null,"outputs":[]}]}