{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.11.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":113366,"databundleVersionId":13605326,"sourceType":"competition"},{"sourceId":13114076,"sourceType":"datasetVersion","datasetId":8307241},{"sourceId":584926,"sourceType":"modelInstanceVersion","isSourceIdPinned":true,"modelInstanceId":437006,"modelId":453727}],"dockerImageVersionId":31089,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":true}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import pandas as pd\nimport torch\nfrom torch.utils.data import Dataset,DataLoader\nimport numpy as np\nfrom nltk.tokenize import word_tokenize\nimport nltk\nimport torch.nn as nn\nfrom tqdm import tqdm\nfrom torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence,pad_sequence\nimport pickle\nfrom sklearn.model_selection import StratifiedShuffleSplit\nfrom sklearn.utils.class_weight import compute_class_weight","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:33.551875Z","iopub.execute_input":"2025-09-20T08:27:33.552190Z","iopub.status.idle":"2025-09-20T08:27:36.624134Z","shell.execute_reply.started":"2025-09-20T08:27:33.552159Z","shell.execute_reply":"2025-09-20T08:27:36.623262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/comments-classification/Dataset/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:36.625514Z","iopub.execute_input":"2025-09-20T08:27:36.625909Z","iopub.status.idle":"2025-09-20T08:27:37.601314Z","shell.execute_reply.started":"2025-09-20T08:27:36.625880Z","shell.execute_reply":"2025-09-20T08:27:37.600692Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.shape","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.602133Z","iopub.execute_input":"2025-09-20T08:27:37.602464Z","iopub.status.idle":"2025-09-20T08:27:37.608354Z","shell.execute_reply.started":"2025-09-20T08:27:37.602433Z","shell.execute_reply":"2025-09-20T08:27:37.607636Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.isnull().sum()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.609151Z","iopub.execute_input":"2025-09-20T08:27:37.609462Z","iopub.status.idle":"2025-09-20T08:27:37.647159Z","shell.execute_reply.started":"2025-09-20T08:27:37.609440Z","shell.execute_reply":"2025-09-20T08:27:37.646127Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df['psychotic_depression'].value_counts()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.649467Z","iopub.execute_input":"2025-09-20T08:27:37.649750Z","iopub.status.idle":"2025-09-20T08:27:37.660687Z","shell.execute_reply.started":"2025-09-20T08:27:37.649732Z","shell.execute_reply":"2025-09-20T08:27:37.660090Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df.info()","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.661485Z","iopub.execute_input":"2025-09-20T08:27:37.661748Z","iopub.status.idle":"2025-09-20T08:27:37.702592Z","shell.execute_reply.started":"2025-09-20T08:27:37.661730Z","shell.execute_reply":"2025-09-20T08:27:37.701996Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class RawCommentsData(Dataset):\n    def __init__(self,path):\n        self.df = pd.read_csv(path)\n        \n\n    def __getitem__(self,idx):\n        comment = self.df.iloc[idx]['comment_text']\n        psychotic_depression = self.df.iloc[idx]['psychotic_depression']\n        return comment,psychotic_depression\n\n    def __len__(self):\n        return len(self.df)","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.703284Z","iopub.execute_input":"2025-09-20T08:27:37.703512Z","iopub.status.idle":"2025-09-20T08:27:37.708104Z","shell.execute_reply.started":"2025-09-20T08:27:37.703494Z","shell.execute_reply":"2025-09-20T08:27:37.707337Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class RawCommentsTest(Dataset):\n    def __init__(self,path):\n        self.df = pd.read_csv(path)\n        \n\n    def __getitem__(self,idx):\n        comment = self.df.iloc[idx]['comment_text']\n        return comment\n\n    def __len__(self):\n        return len(self.df)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.708920Z","iopub.execute_input":"2025-09-20T08:27:37.709094Z","iopub.status.idle":"2025-09-20T08:27:37.718735Z","shell.execute_reply.started":"2025-09-20T08:27:37.709080Z","shell.execute_reply":"2025-09-20T08:27:37.717955Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_train = RawCommentsData(\"/kaggle/input/comments-classification/Dataset/train.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:37.719517Z","iopub.execute_input":"2025-09-20T08:27:37.719765Z","iopub.status.idle":"2025-09-20T08:27:38.694701Z","shell.execute_reply.started":"2025-09-20T08:27:37.719742Z","shell.execute_reply":"2025-09-20T08:27:38.694097Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"raw_test = RawCommentsTest(\"/kaggle/input/comments-classification/Dataset/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:38.695416Z","iopub.execute_input":"2025-09-20T08:27:38.695704Z","iopub.status.idle":"2025-09-20T08:27:39.513177Z","shell.execute_reply.started":"2025-09-20T08:27:38.695682Z","shell.execute_reply":"2025-09-20T08:27:39.512358Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Create Embeddings**","metadata":{}},{"cell_type":"code","source":"embedding_dict = {}\n\nwith open(\"/kaggle/input/glooveembeddings/other/default/1/glove.6B.50d.txt\",'r',encoding=\"utf-8\") as f:\n    for line in f:\n        values = line.split()\n        word = values[0]\n        vector = np.asarray(values[1:],\"float32\")\n        embedding_dict[word] = vector","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:39.514021Z","iopub.execute_input":"2025-09-20T08:27:39.514256Z","iopub.status.idle":"2025-09-20T08:27:43.986067Z","shell.execute_reply.started":"2025-09-20T08:27:39.514231Z","shell.execute_reply":"2025-09-20T08:27:43.985471Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"embedding_dict[\"the\"]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:43.986793Z","iopub.execute_input":"2025-09-20T08:27:43.986976Z","iopub.status.idle":"2025-09-20T08:27:43.992446Z","shell.execute_reply.started":"2025-09-20T08:27:43.986960Z","shell.execute_reply":"2025-09-20T08:27:43.991679Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"nltk.download(\"punkt\")\ndef createCommentEmbedding(comment):\n    comment_list = word_tokenize(comment.lower())\n    embedding_list = []\n    for token in comment_list:\n        if token in embedding_dict:\n            embedding_list.append(torch.tensor(embedding_dict[token]))\n        else:\n            embedding_list.append(torch.zeros_like(torch.tensor(embedding_dict[\"the\"],dtype=torch.float)))\n\n    if not embedding_list:\n        embedding_list.append(torch.zeros_like(torch.tensor(embedding_dict[\"the\"], dtype=torch.float)))\n\n    return embedding_list","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:43.993191Z","iopub.execute_input":"2025-09-20T08:27:43.993436Z","iopub.status.idle":"2025-09-20T08:27:44.090689Z","shell.execute_reply.started":"2025-09-20T08:27:43.993421Z","shell.execute_reply":"2025-09-20T08:27:44.090057Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Creating the dataset**","metadata":{}},{"cell_type":"code","source":"def createTensor(dataset):\n    features = []\n    targets = []\n    for i in range(len(dataset)):\n        comment,label = dataset[i]\n        embedding_list = createCommentEmbedding(comment)\n        if embedding_list:\n            embedding_list = torch.stack(embedding_list)\n            features.append(torch.tensor(embedding_list))\n            targets.append(label)\n    return features,targets","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:44.093488Z","iopub.execute_input":"2025-09-20T08:27:44.093682Z","iopub.status.idle":"2025-09-20T08:27:44.097828Z","shell.execute_reply.started":"2025-09-20T08:27:44.093669Z","shell.execute_reply":"2025-09-20T08:27:44.097147Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def createTestTensor(dataset):\n    features = []\n    for i in range(len(dataset)):\n        comment = dataset[i]\n        embedding_list = createCommentEmbedding(comment)\n        if embedding_list:\n            embedding_list = torch.stack(embedding_list)\n            features.append(torch.tensor(embedding_list))\n        \n    return features","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:44.098383Z","iopub.execute_input":"2025-09-20T08:27:44.098692Z","iopub.status.idle":"2025-09-20T08:27:44.110371Z","shell.execute_reply.started":"2025-09-20T08:27:44.098664Z","shell.execute_reply":"2025-09-20T08:27:44.109797Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_train, y_train = createTensor(raw_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:27:44.111032Z","iopub.execute_input":"2025-09-20T08:27:44.111274Z","iopub.status.idle":"2025-09-20T08:30:57.997015Z","shell.execute_reply.started":"2025-09-20T08:27:44.111247Z","shell.execute_reply":"2025-09-20T08:30:57.996196Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_test = createTestTensor(raw_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:30:57.997827Z","iopub.execute_input":"2025-09-20T08:30:57.998049Z","iopub.status.idle":"2025-09-20T08:33:48.329154Z","shell.execute_reply.started":"2025-09-20T08:30:57.998033Z","shell.execute_reply":"2025-09-20T08:33:48.328274Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"import pickle\n\nwith open(\"X_train.pkl\", \"wb\") as f:\n    pickle.dump(X_train, f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:33:48.330257Z","iopub.execute_input":"2025-09-20T08:33:48.330559Z","iopub.status.idle":"2025-09-20T08:34:07.395833Z","shell.execute_reply.started":"2025-09-20T08:33:48.330533Z","shell.execute_reply":"2025-09-20T08:34:07.394980Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"with open(\"y_train.pkl\", \"wb\") as f:\n    pickle.dump(y_train, f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:07.396868Z","iopub.execute_input":"2025-09-20T08:34:07.397156Z","iopub.status.idle":"2025-09-20T08:34:07.706812Z","shell.execute_reply.started":"2025-09-20T08:34:07.397134Z","shell.execute_reply":"2025-09-20T08:34:07.706205Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"# with open(\"/kaggle/input/train-list/X_train.pkl\", \"rb\") as f:\n#     X_train = pickle.load(f)\n\n# with open(\"/kaggle/input/train-list/y_train.pkl\", \"rb\") as f:\n#     y_train = pickle.load(f)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:07.707482Z","iopub.execute_input":"2025-09-20T08:34:07.707700Z","iopub.status.idle":"2025-09-20T08:34:10.736692Z","shell.execute_reply.started":"2025-09-20T08:34:07.707662Z","shell.execute_reply":"2025-09-20T08:34:10.735716Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CommentsDataset(Dataset):\n    def __init__(self,X,y):\n        self.X = X\n        self.y = y\n        \n\n    def __getitem__(self,idx):\n        return self.X[idx], self.y[idx]\n\n    def __len__(self):\n        return len(self.X)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:10.737597Z","iopub.execute_input":"2025-09-20T08:34:10.737891Z","iopub.status.idle":"2025-09-20T08:34:12.169408Z","shell.execute_reply.started":"2025-09-20T08:34:10.737866Z","shell.execute_reply":"2025-09-20T08:34:12.168451Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class CommentsTestDataset(Dataset):\n    def __init__(self,X):\n        self.X = X\n        \n\n    def __getitem__(self,idx):\n        return self.X[idx]\n\n    def __len__(self):\n        return len(self.X)    \n    ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:12.170593Z","iopub.execute_input":"2025-09-20T08:34:12.171095Z","iopub.status.idle":"2025-09-20T08:34:13.332060Z","shell.execute_reply.started":"2025-09-20T08:34:12.171069Z","shell.execute_reply":"2025-09-20T08:34:13.331251Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"ssf = StratifiedShuffleSplit(n_splits=1,test_size=0.2)\nsplit = ssf.split(X_train,y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.332851Z","iopub.execute_input":"2025-09-20T08:34:13.333101Z","iopub.status.idle":"2025-09-20T08:34:13.357905Z","shell.execute_reply.started":"2025-09-20T08:34:13.333078Z","shell.execute_reply":"2025-09-20T08:34:13.357393Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_split, val_split = next(ssf.split(X_train, y_train))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.358764Z","iopub.execute_input":"2025-09-20T08:34:13.359015Z","iopub.status.idle":"2025-09-20T08:34:13.429934Z","shell.execute_reply.started":"2025-09-20T08:34:13.358992Z","shell.execute_reply":"2025-09-20T08:34:13.429359Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"X_tr = [X_train[ind] for ind in train_split]\ny_tr = [y_train[ind] for ind in train_split]\nX_vl = [X_train[ind] for ind in val_split]\ny_val = [y_train[ind] for ind in val_split]","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.430591Z","iopub.execute_input":"2025-09-20T08:34:13.430814Z","iopub.status.idle":"2025-09-20T08:34:13.497684Z","shell.execute_reply.started":"2025-09-20T08:34:13.430797Z","shell.execute_reply":"2025-09-20T08:34:13.497179Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_dataset = CommentsDataset(X_tr,y_tr)\nval_dataset = CommentsDataset(X_vl,y_val)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.498314Z","iopub.execute_input":"2025-09-20T08:34:13.498528Z","iopub.status.idle":"2025-09-20T08:34:13.501951Z","shell.execute_reply.started":"2025-09-20T08:34:13.498511Z","shell.execute_reply":"2025-09-20T08:34:13.501258Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_dataset = CommentsTestDataset(X_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.502816Z","iopub.execute_input":"2025-09-20T08:34:13.503044Z","iopub.status.idle":"2025-09-20T08:34:13.513424Z","shell.execute_reply.started":"2025-09-20T08:34:13.503030Z","shell.execute_reply":"2025-09-20T08:34:13.512619Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def collate_fn_test(batch):\n    return list(batch)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.514123Z","iopub.execute_input":"2025-09-20T08:34:13.514342Z","iopub.status.idle":"2025-09-20T08:34:13.525096Z","shell.execute_reply.started":"2025-09-20T08:34:13.514327Z","shell.execute_reply":"2025-09-20T08:34:13.524534Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def collate_fn(batch):\n    X, y = zip(*batch)\n    y = torch.tensor(y, dtype=torch.long)      \n    return list(X), y ","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.525755Z","iopub.execute_input":"2025-09-20T08:34:13.526045Z","iopub.status.idle":"2025-09-20T08:34:13.534835Z","shell.execute_reply.started":"2025-09-20T08:34:13.526017Z","shell.execute_reply":"2025-09-20T08:34:13.534262Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"train_loader = DataLoader(train_dataset,batch_size=32,shuffle=True,collate_fn=collate_fn)\nval_loader = DataLoader(val_dataset,batch_size=32,shuffle=True,collate_fn=collate_fn)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.535535Z","iopub.execute_input":"2025-09-20T08:34:13.535806Z","iopub.status.idle":"2025-09-20T08:34:13.548093Z","shell.execute_reply.started":"2025-09-20T08:34:13.535785Z","shell.execute_reply":"2025-09-20T08:34:13.547460Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Building the model**","metadata":{}},{"cell_type":"code","source":"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.548804Z","iopub.execute_input":"2025-09-20T08:34:13.548987Z","iopub.status.idle":"2025-09-20T08:34:13.617307Z","shell.execute_reply.started":"2025-09-20T08:34:13.548973Z","shell.execute_reply":"2025-09-20T08:34:13.616569Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"class LSTMmodel(nn.Module):\n    def __init__(self,embedding_dim,hidden_size,num_layers,output_size=1):\n        super().__init__()\n        self.lstm = nn.LSTM(embedding_dim,hidden_size=hidden_size,num_layers=num_layers,batch_first=True)\n        self.classifier = nn.Linear(hidden_size,output_size)\n\n    def forward(self,X):\n        X = [seq.to(device) for seq in X]\n        lengths = [len(seq) for seq in X]\n        X_padded = pad_sequence(X, batch_first=True)\n        packed = pack_padded_sequence(X_padded, lengths, batch_first=True, enforce_sorted=False)      \n        out,(h,c) = self.lstm(packed)\n        feat = h[-1] \n        out = self.classifier(feat)\n        return out","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.618096Z","iopub.execute_input":"2025-09-20T08:34:13.618319Z","iopub.status.idle":"2025-09-20T08:34:13.634580Z","shell.execute_reply.started":"2025-09-20T08:34:13.618302Z","shell.execute_reply":"2025-09-20T08:34:13.634053Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model = LSTMmodel(50,100,3,2).to(device)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.635233Z","iopub.execute_input":"2025-09-20T08:34:13.635438Z","iopub.status.idle":"2025-09-20T08:34:13.843286Z","shell.execute_reply.started":"2025-09-20T08:34:13.635422Z","shell.execute_reply":"2025-09-20T08:34:13.842421Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def train(epoch):\n    pbar = tqdm(train_loader,total=len(train_loader),desc=f\"Train Epoch {epoch}/{epochs}\")\n    epoch_loss = 0.0\n    for x,y in pbar:\n        y= y.to(device)\n        out = model(x)\n        total_loss = loss(out,y)\n        epoch_loss += total_loss\n        \n        optimizer.zero_grad()\n        total_loss.backward()\n        optimizer.step()\n\n    epoch_loss = epoch_loss/len(train_loader)\n    return epoch_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.844137Z","iopub.execute_input":"2025-09-20T08:34:13.844368Z","iopub.status.idle":"2025-09-20T08:34:13.849469Z","shell.execute_reply.started":"2025-09-20T08:34:13.844350Z","shell.execute_reply":"2025-09-20T08:34:13.848815Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"weights = compute_class_weight(class_weight=\"balanced\", classes=np.unique(y_train), y=y_train)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.850094Z","iopub.execute_input":"2025-09-20T08:34:13.850343Z","iopub.status.idle":"2025-09-20T08:34:13.910918Z","shell.execute_reply.started":"2025-09-20T08:34:13.850318Z","shell.execute_reply":"2025-09-20T08:34:13.910132Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"def val():\n    model.eval()\n    total_val_loss = 0.0\n    for x,y in val_loader:\n        y = y.to(device)\n        out = model(x)\n        val_loss = loss(out,y)\n        total_val_loss+=val_loss\n\n    total_val_loss = total_val_loss/len(val_loader)\n    return total_val_loss","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.911764Z","iopub.execute_input":"2025-09-20T08:34:13.911979Z","iopub.status.idle":"2025-09-20T08:34:13.916479Z","shell.execute_reply.started":"2025-09-20T08:34:13.911961Z","shell.execute_reply":"2025-09-20T08:34:13.915771Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"loss = nn.CrossEntropyLoss(weight=torch.tensor(weights,dtype=torch.float32).to(device))\noptimizer = torch.optim.Adam(model.parameters(),lr=0.001)\nepochs = 10\npatience = 5\ncount = 0\nbest_val_loss = 1e9\n\nfor epoch in range(epochs):\n    model.train()\n    train_loss = train(epoch)\n    validation_loss = val()\n    print(f\"train_loss : {train_loss:.4f} | val_loss : {validation_loss:.4f}\")\n\n    if validation_loss < best_val_loss:\n        count = 0\n        torch.save(model.state_dict(),\"model.pth\")\n\n    else:\n        count+=1\n\n    if count == patience:\n        break","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T08:34:13.917132Z","iopub.execute_input":"2025-09-20T08:34:13.917359Z","iopub.status.idle":"2025-09-20T09:04:33.381206Z","shell.execute_reply.started":"2025-09-20T08:34:13.917341Z","shell.execute_reply":"2025-09-20T09:04:33.380349Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"model.load_state_dict(torch.load(\"/kaggle/working/model.pth\"))","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:33.382302Z","iopub.execute_input":"2025-09-20T09:04:33.382805Z","iopub.status.idle":"2025-09-20T09:04:33.391376Z","shell.execute_reply.started":"2025-09-20T09:04:33.382777Z","shell.execute_reply":"2025-09-20T09:04:33.390691Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = []\ny_target = []\nmodel.eval()\nfor x,y in val_loader:\n    out = model(x)\n    out = out.detach().cpu()\n    y_pred.extend(torch.argmax(out,dim=1))\n    y_target.extend(y)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:33.392035Z","iopub.execute_input":"2025-09-20T09:04:33.392260Z","iopub.status.idle":"2025-09-20T09:04:49.622122Z","shell.execute_reply.started":"2025-09-20T09:04:33.392238Z","shell.execute_reply":"2025-09-20T09:04:49.621486Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"from sklearn.metrics import f1_score\nf1_score(y_target,y_pred)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:49.622900Z","iopub.execute_input":"2025-09-20T09:04:49.623100Z","iopub.status.idle":"2025-09-20T09:04:51.886493Z","shell.execute_reply.started":"2025-09-20T09:04:49.623084Z","shell.execute_reply":"2025-09-20T09:04:51.885842Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"df = pd.read_csv(\"/kaggle/input/comments-classification/Dataset/test.csv\")","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:51.890086Z","iopub.execute_input":"2025-09-20T09:04:51.890290Z","iopub.status.idle":"2025-09-20T09:04:52.598373Z","shell.execute_reply.started":"2025-09-20T09:04:51.890275Z","shell.execute_reply":"2025-09-20T09:04:52.597727Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"test_loader = DataLoader(test_dataset,batch_size=32,shuffle=True,collate_fn=collate_fn_test)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:52.600660Z","iopub.execute_input":"2025-09-20T09:04:52.600874Z","iopub.status.idle":"2025-09-20T09:04:52.604836Z","shell.execute_reply.started":"2025-09-20T09:04:52.600857Z","shell.execute_reply":"2025-09-20T09:04:52.604023Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"y_pred = []\nmodel.eval()\nfor x in test_loader:\n    out = model(x)\n    out = out.detach().cpu()\n    y_pred.extend(torch.argmax(out, dim=1).tolist())","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:04:52.605528Z","iopub.execute_input":"2025-09-20T09:04:52.605710Z","iopub.status.idle":"2025-09-20T09:06:10.935937Z","shell.execute_reply.started":"2025-09-20T09:04:52.605694Z","shell.execute_reply":"2025-09-20T09:06:10.935357Z"}},"outputs":[],"execution_count":null},{"cell_type":"code","source":"result = pd.DataFrame({\n    'ID': range(1,len(y_pred)+1),\n    'psychotic_depression': y_pred\n})\nresult.to_csv('result.csv',index=False)","metadata":{"trusted":true,"execution":{"iopub.status.busy":"2025-09-20T09:06:10.936582Z","iopub.execute_input":"2025-09-20T09:06:10.936790Z","iopub.status.idle":"2025-09-20T09:06:11.069893Z","shell.execute_reply.started":"2025-09-20T09:06:10.936773Z","shell.execute_reply":"2025-09-20T09:06:11.069284Z"}},"outputs":[],"execution_count":null},{"cell_type":"markdown","source":"**Addressing Overfitting**","metadata":{}}]}