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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "markdown",
|
5 |
+
"metadata": {},
|
6 |
+
"source": [
|
7 |
+
"## Setting Up"
|
8 |
+
]
|
9 |
+
},
|
10 |
+
{
|
11 |
+
"cell_type": "code",
|
12 |
+
"execution_count": 1,
|
13 |
+
"metadata": {},
|
14 |
+
"outputs": [],
|
15 |
+
"source": [
|
16 |
+
"%%capture\n",
|
17 |
+
"%pip install -U transformers==4.52.1\n",
|
18 |
+
"%pip install -U datasets \n",
|
19 |
+
"%pip install -U accelerate \n",
|
20 |
+
"%pip install -U peft \n",
|
21 |
+
"%pip install -U trl \n",
|
22 |
+
"%pip install -U bitsandbytes"
|
23 |
+
]
|
24 |
+
},
|
25 |
+
{
|
26 |
+
"cell_type": "code",
|
27 |
+
"execution_count": 2,
|
28 |
+
"metadata": {},
|
29 |
+
"outputs": [
|
30 |
+
{
|
31 |
+
"name": "stderr",
|
32 |
+
"output_type": "stream",
|
33 |
+
"text": [
|
34 |
+
"Note: Environment variable`HF_TOKEN` is set and is the current active token independently from the token you've just configured.\n"
|
35 |
+
]
|
36 |
+
}
|
37 |
+
],
|
38 |
+
"source": [
|
39 |
+
"from huggingface_hub import login\n",
|
40 |
+
"import os\n",
|
41 |
+
"\n",
|
42 |
+
"hf_token = os.environ.get(\"HF_TOKEN\")\n",
|
43 |
+
"login(hf_token)"
|
44 |
+
]
|
45 |
+
},
|
46 |
+
{
|
47 |
+
"cell_type": "markdown",
|
48 |
+
"metadata": {},
|
49 |
+
"source": [
|
50 |
+
"## Loading the model and tokenizer"
|
51 |
+
]
|
52 |
+
},
|
53 |
+
{
|
54 |
+
"cell_type": "code",
|
55 |
+
"execution_count": 3,
|
56 |
+
"metadata": {
|
57 |
+
"_cell_guid": "82e08da0-4ee2-4235-a54f-c2bed3609a2b",
|
58 |
+
"_uuid": "059b3da3-4a60-40ed-8a3c-2c395b2c3a8d",
|
59 |
+
"collapsed": false,
|
60 |
+
"jupyter": {
|
61 |
+
"outputs_hidden": false
|
62 |
+
}
|
63 |
+
},
|
64 |
+
"outputs": [],
|
65 |
+
"source": [
|
66 |
+
"from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig\n",
|
67 |
+
"import torch\n"
|
68 |
+
]
|
69 |
+
},
|
70 |
+
{
|
71 |
+
"cell_type": "code",
|
72 |
+
"execution_count": 4,
|
73 |
+
"metadata": {},
|
74 |
+
"outputs": [],
|
75 |
+
"source": [
|
76 |
+
"bnb_config = BitsAndBytesConfig(\n",
|
77 |
+
" load_in_4bit=True,\n",
|
78 |
+
" bnb_4bit_use_double_quant=False,\n",
|
79 |
+
" bnb_4bit_quant_type=\"nf4\",\n",
|
80 |
+
" bnb_4bit_compute_dtype=torch.bfloat16,\n",
|
81 |
+
")"
|
82 |
+
]
|
83 |
+
},
|
84 |
+
{
|
85 |
+
"cell_type": "code",
|
86 |
+
"execution_count": 5,
|
87 |
+
"metadata": {},
|
88 |
+
"outputs": [
|
89 |
+
{
|
90 |
+
"name": "stderr",
|
91 |
+
"output_type": "stream",
|
92 |
+
"text": [
|
93 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n"
|
94 |
+
]
|
95 |
+
},
|
96 |
+
{
|
97 |
+
"data": {
|
98 |
+
"application/vnd.jupyter.widget-view+json": {
|
99 |
+
"model_id": "9ebe60e4563942ddb0d3674d6788bceb",
|
100 |
+
"version_major": 2,
|
101 |
+
"version_minor": 0
|
102 |
+
},
|
103 |
+
"text/plain": [
|
104 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
105 |
+
]
|
106 |
+
},
|
107 |
+
"metadata": {},
|
108 |
+
"output_type": "display_data"
|
109 |
+
}
|
110 |
+
],
|
111 |
+
"source": [
|
112 |
+
"# Load tokenizer & model\n",
|
113 |
+
"\n",
|
114 |
+
"model_dir = \"deepseek-ai/DeepSeek-R1-0528-Qwen3-8B\"\n",
|
115 |
+
"\n",
|
116 |
+
"tokenizer = AutoTokenizer.from_pretrained(model_dir, use_fast=True)\n",
|
117 |
+
"\n",
|
118 |
+
"model = AutoModelForCausalLM.from_pretrained(\n",
|
119 |
+
" model_dir,\n",
|
120 |
+
" quantization_config=bnb_config, \n",
|
121 |
+
" device_map=\"auto\", \n",
|
122 |
+
" torch_dtype=torch.bfloat16,\n",
|
123 |
+
" trust_remote_code=True \n",
|
124 |
+
")\n",
|
125 |
+
"\n",
|
126 |
+
"model.config.use_cache = False\n",
|
127 |
+
"model.config.pretraining_tp = 1"
|
128 |
+
]
|
129 |
+
},
|
130 |
+
{
|
131 |
+
"cell_type": "code",
|
132 |
+
"execution_count": 6,
|
133 |
+
"metadata": {},
|
134 |
+
"outputs": [
|
135 |
+
{
|
136 |
+
"name": "stdout",
|
137 |
+
"output_type": "stream",
|
138 |
+
"text": [
|
139 |
+
"Fri May 30 11:42:48 2025 \n",
|
140 |
+
"+-----------------------------------------------------------------------------------------+\n",
|
141 |
+
"| NVIDIA-SMI 565.57.01 Driver Version: 565.57.01 CUDA Version: 12.7 |\n",
|
142 |
+
"|-----------------------------------------+------------------------+----------------------+\n",
|
143 |
+
"| GPU Name Persistence-M | Bus-Id Disp.A | Volatile Uncorr. ECC |\n",
|
144 |
+
"| Fan Temp Perf Pwr:Usage/Cap | Memory-Usage | GPU-Util Compute M. |\n",
|
145 |
+
"| | | MIG M. |\n",
|
146 |
+
"|=========================================+========================+======================|\n",
|
147 |
+
"| 0 NVIDIA GeForce RTX 4090 On | 00000000:06:00.0 Off | Off |\n",
|
148 |
+
"| 0% 27C P0 58W / 450W | 8360MiB / 24564MiB | 0% Default |\n",
|
149 |
+
"| | | N/A |\n",
|
150 |
+
"+-----------------------------------------+------------------------+----------------------+\n",
|
151 |
+
" \n",
|
152 |
+
"+-----------------------------------------------------------------------------------------+\n",
|
153 |
+
"| Processes: |\n",
|
154 |
+
"| GPU GI CI PID Type Process name GPU Memory |\n",
|
155 |
+
"| ID ID Usage |\n",
|
156 |
+
"|=========================================================================================|\n",
|
157 |
+
"+-----------------------------------------------------------------------------------------+\n"
|
158 |
+
]
|
159 |
+
}
|
160 |
+
],
|
161 |
+
"source": [
|
162 |
+
"!nvidia-smi"
|
163 |
+
]
|
164 |
+
},
|
165 |
+
{
|
166 |
+
"cell_type": "markdown",
|
167 |
+
"metadata": {},
|
168 |
+
"source": [
|
169 |
+
"## Loading and processing the dataset"
|
170 |
+
]
|
171 |
+
},
|
172 |
+
{
|
173 |
+
"cell_type": "code",
|
174 |
+
"execution_count": 7,
|
175 |
+
"metadata": {},
|
176 |
+
"outputs": [],
|
177 |
+
"source": [
|
178 |
+
"train_prompt_style = \"\"\"\n",
|
179 |
+
"Please answer with one of the options in the bracket. Write reasoning in between <analysis></analysis>. Write the answer in between <answer></answer>.\n",
|
180 |
+
"### Question:\n",
|
181 |
+
"{}\n",
|
182 |
+
"\n",
|
183 |
+
"### Response:\n",
|
184 |
+
"{}\"\"\""
|
185 |
+
]
|
186 |
+
},
|
187 |
+
{
|
188 |
+
"cell_type": "code",
|
189 |
+
"execution_count": 8,
|
190 |
+
"metadata": {},
|
191 |
+
"outputs": [],
|
192 |
+
"source": [
|
193 |
+
"EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN\n",
|
194 |
+
"\n",
|
195 |
+
"def formatting_prompts_func(examples):\n",
|
196 |
+
" inputs = examples[\"input\"]\n",
|
197 |
+
" outputs = examples[\"output\"]\n",
|
198 |
+
" texts = []\n",
|
199 |
+
" for question, response in zip(inputs, outputs):\n",
|
200 |
+
" # Remove the \"Q:\" prefix from the question\n",
|
201 |
+
" question = question.replace(\"Q:\", \"\")\n",
|
202 |
+
" \n",
|
203 |
+
" # Append the EOS token to the response if it's not already there\n",
|
204 |
+
" if not response.endswith(tokenizer.eos_token):\n",
|
205 |
+
" response += tokenizer.eos_token\n",
|
206 |
+
" \n",
|
207 |
+
" text = train_prompt_style.format(question, response)\n",
|
208 |
+
" texts.append(text)\n",
|
209 |
+
" return {\"text\": texts}"
|
210 |
+
]
|
211 |
+
},
|
212 |
+
{
|
213 |
+
"cell_type": "code",
|
214 |
+
"execution_count": 9,
|
215 |
+
"metadata": {},
|
216 |
+
"outputs": [
|
217 |
+
{
|
218 |
+
"name": "stdout",
|
219 |
+
"output_type": "stream",
|
220 |
+
"text": [
|
221 |
+
"\n",
|
222 |
+
"Please answer with one of the options in the bracket. Write reasoning in between <analysis></analysis>. Write the answer in between <answer></answer>.\n",
|
223 |
+
"### Question:\n",
|
224 |
+
"A research group wants to assess the relationship between childhood diet and cardiovascular disease in adulthood. A prospective cohort study of 500 children between 10 to 15 years of age is conducted in which the participants' diets are recorded for 1 year and then the patients are assessed 20 years later for the presence of cardiovascular disease. A statistically significant association is found between childhood consumption of vegetables and decreased risk of hyperlipidemia and exercise tolerance. When these findings are submitted to a scientific journal, a peer reviewer comments that the researchers did not discuss the study's validity. Which of the following additional analyses would most likely address the concerns about this study's design?? \n",
|
225 |
+
"{'A': 'Blinding', 'B': 'Crossover', 'C': 'Matching', 'D': 'Stratification', 'E': 'Randomization'},\n",
|
226 |
+
"\n",
|
227 |
+
"### Response:\n",
|
228 |
+
"<analysis>\n",
|
229 |
+
"\n",
|
230 |
+
"This is a question about assessing the validity of a prospective cohort study. The study found an association between childhood diet and cardiovascular disease in adulthood. The peer reviewer is concerned that the researchers did not discuss the validity of the study design. \n",
|
231 |
+
"\n",
|
232 |
+
"To address concerns about validity in a prospective cohort study, we need to consider potential confounding factors that could influence the results. The additional analysis suggested should help control for confounding.\n",
|
233 |
+
"</analysis>\n",
|
234 |
+
"<answer>\n",
|
235 |
+
"D: Stratification\n",
|
236 |
+
"</answer><|end▁of▁sentence|>\n"
|
237 |
+
]
|
238 |
+
}
|
239 |
+
],
|
240 |
+
"source": [
|
241 |
+
"from datasets import load_dataset\n",
|
242 |
+
"\n",
|
243 |
+
"dataset = load_dataset(\n",
|
244 |
+
" \"mamachang/medical-reasoning\",\n",
|
245 |
+
" split=\"train\",\n",
|
246 |
+
" trust_remote_code=True,\n",
|
247 |
+
")\n",
|
248 |
+
"dataset = dataset.map(\n",
|
249 |
+
" formatting_prompts_func,\n",
|
250 |
+
" batched=True,\n",
|
251 |
+
")\n",
|
252 |
+
"print(dataset[\"text\"][10])"
|
253 |
+
]
|
254 |
+
},
|
255 |
+
{
|
256 |
+
"cell_type": "code",
|
257 |
+
"execution_count": 10,
|
258 |
+
"metadata": {},
|
259 |
+
"outputs": [],
|
260 |
+
"source": [
|
261 |
+
"from transformers import DataCollatorForLanguageModeling\n",
|
262 |
+
"\n",
|
263 |
+
"data_collator = DataCollatorForLanguageModeling(\n",
|
264 |
+
" tokenizer=tokenizer,\n",
|
265 |
+
" mlm=False\n",
|
266 |
+
")"
|
267 |
+
]
|
268 |
+
},
|
269 |
+
{
|
270 |
+
"cell_type": "markdown",
|
271 |
+
"metadata": {},
|
272 |
+
"source": [
|
273 |
+
"## Model inference before fine-tuning"
|
274 |
+
]
|
275 |
+
},
|
276 |
+
{
|
277 |
+
"cell_type": "code",
|
278 |
+
"execution_count": 11,
|
279 |
+
"metadata": {},
|
280 |
+
"outputs": [],
|
281 |
+
"source": [
|
282 |
+
"inference_prompt_style = \"\"\"\n",
|
283 |
+
"Please answer with one of the options in the bracket. Write reasoning in between <analysis></analysis>. Write the answer in between <answer></answer>.\n",
|
284 |
+
"\n",
|
285 |
+
"### Question:\n",
|
286 |
+
"{}\n",
|
287 |
+
"\n",
|
288 |
+
"### Response:\n",
|
289 |
+
"<analysis>\n",
|
290 |
+
"\"\"\""
|
291 |
+
]
|
292 |
+
},
|
293 |
+
{
|
294 |
+
"cell_type": "code",
|
295 |
+
"execution_count": 12,
|
296 |
+
"metadata": {},
|
297 |
+
"outputs": [
|
298 |
+
{
|
299 |
+
"name": "stderr",
|
300 |
+
"output_type": "stream",
|
301 |
+
"text": [
|
302 |
+
"Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.\n"
|
303 |
+
]
|
304 |
+
},
|
305 |
+
{
|
306 |
+
"name": "stdout",
|
307 |
+
"output_type": "stream",
|
308 |
+
"text": [
|
309 |
+
"\n",
|
310 |
+
"<analysis>\n",
|
311 |
+
"<think>\n",
|
312 |
+
"First, the question is about a prospective cohort study. In this study, 500 children aged 10 to 15 are followed for 20 years, with their diets recorded for one year, and then they're assessed for cardiovascular disease. A statistically significant association is found between vegetable consumption and decreased risk of hyperlipidemia and better exercise tolerance.\n",
|
313 |
+
"\n",
|
314 |
+
"The peer reviewer is concerned about the study's validity, and I need to figure out which additional analysis would most likely address those concerns. The options are: A) Blinding, B) Crossover, C) Matching, D) Stratification, E) Randomization.\n",
|
315 |
+
"\n",
|
316 |
+
"Validity in a cohort study often refers to how well the study is designed to minimize bias and confounding factors. Since it's a prospective cohort study, the main issue might be confounding or selection bias.\n",
|
317 |
+
"\n",
|
318 |
+
"The study is assessing diet and cardiovascular disease. Diet is a potential confounder because it might be related to other factors that affect cardiovascular disease. For example, children who eat more vegetables might also have better overall health, exercise habits, or socioeconomic status, which could influence outcomes later.\n",
|
319 |
+
"\n",
|
320 |
+
"The reviewer didn't specify what the concern is, but based on the context, it's likely about confounding or bias in the study design. The options are all methods that can be used to address validity issues.\n",
|
321 |
+
"\n",
|
322 |
+
"Let me review each option:\n",
|
323 |
+
"\n",
|
324 |
+
"- A) Blinding: This is typically used in randomized controlled trials to prevent bias in outcome assessment. In a cohort study, blinding might not be directly applicable, especially if the exposure is diet, which is recorded at the beginning and not something that can be blinded during assessment.\n",
|
325 |
+
"\n",
|
326 |
+
"- B) Crossover: This is a design where participants switch between different interventions or conditions. But this is a cohort study, not an intervention study, so crossover might not be relevant. The study is about natural history, not randomizing interventions.\n",
|
327 |
+
"\n",
|
328 |
+
"- C) Matching: In cohort studies, matching is often used in case-control studies to control for confounding, but in a prospective cohort study, matching is not typically done. Cohort studies usually don't involve matching; it's more for case-control.\n",
|
329 |
+
"\n",
|
330 |
+
"- D) Stratification: This is a method to control for confounding by dividing the study population into strata based on potential confounders and analyzing within each stratum. For example, stratifying by age, sex, or other factors to see if the association holds.\n",
|
331 |
+
"\n",
|
332 |
+
"- E) Randomization: This is key in randomized controlled trials to assign participants to groups randomly, reducing selection bias. In a cohort study, randomization isn't used because it's observational; participants are followed based on their existing exposures.\n",
|
333 |
+
"\n",
|
334 |
+
"The study is a prospective cohort study, so it's observational. The researchers recorded diets and then followed up. The concern is about validity, which could be due to confounding factors.\n",
|
335 |
+
"\n",
|
336 |
+
"In cohort studies, to address validity, we often use methods to control for confounding, like stratification or adjustment in statistical models.\n",
|
337 |
+
"\n",
|
338 |
+
"But the question asks for an \"additional analysis\" that would address the concerns. So, it's not about the design itself, but an analysis that could be added.\n",
|
339 |
+
"\n",
|
340 |
+
"Now, looking at the options:\n",
|
341 |
+
"\n",
|
342 |
+
"- Blinding: Not really applicable to a cohort study for diet exposure.\n",
|
343 |
+
"\n",
|
344 |
+
"- Crossover: Doesn't fit, as it's not an intervention study.\n",
|
345 |
+
"\n",
|
346 |
+
"- Matching: In cohort studies, matching is not standard; it's more for case-control. But sometimes, in cohort studies, matching can be used if it's a sub-study or something, but generally, it's not.\n",
|
347 |
+
"\n",
|
348 |
+
"- Stratification: This is a common analysis in cohort studies to check for confounding. For example, if there's an association, stratifying by other factors can show if it's consistent or biased.\n",
|
349 |
+
"\n",
|
350 |
+
"- Randomization: In a cohort study, randomization isn't part of the design; it's retrospective or something. But the study is prospective, meaning they start from the beginning, but it's still observational.\n",
|
351 |
+
"\n",
|
352 |
+
"The peer reviewer might be concerned that the association is due to confounding, and without randomization, it's hard to establish causality.\n",
|
353 |
+
"\n",
|
354 |
+
"But randomization is for the design, not an analysis. The question says \"additional analyses,\" so randomization might not be the right choice because it's a method to be implemented during the study, not an analysis.\n",
|
355 |
+
"\n",
|
356 |
+
"Let's read the question carefully: \"which of the following additional analyses would most likely address the concerns about this study's design?\"\n",
|
357 |
+
"\n",
|
358 |
+
"So, it's specifically about analyses, not design changes.\n",
|
359 |
+
"\n",
|
360 |
+
"In the context of a cohort study, analyses to address validity often include controlling for confounders.\n",
|
361 |
+
"\n",
|
362 |
+
"Stratification is an analysis method where you divide the data into strata and compare.\n",
|
363 |
+
"\n",
|
364 |
+
"Randomization is a design element, not an analysis.\n",
|
365 |
+
"\n",
|
366 |
+
"But option E is \"Randomization,\" which might be confusing.\n",
|
367 |
+
"\n",
|
368 |
+
"Perhaps the reviewer is concerned about selection bias or something, and randomization could be a way to address it if the study were designed differently, but it's not.\n",
|
369 |
+
"\n",
|
370 |
+
"Let's think about what the concerns might be.\n",
|
371 |
+
"\n",
|
372 |
+
"The study is a prospective cohort, so they probably randomized the assignment of diet recording or something? No, the diet is recorded for one year, and then they follow up. But the association is found, and validity might be questioned if there were unmeasured confounders.\n",
|
373 |
+
"\n",
|
374 |
+
"Common concerns in cohort studies include:\n",
|
375 |
+
"\n",
|
376 |
+
"- Confounding by factors not measured\n",
|
377 |
+
"\n",
|
378 |
+
"- Loss to follow-up\n",
|
379 |
+
"\n",
|
380 |
+
"- Measurement error\n",
|
381 |
+
"\n",
|
382 |
+
"- Selection bias if the cohort wasn't representative\n",
|
383 |
+
"\n",
|
384 |
+
"But the options are given, so I need to see which one is most relevant.\n",
|
385 |
+
"\n",
|
386 |
+
"Let's consider each option:\n",
|
387 |
+
"\n",
|
388 |
+
"- A) Blinding: In a cohort study, blinding could be for the outcome assessors to reduce bias, but the exposure is diet, which is recorded at the beginning, so blinding might not help with confounding.\n",
|
389 |
+
"\n",
|
390 |
+
"- B) Crossover: Not applicable, as it's not an intervention study\n"
|
391 |
+
]
|
392 |
+
}
|
393 |
+
],
|
394 |
+
"source": [
|
395 |
+
"question = dataset[10]['input']\n",
|
396 |
+
"question = question.replace(\"Q:\", \"\")\n",
|
397 |
+
"\n",
|
398 |
+
"inputs = tokenizer(\n",
|
399 |
+
" [inference_prompt_style.format(question) + tokenizer.eos_token],\n",
|
400 |
+
" return_tensors=\"pt\"\n",
|
401 |
+
").to(\"cuda\")\n",
|
402 |
+
"\n",
|
403 |
+
"outputs = model.generate(\n",
|
404 |
+
" input_ids=inputs.input_ids,\n",
|
405 |
+
" attention_mask=inputs.attention_mask,\n",
|
406 |
+
" max_new_tokens=1200,\n",
|
407 |
+
" eos_token_id=tokenizer.eos_token_id,\n",
|
408 |
+
" use_cache=True,\n",
|
409 |
+
")\n",
|
410 |
+
"response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n",
|
411 |
+
"print(response[0].split(\"### Response:\")[1])"
|
412 |
+
]
|
413 |
+
},
|
414 |
+
{
|
415 |
+
"cell_type": "markdown",
|
416 |
+
"metadata": {},
|
417 |
+
"source": [
|
418 |
+
"## Setting up the model"
|
419 |
+
]
|
420 |
+
},
|
421 |
+
{
|
422 |
+
"cell_type": "code",
|
423 |
+
"execution_count": 13,
|
424 |
+
"metadata": {},
|
425 |
+
"outputs": [],
|
426 |
+
"source": [
|
427 |
+
"from peft import LoraConfig, get_peft_model\n",
|
428 |
+
"\n",
|
429 |
+
"# LoRA config\n",
|
430 |
+
"peft_config = LoraConfig(\n",
|
431 |
+
" lora_alpha=16, # Scaling factor for LoRA\n",
|
432 |
+
" lora_dropout=0.05, # Add slight dropout for regularization\n",
|
433 |
+
" r=64, # Rank of the LoRA update matrices\n",
|
434 |
+
" bias=\"none\", # No bias reparameterization\n",
|
435 |
+
" task_type=\"CAUSAL_LM\", # Task type: Causal Language Modeling\n",
|
436 |
+
" target_modules=[\n",
|
437 |
+
" \"q_proj\",\n",
|
438 |
+
" \"k_proj\",\n",
|
439 |
+
" \"v_proj\",\n",
|
440 |
+
" \"o_proj\",\n",
|
441 |
+
" \"gate_proj\",\n",
|
442 |
+
" \"up_proj\",\n",
|
443 |
+
" \"down_proj\",\n",
|
444 |
+
" ], # Target modules for LoRA\n",
|
445 |
+
")\n",
|
446 |
+
"\n",
|
447 |
+
"model = get_peft_model(model, peft_config)"
|
448 |
+
]
|
449 |
+
},
|
450 |
+
{
|
451 |
+
"cell_type": "code",
|
452 |
+
"execution_count": 14,
|
453 |
+
"metadata": {},
|
454 |
+
"outputs": [
|
455 |
+
{
|
456 |
+
"name": "stderr",
|
457 |
+
"output_type": "stream",
|
458 |
+
"text": [
|
459 |
+
"No label_names provided for model class `PeftModelForCausalLM`. Since `PeftModel` hides base models input arguments, if label_names is not given, label_names can't be set automatically within `Trainer`. Note that empty label_names list will be used instead.\n"
|
460 |
+
]
|
461 |
+
}
|
462 |
+
],
|
463 |
+
"source": [
|
464 |
+
"from trl import SFTTrainer\n",
|
465 |
+
"from transformers import TrainingArguments\n",
|
466 |
+
"\n",
|
467 |
+
"\n",
|
468 |
+
"# Training Arguments\n",
|
469 |
+
"training_arguments = TrainingArguments(\n",
|
470 |
+
" output_dir=\"DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning\",\n",
|
471 |
+
" per_device_train_batch_size=1,\n",
|
472 |
+
" per_device_eval_batch_size=1,\n",
|
473 |
+
" gradient_accumulation_steps=2,\n",
|
474 |
+
" optim=\"paged_adamw_32bit\",\n",
|
475 |
+
" num_train_epochs=1,\n",
|
476 |
+
" logging_steps=0.2,\n",
|
477 |
+
" warmup_steps=10,\n",
|
478 |
+
" logging_strategy=\"steps\",\n",
|
479 |
+
" learning_rate=2e-4,\n",
|
480 |
+
" fp16=False,\n",
|
481 |
+
" bf16=False,\n",
|
482 |
+
" group_by_length=True,\n",
|
483 |
+
" report_to=\"none\"\n",
|
484 |
+
")\n",
|
485 |
+
"\n",
|
486 |
+
"# Initialize the Trainer\n",
|
487 |
+
"trainer = SFTTrainer(\n",
|
488 |
+
" model=model,\n",
|
489 |
+
" args=training_arguments,\n",
|
490 |
+
" train_dataset=dataset,\n",
|
491 |
+
" peft_config=peft_config,\n",
|
492 |
+
" data_collator=data_collator,\n",
|
493 |
+
")"
|
494 |
+
]
|
495 |
+
},
|
496 |
+
{
|
497 |
+
"cell_type": "markdown",
|
498 |
+
"metadata": {},
|
499 |
+
"source": [
|
500 |
+
"## Model Training"
|
501 |
+
]
|
502 |
+
},
|
503 |
+
{
|
504 |
+
"cell_type": "code",
|
505 |
+
"execution_count": 15,
|
506 |
+
"metadata": {},
|
507 |
+
"outputs": [
|
508 |
+
{
|
509 |
+
"data": {
|
510 |
+
"text/html": [
|
511 |
+
"\n",
|
512 |
+
" <div>\n",
|
513 |
+
" \n",
|
514 |
+
" <progress value='1851' max='1851' style='width:300px; height:20px; vertical-align: middle;'></progress>\n",
|
515 |
+
" [1851/1851 15:16, Epoch 1/1]\n",
|
516 |
+
" </div>\n",
|
517 |
+
" <table border=\"1\" class=\"dataframe\">\n",
|
518 |
+
" <thead>\n",
|
519 |
+
" <tr style=\"text-align: left;\">\n",
|
520 |
+
" <th>Step</th>\n",
|
521 |
+
" <th>Training Loss</th>\n",
|
522 |
+
" </tr>\n",
|
523 |
+
" </thead>\n",
|
524 |
+
" <tbody>\n",
|
525 |
+
" <tr>\n",
|
526 |
+
" <td>371</td>\n",
|
527 |
+
" <td>1.003000</td>\n",
|
528 |
+
" </tr>\n",
|
529 |
+
" <tr>\n",
|
530 |
+
" <td>742</td>\n",
|
531 |
+
" <td>0.942100</td>\n",
|
532 |
+
" </tr>\n",
|
533 |
+
" <tr>\n",
|
534 |
+
" <td>1113</td>\n",
|
535 |
+
" <td>0.918800</td>\n",
|
536 |
+
" </tr>\n",
|
537 |
+
" <tr>\n",
|
538 |
+
" <td>1484</td>\n",
|
539 |
+
" <td>0.895600</td>\n",
|
540 |
+
" </tr>\n",
|
541 |
+
" </tbody>\n",
|
542 |
+
"</table><p>"
|
543 |
+
],
|
544 |
+
"text/plain": [
|
545 |
+
"<IPython.core.display.HTML object>"
|
546 |
+
]
|
547 |
+
},
|
548 |
+
"metadata": {},
|
549 |
+
"output_type": "display_data"
|
550 |
+
},
|
551 |
+
{
|
552 |
+
"name": "stderr",
|
553 |
+
"output_type": "stream",
|
554 |
+
"text": [
|
555 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n",
|
556 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n",
|
557 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n",
|
558 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n"
|
559 |
+
]
|
560 |
+
},
|
561 |
+
{
|
562 |
+
"data": {
|
563 |
+
"text/plain": [
|
564 |
+
"TrainOutput(global_step=1851, training_loss=0.9323031581588077, metrics={'train_runtime': 917.8344, 'train_samples_per_second': 4.033, 'train_steps_per_second': 2.017, 'total_flos': 7.739078550532915e+16, 'train_loss': 0.9323031581588077})"
|
565 |
+
]
|
566 |
+
},
|
567 |
+
"execution_count": 15,
|
568 |
+
"metadata": {},
|
569 |
+
"output_type": "execute_result"
|
570 |
+
}
|
571 |
+
],
|
572 |
+
"source": [
|
573 |
+
"import gc, torch\n",
|
574 |
+
"gc.collect()\n",
|
575 |
+
"torch.cuda.empty_cache()\n",
|
576 |
+
"model.config.use_cache = False\n",
|
577 |
+
"trainer.train()"
|
578 |
+
]
|
579 |
+
},
|
580 |
+
{
|
581 |
+
"cell_type": "markdown",
|
582 |
+
"metadata": {},
|
583 |
+
"source": [
|
584 |
+
"## Model inference after fine-tuning"
|
585 |
+
]
|
586 |
+
},
|
587 |
+
{
|
588 |
+
"cell_type": "code",
|
589 |
+
"execution_count": 16,
|
590 |
+
"metadata": {},
|
591 |
+
"outputs": [
|
592 |
+
{
|
593 |
+
"name": "stderr",
|
594 |
+
"output_type": "stream",
|
595 |
+
"text": [
|
596 |
+
"Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.\n"
|
597 |
+
]
|
598 |
+
},
|
599 |
+
{
|
600 |
+
"name": "stdout",
|
601 |
+
"output_type": "stream",
|
602 |
+
"text": [
|
603 |
+
"\n",
|
604 |
+
"<analysis>\n",
|
605 |
+
"This is a question about evaluating the validity of a prospective cohort study design. The study looked at childhood diet and cardiovascular disease in adulthood. The peer reviewer raised concerns about the study's validity, likely because it was an observational study without randomization or control group. \n",
|
606 |
+
"\n",
|
607 |
+
"The question asks which additional analysis would most likely address the concerns about the study's design. The choices include blinding, crossover, matching, stratification, and randomization. \n",
|
608 |
+
"\n",
|
609 |
+
"Randomization is the key to reducing bias in observational studies. By randomly assigning participants to different groups, you can control for confounding factors. The other choices do not directly address the lack of randomization in the original study design.\n",
|
610 |
+
"</analysis>\n",
|
611 |
+
"<answer>\n",
|
612 |
+
"E: Randomization\n",
|
613 |
+
"</answer>\n"
|
614 |
+
]
|
615 |
+
}
|
616 |
+
],
|
617 |
+
"source": [
|
618 |
+
"question = dataset[10]['input']\n",
|
619 |
+
"question = question.replace(\"Q:\", \"\")\n",
|
620 |
+
"\n",
|
621 |
+
"inputs = tokenizer(\n",
|
622 |
+
" [inference_prompt_style.format(question,) + tokenizer.eos_token],\n",
|
623 |
+
" return_tensors=\"pt\"\n",
|
624 |
+
").to(\"cuda\")\n",
|
625 |
+
"\n",
|
626 |
+
"outputs = model.generate(\n",
|
627 |
+
" input_ids=inputs.input_ids,\n",
|
628 |
+
" attention_mask=inputs.attention_mask,\n",
|
629 |
+
" max_new_tokens=1200,\n",
|
630 |
+
" eos_token_id=tokenizer.eos_token_id,\n",
|
631 |
+
" use_cache=True,\n",
|
632 |
+
")\n",
|
633 |
+
"response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n",
|
634 |
+
"print(response[0].split(\"### Response:\")[1])"
|
635 |
+
]
|
636 |
+
},
|
637 |
+
{
|
638 |
+
"cell_type": "code",
|
639 |
+
"execution_count": 17,
|
640 |
+
"metadata": {},
|
641 |
+
"outputs": [
|
642 |
+
{
|
643 |
+
"name": "stdout",
|
644 |
+
"output_type": "stream",
|
645 |
+
"text": [
|
646 |
+
"<analysis>\n",
|
647 |
+
"\n",
|
648 |
+
"This is a question about assessing the validity of a prospective cohort study. The study found an association between childhood diet and cardiovascular disease in adulthood. The peer reviewer is concerned that the researchers did not discuss the validity of the study design. \n",
|
649 |
+
"\n",
|
650 |
+
"To address concerns about validity in a prospective cohort study, we need to consider potential confounding factors that could influence the results. The additional analysis suggested should help control for confounding.\n",
|
651 |
+
"</analysis>\n",
|
652 |
+
"<answer>\n",
|
653 |
+
"D: Stratification\n",
|
654 |
+
"</answer>\n"
|
655 |
+
]
|
656 |
+
}
|
657 |
+
],
|
658 |
+
"source": [
|
659 |
+
"print(dataset[10]['output'])"
|
660 |
+
]
|
661 |
+
},
|
662 |
+
{
|
663 |
+
"cell_type": "code",
|
664 |
+
"execution_count": 18,
|
665 |
+
"metadata": {},
|
666 |
+
"outputs": [
|
667 |
+
{
|
668 |
+
"name": "stderr",
|
669 |
+
"output_type": "stream",
|
670 |
+
"text": [
|
671 |
+
"Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.\n"
|
672 |
+
]
|
673 |
+
},
|
674 |
+
{
|
675 |
+
"name": "stdout",
|
676 |
+
"output_type": "stream",
|
677 |
+
"text": [
|
678 |
+
"\n",
|
679 |
+
"<analysis>\n",
|
680 |
+
"<answer>\n",
|
681 |
+
"D: Distal symmetric sensorimotor polyneuropathy\n",
|
682 |
+
"</answer> \n",
|
683 |
+
"</think>\n",
|
684 |
+
"<analysis>\n",
|
685 |
+
"\n",
|
686 |
+
"This is a clinical vignette describing a 55-year-old man with burning and shooting pain in his feet and lower legs that worsens at night. He has a history of type 2 diabetes mellitus and hypertension. The description of the pain being burning and shooting in a symmetric distribution in the lower extremities, worsening at night, along with his history of diabetes, is most consistent with distal symmetric sensorimotor polyneuropathy. Autonomic neuropathy would not cause sensory symptoms. Isolated cranial nerve or peripheral nerve neuropathy would not explain the symmetric distribution. Radiculopathy would not explain the sensory symptoms.\n",
|
687 |
+
"</analysis>\n",
|
688 |
+
"<answer>\n",
|
689 |
+
"D: Distal symmetric sensorimotor polyneuropathy\n",
|
690 |
+
"</answer>\n"
|
691 |
+
]
|
692 |
+
}
|
693 |
+
],
|
694 |
+
"source": [
|
695 |
+
"question = dataset[100]['input']\n",
|
696 |
+
"question = question.replace(\"Q:\", \"\")\n",
|
697 |
+
"\n",
|
698 |
+
"inputs = tokenizer(\n",
|
699 |
+
" [inference_prompt_style.format(question) + tokenizer.eos_token],\n",
|
700 |
+
" return_tensors=\"pt\"\n",
|
701 |
+
").to(\"cuda\")\n",
|
702 |
+
"\n",
|
703 |
+
"outputs = model.generate(\n",
|
704 |
+
" input_ids=inputs.input_ids,\n",
|
705 |
+
" attention_mask=inputs.attention_mask,\n",
|
706 |
+
" max_new_tokens=1200,\n",
|
707 |
+
" eos_token_id=tokenizer.eos_token_id,\n",
|
708 |
+
" use_cache=True,\n",
|
709 |
+
")\n",
|
710 |
+
"response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n",
|
711 |
+
"print(response[0].split(\"### Response:\")[1])"
|
712 |
+
]
|
713 |
+
},
|
714 |
+
{
|
715 |
+
"cell_type": "code",
|
716 |
+
"execution_count": 19,
|
717 |
+
"metadata": {},
|
718 |
+
"outputs": [
|
719 |
+
{
|
720 |
+
"name": "stdout",
|
721 |
+
"output_type": "stream",
|
722 |
+
"text": [
|
723 |
+
"<analysis>\n",
|
724 |
+
"\n",
|
725 |
+
"This patient has a history of type 2 diabetes mellitus and is experiencing burning and shooting pains in his feet and lower legs that are worse at night and have progressed over the past 6 months. This presentation is most consistent with distal symmetric sensorimotor polyneuropathy, a type of diabetic neuropathy that affects the distal extremities in a length-dependent pattern. Autonomic neuropathy, cranial nerve neuropathy, and radiculopathy would not explain the symmetric distal distribution. Isolated peripheral neuropathy would not be expected in the setting of longstanding diabetes.\n",
|
726 |
+
"</analysis>\n",
|
727 |
+
"<answer>\n",
|
728 |
+
"D: Distal symmetric sensorimotor polyneuropathy\n",
|
729 |
+
"</answer>\n"
|
730 |
+
]
|
731 |
+
}
|
732 |
+
],
|
733 |
+
"source": [
|
734 |
+
"print(dataset[100]['output'])"
|
735 |
+
]
|
736 |
+
},
|
737 |
+
{
|
738 |
+
"cell_type": "markdown",
|
739 |
+
"metadata": {},
|
740 |
+
"source": [
|
741 |
+
"## Saving the model"
|
742 |
+
]
|
743 |
+
},
|
744 |
+
{
|
745 |
+
"cell_type": "code",
|
746 |
+
"execution_count": 20,
|
747 |
+
"metadata": {},
|
748 |
+
"outputs": [
|
749 |
+
{
|
750 |
+
"data": {
|
751 |
+
"application/vnd.jupyter.widget-view+json": {
|
752 |
+
"model_id": "a83548e89c4743ab8d356440e50f5944",
|
753 |
+
"version_major": 2,
|
754 |
+
"version_minor": 0
|
755 |
+
},
|
756 |
+
"text/plain": [
|
757 |
+
"README.md: 0%| | 0.00/5.90k [00:00<?, ?B/s]"
|
758 |
+
]
|
759 |
+
},
|
760 |
+
"metadata": {},
|
761 |
+
"output_type": "display_data"
|
762 |
+
},
|
763 |
+
{
|
764 |
+
"name": "stderr",
|
765 |
+
"output_type": "stream",
|
766 |
+
"text": [
|
767 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n"
|
768 |
+
]
|
769 |
+
},
|
770 |
+
{
|
771 |
+
"data": {
|
772 |
+
"application/vnd.jupyter.widget-view+json": {
|
773 |
+
"model_id": "7e13d58a863e4f8fba3bf430b3c906a6",
|
774 |
+
"version_major": 2,
|
775 |
+
"version_minor": 0
|
776 |
+
},
|
777 |
+
"text/plain": [
|
778 |
+
"Uploading...: 0%| | 0.00/698M [00:00<?, ?B/s]"
|
779 |
+
]
|
780 |
+
},
|
781 |
+
"metadata": {},
|
782 |
+
"output_type": "display_data"
|
783 |
+
},
|
784 |
+
{
|
785 |
+
"data": {
|
786 |
+
"application/vnd.jupyter.widget-view+json": {
|
787 |
+
"model_id": "3dbd46371b5549039c903a11d39953fb",
|
788 |
+
"version_major": 2,
|
789 |
+
"version_minor": 0
|
790 |
+
},
|
791 |
+
"text/plain": [
|
792 |
+
"Uploading...: 0%| | 0.00/11.4M [00:00<?, ?B/s]"
|
793 |
+
]
|
794 |
+
},
|
795 |
+
"metadata": {},
|
796 |
+
"output_type": "display_data"
|
797 |
+
},
|
798 |
+
{
|
799 |
+
"name": "stderr",
|
800 |
+
"output_type": "stream",
|
801 |
+
"text": [
|
802 |
+
"No files have been modified since last commit. Skipping to prevent empty commit.\n"
|
803 |
+
]
|
804 |
+
},
|
805 |
+
{
|
806 |
+
"data": {
|
807 |
+
"text/plain": [
|
808 |
+
"CommitInfo(commit_url='https://huggingface.co/kingabzpro/DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning/commit/93da153613461499f6f4fe7576689126adfd0a95', commit_message='Upload tokenizer', commit_description='', oid='93da153613461499f6f4fe7576689126adfd0a95', pr_url=None, repo_url=RepoUrl('https://huggingface.co/kingabzpro/DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning', endpoint='https://huggingface.co', repo_type='model', repo_id='kingabzpro/DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning'), pr_revision=None, pr_num=None)"
|
809 |
+
]
|
810 |
+
},
|
811 |
+
"execution_count": 20,
|
812 |
+
"metadata": {},
|
813 |
+
"output_type": "execute_result"
|
814 |
+
}
|
815 |
+
],
|
816 |
+
"source": [
|
817 |
+
"new_model_name = \"DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning\"\n",
|
818 |
+
"trainer.model.push_to_hub(new_model_name)\n",
|
819 |
+
"trainer.processing_class.push_to_hub(new_model_name)"
|
820 |
+
]
|
821 |
+
},
|
822 |
+
{
|
823 |
+
"cell_type": "markdown",
|
824 |
+
"metadata": {},
|
825 |
+
"source": [
|
826 |
+
"## Loading the Adopter and testing the model"
|
827 |
+
]
|
828 |
+
},
|
829 |
+
{
|
830 |
+
"cell_type": "code",
|
831 |
+
"execution_count": 21,
|
832 |
+
"metadata": {},
|
833 |
+
"outputs": [],
|
834 |
+
"source": [
|
835 |
+
"del model\n",
|
836 |
+
"del trainer\n",
|
837 |
+
"torch.cuda.empty_cache()"
|
838 |
+
]
|
839 |
+
},
|
840 |
+
{
|
841 |
+
"cell_type": "code",
|
842 |
+
"execution_count": 22,
|
843 |
+
"metadata": {},
|
844 |
+
"outputs": [
|
845 |
+
{
|
846 |
+
"name": "stderr",
|
847 |
+
"output_type": "stream",
|
848 |
+
"text": [
|
849 |
+
"Unrecognized keys in `rope_scaling` for 'rope_type'='yarn': {'attn_factor'}\n"
|
850 |
+
]
|
851 |
+
},
|
852 |
+
{
|
853 |
+
"data": {
|
854 |
+
"application/vnd.jupyter.widget-view+json": {
|
855 |
+
"model_id": "0c1fdc4ef0d3446e83fd86105230fe92",
|
856 |
+
"version_major": 2,
|
857 |
+
"version_minor": 0
|
858 |
+
},
|
859 |
+
"text/plain": [
|
860 |
+
"Loading checkpoint shards: 0%| | 0/2 [00:00<?, ?it/s]"
|
861 |
+
]
|
862 |
+
},
|
863 |
+
"metadata": {},
|
864 |
+
"output_type": "display_data"
|
865 |
+
},
|
866 |
+
{
|
867 |
+
"data": {
|
868 |
+
"application/vnd.jupyter.widget-view+json": {
|
869 |
+
"model_id": "04e8fd839dc94247bfdfd47b36458ba2",
|
870 |
+
"version_major": 2,
|
871 |
+
"version_minor": 0
|
872 |
+
},
|
873 |
+
"text/plain": [
|
874 |
+
"adapter_config.json: 0%| | 0.00/870 [00:00<?, ?B/s]"
|
875 |
+
]
|
876 |
+
},
|
877 |
+
"metadata": {},
|
878 |
+
"output_type": "display_data"
|
879 |
+
},
|
880 |
+
{
|
881 |
+
"data": {
|
882 |
+
"application/vnd.jupyter.widget-view+json": {
|
883 |
+
"model_id": "559929904fc3436faff5b28ece76f727",
|
884 |
+
"version_major": 2,
|
885 |
+
"version_minor": 0
|
886 |
+
},
|
887 |
+
"text/plain": [
|
888 |
+
"adapter_model.safetensors: 0%| | 0.00/698M [00:00<?, ?B/s]"
|
889 |
+
]
|
890 |
+
},
|
891 |
+
"metadata": {},
|
892 |
+
"output_type": "display_data"
|
893 |
+
}
|
894 |
+
],
|
895 |
+
"source": [
|
896 |
+
"from transformers import AutoTokenizer, AutoModelForCausalLM, BitsAndBytesConfig\n",
|
897 |
+
"from peft import PeftModel\n",
|
898 |
+
"import torch\n",
|
899 |
+
"\n",
|
900 |
+
"# Base model\n",
|
901 |
+
"base_model_id = \"deepseek-ai/DeepSeek-R1-0528-Qwen3-8B\"\n",
|
902 |
+
"\n",
|
903 |
+
"# Your fine-tuned LoRA adapter repository\n",
|
904 |
+
"lora_adapter_id = \"kingabzpro/DeepSeek-R1-0528-Qwen3-8B-Medical-Reasoning\"\n",
|
905 |
+
"\n",
|
906 |
+
"# Load the model in 4-bit\n",
|
907 |
+
"bnb_config = BitsAndBytesConfig(\n",
|
908 |
+
" load_in_4bit=True,\n",
|
909 |
+
" bnb_4bit_use_double_quant=False,\n",
|
910 |
+
" bnb_4bit_quant_type=\"nf4\",\n",
|
911 |
+
" bnb_4bit_compute_dtype=torch.bfloat16,\n",
|
912 |
+
")\n",
|
913 |
+
"\n",
|
914 |
+
"# Load base model\n",
|
915 |
+
"base_model = AutoModelForCausalLM.from_pretrained(\n",
|
916 |
+
" base_model_id,\n",
|
917 |
+
" device_map=\"auto\",\n",
|
918 |
+
" torch_dtype=torch.bfloat16,\n",
|
919 |
+
" quantization_config=bnb_config,\n",
|
920 |
+
" trust_remote_code=True,\n",
|
921 |
+
")\n",
|
922 |
+
"\n",
|
923 |
+
"# Attach the LoRA adapter\n",
|
924 |
+
"model = PeftModel.from_pretrained(\n",
|
925 |
+
" base_model,\n",
|
926 |
+
" lora_adapter_id,\n",
|
927 |
+
" device_map=\"auto\",\n",
|
928 |
+
" trust_remote_code=True,\n",
|
929 |
+
")\n",
|
930 |
+
"\n",
|
931 |
+
"# Load tokenizer\n",
|
932 |
+
"tokenizer = AutoTokenizer.from_pretrained(base_model_id, trust_remote_code=True)\n"
|
933 |
+
]
|
934 |
+
},
|
935 |
+
{
|
936 |
+
"cell_type": "code",
|
937 |
+
"execution_count": 23,
|
938 |
+
"metadata": {},
|
939 |
+
"outputs": [
|
940 |
+
{
|
941 |
+
"name": "stderr",
|
942 |
+
"output_type": "stream",
|
943 |
+
"text": [
|
944 |
+
"Setting `pad_token_id` to `eos_token_id`:151645 for open-end generation.\n"
|
945 |
+
]
|
946 |
+
},
|
947 |
+
{
|
948 |
+
"name": "stdout",
|
949 |
+
"output_type": "stream",
|
950 |
+
"text": [
|
951 |
+
"\n",
|
952 |
+
"<analysis>\n",
|
953 |
+
"\n",
|
954 |
+
"This is a question about evaluating the validity of a prospective cohort study design. The study looked at childhood diet and cardiovascular disease in adulthood. The peer reviewer was concerned about the study's validity. \n",
|
955 |
+
"\n",
|
956 |
+
"To address concerns about validity in a prospective cohort study, we need to consider potential confounding factors. The choices given are different statistical methods that can help control for confounding. \n",
|
957 |
+
"\n",
|
958 |
+
"Blinding and crossover designs are not applicable to a prospective cohort study. Matching and stratification can help control for confounding by balancing the distribution of confounders between groups. Randomization is the best way to minimize confounding by randomly assigning participants to different exposure groups.\n",
|
959 |
+
"</think>\n",
|
960 |
+
"<analysis>\n",
|
961 |
+
"This is a question about evaluating the validity of a prospective cohort study design. The study looked at childhood diet and cardiovascular disease in adulthood. The peer reviewer was concerned about the study's validity. \n",
|
962 |
+
"\n",
|
963 |
+
"To address concerns about validity in a prospective cohort study, we need to consider potential confounding factors. The choices given are different statistical methods that can help control for confounding. \n",
|
964 |
+
"\n",
|
965 |
+
"Blinding and crossover designs are not applicable to a prospective cohort study. Matching and stratification can help control for confounding by balancing the distribution of confounders between groups. Randomization is the best way to minimize confounding by randomly assigning participants to different exposure groups.\n",
|
966 |
+
"</analysis>\n",
|
967 |
+
"<answer>\n",
|
968 |
+
"E: Randomization\n",
|
969 |
+
"</answer>\n"
|
970 |
+
]
|
971 |
+
}
|
972 |
+
],
|
973 |
+
"source": [
|
974 |
+
"# Inference example\n",
|
975 |
+
"prompt = \"\"\"\n",
|
976 |
+
"Please answer with one of the options in the bracket. Write reasoning in between <analysis></analysis>. Write the answer in between <answer></answer>.\n",
|
977 |
+
"\n",
|
978 |
+
"### Question:\n",
|
979 |
+
"A research group wants to assess the relationship between childhood diet and cardiovascular disease in adulthood.\n",
|
980 |
+
"A prospective cohort study of 500 children between 10 to 15 years of age is conducted in which the participants' diets are recorded for 1 year and then the patients are assessed 20 years later for the presence of cardiovascular disease.\n",
|
981 |
+
"A statistically significant association is found between childhood consumption of vegetables and decreased risk of hyperlipidemia and exercise tolerance.\n",
|
982 |
+
"When these findings are submitted to a scientific journal, a peer reviewer comments that the researchers did not discuss the study's validity.\n",
|
983 |
+
"Which of the following additional analyses would most likely address the concerns about this study's design? \n",
|
984 |
+
"{'A': 'Blinding', 'B': 'Crossover', 'C': 'Matching', 'D': 'Stratification', 'E': 'Randomization'},\n",
|
985 |
+
"### Response:\n",
|
986 |
+
"<analysis>\n",
|
987 |
+
"\n",
|
988 |
+
"\"\"\"\n",
|
989 |
+
"\n",
|
990 |
+
"inputs = tokenizer(\n",
|
991 |
+
" [prompt + tokenizer.eos_token],\n",
|
992 |
+
" return_tensors=\"pt\"\n",
|
993 |
+
").to(\"cuda\")\n",
|
994 |
+
"\n",
|
995 |
+
"outputs = model.generate(\n",
|
996 |
+
" input_ids=inputs.input_ids,\n",
|
997 |
+
" attention_mask=inputs.attention_mask,\n",
|
998 |
+
" max_new_tokens=1200,\n",
|
999 |
+
" eos_token_id=tokenizer.eos_token_id,\n",
|
1000 |
+
" use_cache=True,\n",
|
1001 |
+
")\n",
|
1002 |
+
"response = tokenizer.batch_decode(outputs, skip_special_tokens=True)\n",
|
1003 |
+
"print(response[0].split(\"### Response:\")[1])"
|
1004 |
+
]
|
1005 |
+
}
|
1006 |
+
],
|
1007 |
+
"metadata": {
|
1008 |
+
"kaggle": {
|
1009 |
+
"accelerator": "nvidiaTeslaT4",
|
1010 |
+
"dataSources": [
|
1011 |
+
{
|
1012 |
+
"modelId": 322000,
|
1013 |
+
"modelInstanceId": 301517,
|
1014 |
+
"sourceId": 363139,
|
1015 |
+
"sourceType": "modelInstanceVersion"
|
1016 |
+
}
|
1017 |
+
],
|
1018 |
+
"dockerImageVersionId": 31011,
|
1019 |
+
"isGpuEnabled": true,
|
1020 |
+
"isInternetEnabled": true,
|
1021 |
+
"language": "python",
|
1022 |
+
"sourceType": "notebook"
|
1023 |
+
},
|
1024 |
+
"kernelspec": {
|
1025 |
+
"display_name": "Python 3 (ipykernel)",
|
1026 |
+
"language": "python",
|
1027 |
+
"name": "python3"
|
1028 |
+
},
|
1029 |
+
"language_info": {
|
1030 |
+
"codemirror_mode": {
|
1031 |
+
"name": "ipython",
|
1032 |
+
"version": 3
|
1033 |
+
},
|
1034 |
+
"file_extension": ".py",
|
1035 |
+
"mimetype": "text/x-python",
|
1036 |
+
"name": "python",
|
1037 |
+
"nbconvert_exporter": "python",
|
1038 |
+
"pygments_lexer": "ipython3",
|
1039 |
+
"version": "3.11.10"
|
1040 |
+
}
|
1041 |
+
},
|
1042 |
+
"nbformat": 4,
|
1043 |
+
"nbformat_minor": 4
|
1044 |
+
}
|