File size: 9,813 Bytes
662679f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 | {
"cells": [
{
"cell_type": "code",
"execution_count": 5,
"id": "917ac7f2-249d-47d9-a26f-ff8dd9fd786d",
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"✅ Fine-Tuned Custom DeBERTa Model Successfully Loaded from pytorch_model.bin!\n"
]
}
],
"source": [
"from transformers import AutoTokenizer, AutoModel\n",
"import torch\n",
"import torch.nn as nn\n",
"from torch.optim import AdamW\n",
"\n",
"MODEL_DIR = r\"C:\\Users\\amil\\OneDrive\\Documents\\AI-Driven Personalized Therapy Recommendations system\\Module_3\\Predictive_model\\model\\converted_model\"\n",
"MODEL_BIN_PATH = f\"{MODEL_DIR}/pytorch_model.bin\"\n",
"\n",
"\n",
"tokenizer = AutoTokenizer.from_pretrained(MODEL_DIR)\n",
"\n",
"MODEL_CHECKPOINT = \"microsoft/deberta-v3-base\"\n",
"NUM_LABELS = 19\n",
"\n",
"deberta_model = AutoModel.from_pretrained(MODEL_CHECKPOINT)\n",
"\n",
"# Define your custom classifier\n",
"class CustomDebertaClassifier(nn.Module):\n",
" def __init__(self, deberta_model, num_labels):\n",
" super(CustomDebertaClassifier, self).__init__()\n",
" self.deberta = deberta_model \n",
" self.dropout = nn.Dropout(0.3)\n",
" self.classifier = nn.Linear(768, num_labels) # 19 output classes\n",
" self.criterion = nn.CrossEntropyLoss()\n",
"\n",
" def forward(self, input_ids, attention_mask, labels=None):\n",
" outputs = self.deberta(input_ids=input_ids, attention_mask=attention_mask)\n",
" pooled_output = outputs.last_hidden_state[:, 0, :] # CLS token representation\n",
" pooled_output = self.dropout(pooled_output)\n",
" logits = self.classifier(pooled_output)\n",
" loss = None\n",
" if labels is not None:\n",
" loss = self.criterion(logits, labels) \n",
" return {\"loss\": loss, \"logits\": logits}\n",
"\n",
"model = CustomDebertaClassifier(deberta_model, NUM_LABELS)\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"model.to(device)\n",
"\n",
"model.load_state_dict(torch.load(MODEL_BIN_PATH, map_location=device))\n",
"\n",
"optimizer = AdamW(model.parameters(), lr=2e-5, weight_decay=0.01)\n",
"\n",
"model.eval()\n",
"print(\"✅ Fine-Tuned Custom DeBERTa Model Successfully Loaded from pytorch_model.bin!\")\n"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "237fc796-ae65-4d1f-85d4-36d71bd1ab0c",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"CustomDebertaClassifier(\n",
" (deberta): DebertaV2Model(\n",
" (embeddings): DebertaV2Embeddings(\n",
" (word_embeddings): Embedding(128100, 768, padding_idx=0)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-07, elementwise_affine=True)\n",
" (dropout): StableDropout()\n",
" )\n",
" (encoder): DebertaV2Encoder(\n",
" (layer): ModuleList(\n",
" (0-11): 12 x DebertaV2Layer(\n",
" (attention): DebertaV2Attention(\n",
" (self): DisentangledSelfAttention(\n",
" (query_proj): Linear(in_features=768, out_features=768, bias=True)\n",
" (key_proj): Linear(in_features=768, out_features=768, bias=True)\n",
" (value_proj): Linear(in_features=768, out_features=768, bias=True)\n",
" (pos_dropout): StableDropout()\n",
" (dropout): StableDropout()\n",
" )\n",
" (output): DebertaV2SelfOutput(\n",
" (dense): Linear(in_features=768, out_features=768, bias=True)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-07, elementwise_affine=True)\n",
" (dropout): StableDropout()\n",
" )\n",
" )\n",
" (intermediate): DebertaV2Intermediate(\n",
" (dense): Linear(in_features=768, out_features=3072, bias=True)\n",
" (intermediate_act_fn): GELUActivation()\n",
" )\n",
" (output): DebertaV2Output(\n",
" (dense): Linear(in_features=3072, out_features=768, bias=True)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-07, elementwise_affine=True)\n",
" (dropout): StableDropout()\n",
" )\n",
" )\n",
" )\n",
" (rel_embeddings): Embedding(512, 768)\n",
" (LayerNorm): LayerNorm((768,), eps=1e-07, elementwise_affine=True)\n",
" )\n",
" )\n",
" (dropout): Dropout(p=0.3, inplace=False)\n",
" (classifier): Linear(in_features=768, out_features=19, bias=True)\n",
" (criterion): CrossEntropyLoss()\n",
")"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"model.eval()"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "afb427f3-b7cd-4bd0-b9f7-5b2fec7a5134",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Asking to truncate to max_length but no maximum length is provided and the model has no predefined maximum length. Default to no truncation.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Raw logits: tensor([[-1.8831e+00, 1.9448e-01, 7.4612e-03, 1.2491e+00, 3.3083e-01,\n",
" -8.5939e-01, -1.0972e+00, -1.2843e+00, 3.3758e+00, 1.3420e-01,\n",
" 3.3786e-01, -8.1514e-01, -1.1681e+00, 3.6260e-01, -9.8768e-01,\n",
" 9.8034e+00, -2.6125e+00, -1.2301e+00, -9.3918e-01]], device='cuda:0')\n",
"Text: Imagine waking up in a world where reality feels like a fragile illusion—where your own thoughts betray you, where voices whisper secrets only you can hear, and where the very fabric of existence seems to shift before your eyes. This is the daily experience for someone with schizophrenia, a condition that warps perception, distorts emotions, and disrupts the very nature of self-awareness.\n",
"Prediction: 15 (Confidence: 1.00)\n",
"--------------------------------------------------\n"
]
}
],
"source": [
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"def predict(texts):\n",
" \"\"\"\n",
" Perform inference on a list of input texts.\n",
" Args:\n",
" texts (list): List of input text strings\n",
" Returns:\n",
" list: Predicted labels and probabilities\n",
" \"\"\"\n",
" # Tokenize input\n",
" inputs = tokenizer(texts, padding=True, truncation=True, return_tensors=\"pt\")\n",
" \n",
" # Move tensors to the correct device\n",
" input_ids = inputs[\"input_ids\"].to(device)\n",
" attention_mask = inputs[\"attention_mask\"].to(device)\n",
"\n",
" # Run inference\n",
" with torch.no_grad():\n",
" outputs = model(input_ids=input_ids, attention_mask=attention_mask)\n",
" logits = outputs[\"logits\"]\n",
" # Get raw model outputs\n",
" print(\"Raw logits:\", logits)\n",
"\n",
" # Convert logits to probabilities\n",
" probs = torch.nn.functional.softmax(logits, dim=-1)\n",
" preds = torch.argmax(probs, dim=-1).cpu().numpy()\n",
" \n",
" return [{\"text\": text, \"prediction\": pred, \"confidence\": prob.max().item()} for text, pred, prob in zip(texts, preds, probs)]\n",
"\n",
"sample_texts = [\"Imagine waking up in a world where reality feels like a fragile illusion—where your own thoughts betray you, where voices whisper secrets only you can hear, and where the very fabric of existence seems to shift before your eyes. This is the daily experience for someone with schizophrenia, a condition that warps perception, distorts emotions, and disrupts the very nature of self-awareness.\"]\n",
"predictions = predict(sample_texts)\n",
"for result in predictions:\n",
" print(f\"Text: {result['text']}\")\n",
" print(f\"Prediction: {result['prediction']} (Confidence: {result['confidence']:.2f})\")\n",
" print(\"-\" * 50)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "03e83005-b019-427e-937c-498a4e85ef62",
"metadata": {},
"outputs": [],
"source": [
"mental_health_mapping = {\n",
" 0: \"ADHD\",\n",
" 1: \"Anxiety\",\n",
" 2: \"BDD\",\n",
" 3: \"Bipolar\",\n",
" 4: \"BPD\",\n",
" 5: \"Depression\",\n",
" 6: \"Eating Disorder\",\n",
" 7: \"Hoarding Disorder\",\n",
" 8: \"Mental Illness\",\n",
" 9: \"Normal\",\n",
" 10: \"OCD\",\n",
" 11: \"Off My Chest\",\n",
" 12: \"Panic Disorder\",\n",
" 13: \"Personality Disorder\",\n",
" 14: \"PTSD\",\n",
" 15: \"Schizophrenia\",\n",
" 16: \"Social Anxiety\",\n",
" 17: \"Stress\",\n",
" 18: \"Suicidal\"\n",
"}\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "tf_gpu",
"language": "python",
"name": "my_env"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.20"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|