Views
No views yet
google/gemma-3-4b-pt is a 4B base model suitable for custom post-training. In this project, we apply continuous pretraining (CPT) on a metadata-only dataset of Physics-Informed Neural Networks (PINNs) papers from arXiv. The goal is to make the model respond more precisely to questions from this domain and improve its familiarity with PINN-related terminology, topics, and paper metadata.max_seq_length value to 1024:
90/5/5 ratio.trl and a causal language modelling objective with the following settings:10246421e-4adamw_torch
1import torch
2from transformers import AutoTokenizer, AutoModelForCausalLM
3from peft import PeftModel
4
5base_model_id = "google/gemma-3-4b-pt"
6adapter_id = "pymlex/gemma3-4b-pinn-expert"
7
8tokenizer = AutoTokenizer.from_pretrained(base_model_id)
9if tokenizer.pad_token is None:
10 tokenizer.pad_token = tokenizer.eos_token
11
12base_model = AutoModelForCausalLM.from_pretrained(
13 base_model_id,
14 device_map="auto",
15 torch_dtype=torch.bfloat16 if torch.cuda.is_available() and torch.cuda.is_bf16_supported() else torch.float16,
16)
17
18model = PeftModel.from_pretrained(base_model, adapter_id)
19model.eval()1def build_prompt(record):
2 return (
3 f"Title: {record.get('Title', '')}\n"
4 f"Authors: {record.get('Authors', '')}\n"
5 f"Published: {record.get('Published', '')}\n"
6 f"Updated: {record.get('Updated', '')}\n"
7 f"Summary: "
8 )
9
10
11def generate_continuation(model, tokenizer, prompt, max_new_tokens=220):
12 inputs = tokenizer(prompt, return_tensors="pt").to(model.device)
13
14 with torch.inference_mode():
15 outputs = model.generate(
16 **inputs,
17 max_new_tokens=max_new_tokens,
18 do_sample=False,
19 num_beams=1,
20 repetition_penalty=1.1,
21 eos_token_id=tokenizer.eos_token_id,
22 pad_token_id=tokenizer.pad_token_id,
23 )
24
25 prompt_len = inputs["input_ids"].shape[1]
26 continuation_ids = outputs[0, prompt_len:]
27
28 continuation = tokenizer.decode(continuation_ids, skip_special_tokens=True)
29 return continuation
30
31
32sample_record = {
33 "Title": "fPINNs: Fractional Physics-Informed Neural Networks",
34 "Authors": "Guofei Pang, Lu Lu, George Em Karniadakis",
35 "Published": "2018-11-20T02:48:36Z",
36 "Updated": "2018-11-20T02:48:36Z",
37}
38
39prompt = build_prompt(sample_record)
40output = generate_continuation(model, tokenizer, prompt, max_new_tokens=400)
41print("Prompt:")
42print(prompt)
43print("\nGenerated continuation:")
44print(output)| Model | Perplexity |
|---|---|
| Base Model | 9.200 |
| Tuned Model | 6.646 |
28%. This shows that CPT makes the model less surprised by PINN-domain text even on held-out examples.