Model AI DeepSeek mencabar dominasi OpenAI. Model -model penalaran maju ini boleh didapati secara bebas, mendemokrasi akses kepada AI yang kuat. Ketahui cara menyempurnakan DeepSeek dengan tutorial video kami:
Tutorial ini menyala-nyata model DeepSeek-R1-Distill-Llama-8B menggunakan dataset rantaian yang dipelajari oleh muka. Model suling ini, yang diperoleh dari Llama 3.1 8b, menawarkan keupayaan penalaran yang setanding dengan DeepSeek-R1 yang asal. Baru untuk LLM dan penalaan halus? Pertimbangkan pengenalan kami kepada LLMS dalam kursus Python.
imej oleh pengarang
Memperkenalkan Model DeepSeek R1DeepSeek-R1-Zero
DeepSeek-R1
Penyulingan DeepSeek
Sumber: DeepSeek-AI/DeepSeek-R1
Ketahui lebih lanjut mengenai ciri-ciri DeepSeek-R1, pembangunan, model sulingan, akses, harga, dan perbandingan Openai O1 dalam catatan blog kami: "DeepSeek-R1: Ciri-ciri, Perbandingan O1, Model Sulingan & Lagi".
Fine-Tuning DeepSeek R1: Panduan Praktikalikuti langkah-langkah ini untuk menyempurnakan model R1 DeepSeek anda:
kami menggunakan akses GPU percuma Kaggle. Buat buku nota Kaggle, sambil menambah wajah dan token yang memeluk & berat badan anda sebagai rahsia. Pasang pakej
<code>%%capture !pip install unsloth !pip install --force-reinstall --no-cache-dir --no-deps git+https://github.com/unslothai/unsloth.git</code>
mengesahkan dengan cli muka yang memeluk dan berat & berat sebelah (wandb):
<code>from huggingface_hub import login from kaggle_secrets import UserSecretsClient user_secrets = UserSecretsClient() hf_token = user_secrets.get_secret("HUGGINGFACE_TOKEN") login(hf_token) import wandb wb_token = user_secrets.get_secret("wandb") wandb.login(key=wb_token) run = wandb.init( project='Fine-tune-DeepSeek-R1-Distill-Llama-8B on Medical COT Dataset', job_type="training", anonymous="allow" )</code>
Muatkan versi Unsloth DeepSeek-R1-Distill-Llama-8B menggunakan kuantisasi 4-bit untuk prestasi yang dioptimumkan:
<code>from unsloth import FastLanguageModel max_seq_length = 2048 dtype = None load_in_4bit = True model, tokenizer = FastLanguageModel.from_pretrained( model_name = "unsloth/DeepSeek-R1-Distill-Llama-8B", max_seq_length = max_seq_length, dtype = dtype, load_in_4bit = load_in_4bit, token = hf_token, )</code>
Tentukan gaya segera dengan ruang letak untuk soalan dan tindak balas. Ini membimbing penalaran langkah demi langkah model:
<code>prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response. ### Instruction: You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. Please answer the following medical question. ### Question: {} ### Response: <think>{}"""</think></code>
menguji model dengan soalan perubatan sampel:
<code>question = "A 61-year-old woman with a long history of involuntary urine loss during activities like coughing or sneezing but no leakage at night undergoes a gynecological exam and Q-tip test. Based on these findings, what would cystometry most likely reveal about her residual volume and detrusor contractions?" FastLanguageModel.for_inference(model) inputs = tokenizer([prompt_style.format(question, "")], return_tensors="pt").to("cuda") outputs = model.generate( input_ids=inputs.input_ids, attention_mask=inputs.attention_mask, max_new_tokens=1200, use_cache=True, ) response = tokenizer.batch_decode(outputs) print(response[0].split("### Response:")[1])</code>
Perhatikan penalaran pra-penalaan model dan mengenal pasti kawasan untuk penambahbaikan melalui penalaan halus.
Ubah suai gaya cepat untuk memasukkan pemegang tempat untuk rantaian pemikiran yang kompleks:
<code>train_prompt_style = """Below is an instruction that describes a task, paired with an input that provides further context. Write a response that appropriately completes the request. Before answering, think carefully about the question and create a step-by-step chain of thoughts to ensure a logical and accurate response. ### Instruction: You are a medical expert with advanced knowledge in clinical reasoning, diagnostics, and treatment planning. Please answer the following medical question. ### Question: {} ### Response: <think> {} </think> {}"""</code>
Buat fungsi untuk memformat dataset:
<code>EOS_TOKEN = tokenizer.eos_token # Must add EOS_TOKEN def formatting_prompts_func(examples): inputs = examples["Question"] cots = examples["Complex_CoT"] outputs = examples["Response"] texts = [] for input, cot, output in zip(inputs, cots, outputs): text = train_prompt_style.format(input, cot, output) + EOS_TOKEN texts.append(text) return { "text": texts, }</code>
memuatkan dan memproses dataset:
<code>from datasets import load_dataset dataset = load_dataset("FreedomIntelligence/medical-o1-reasoning-SFT","en", split = "train[0:500]",trust_remote_code=True) dataset = dataset.map(formatting_prompts_func, batched = True,) dataset["text"][0]</code>
Konfigurasikan model menggunakan LORA:
<code>model = FastLanguageModel.get_peft_model( model, r=16, target_modules=[ "q_proj", "k_proj", "v_proj", "o_proj", "gate_proj", "up_proj", "down_proj", ], lora_alpha=16, lora_dropout=0, bias="none", use_gradient_checkpointing="unsloth", # True or "unsloth" for very long context random_state=3407, use_rslora=False, loftq_config=None, )</code>
Sediakan jurulatih:
<code>from trl import SFTTrainer from transformers import TrainingArguments from unsloth import is_bfloat16_supported trainer = SFTTrainer( model=model, tokenizer=tokenizer, train_dataset=dataset, dataset_text_field="text", max_seq_length=max_seq_length, dataset_num_proc=2, args=TrainingArguments( per_device_train_batch_size=2, gradient_accumulation_steps=4, # Use num_train_epochs = 1, warmup_ratio for full training runs! warmup_steps=5, max_steps=60, learning_rate=2e-4, fp16=not is_bfloat16_supported(), bf16=is_bfloat16_supported(), logging_steps=10, optim="adamw_8bit", weight_decay=0.01, lr_scheduler_type="linear", seed=3407, output_, ), )</code>
Melatih model:
<code>trainer_stats = trainer.train()</code>
(Nota: Sambutan asal termasuk imej kemajuan latihan; ini ditinggalkan di sini sebagai pembiakan imej tidak mungkin.)
Bandingkan hasil dengan menanyakan model halus dengan soalan yang sama seperti sebelumnya. Perhatikan peningkatan ketabahan dan tindak balas tindak balas.
(nota: tindak balas asal termasuk output model yang lebih baik; ini ditinggalkan di sini untuk keringkasan.)
simpan model tempatan dan tolak ke hab muka yang memeluk:
<code>new_model_local = "DeepSeek-R1-Medical-COT" model.save_pretrained(new_model_local) tokenizer.save_pretrained(new_model_local) model.save_pretrained_merged(new_model_local, tokenizer, save_method = "merged_16bit",) new_model_online = "kingabzpro/DeepSeek-R1-Medical-COT" model.push_to_hub(new_model_online) tokenizer.push_to_hub(new_model_online) model.push_to_hub_merged(new_model_online, tokenizer, save_method = "merged_16bit")</code>
(Nota: Sambutan asal termasuk imej yang menunjukkan penjimatan model yang berjaya dan menolak; ini ditinggalkan di sini.)
Respons yang ditulis semula mengekalkan maklumat teras sambil memudahkan struktur dan mengeluarkan pengulangan yang tidak perlu. Blok kod dikekalkan untuk kesempurnaan. Imej -imej itu dirujuk tetapi tidak diterbitkan semula.
Atas ialah kandungan terperinci Penalaan DeepSeek R1 (Model Penalaran). Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!