CoolFace
Modelpublic

somrajmondal/phi3-mini-finance-lora-fp16

sourceHugging Faceapache-2.0updated 4mo agoView on Hugging Face
1likes50downloads
Model Card

๐Ÿ’น Phi-3-mini Finance LoRA (fp16)

A domain-specialized version of Microsoft's Phi-3-mini-4k-instruct, fine-tuned on financial Q&A data using LoRA + 4-bit NF4 quantization โ€” trained entirely on a free Google Colab T4 GPU.

<img src="https://raw.githubusercontent.com/unslothai/unsloth/main/images/unsloth%20made%20with%20love.png" width="200"/>


๐Ÿ“‹ Model Details

FieldDetails
Developed bysomrajmondal
Base modelunsloth/phi-3-mini-4k-instruct-bnb-4bit
Model typeCausal Language Model (Phi-3 architecture)
Parameters3.8B total / 29.8M trainable (0.78%)
LanguageEnglish
LicenseApache 2.0
Fine-tuning methodLoRA (Low-Rank Adaptation)
Quantization4-bit NF4 during training, merged to fp16

๐ŸŽฏ What This Model Does

This model is fine-tuned to answer finance and investment questions clearly and accurately. It was trained on the gbharti/finance-alpaca dataset covering topics like:

  • โ€”Stock market concepts (P/E ratio, dividends, market cap)
  • โ€”Investment strategies (ETFs, mutual funds, dollar-cost averaging)
  • โ€”Fixed income (bonds, yields, interest rates)
  • โ€”Personal finance (compound interest, savings, budgeting)
  • โ€”Financial planning and portfolio diversification

๐Ÿ‹๏ธ Training Details

SettingValue
Datasetgbharti/finance-alpaca
Training rows5,000
Epochs2
Total steps1,250
Batch size2 (effective: 8 with grad accumulation)
Learning rate2e-4 (cosine scheduler)
OptimizerAdamW 8-bit
LoRA rank (r)16
LoRA alpha16
LoRA dropout0.05
LoRA target modulesqproj, kproj, vproj, oproj, gateproj, upproj, down_proj
Max sequence length1024
Final training loss2.18
Peak VRAM used3.69 GB
Training hardwareGoogle Colab Free T4 (15GB VRAM)
Training time~2 hours 20 minutes
FrameworkUnsloth + HuggingFace TRL

๐Ÿš€ How to Use

Quick Start (Transformers)

python
from transformers import AutoTokenizer, AutoModelForCausalLM
import torch

model_id = "somrajmondal/phi3-mini-finance-lora-fp16"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
    model_id,
    torch_dtype=torch.float16,
    device_map="auto",
)

question = "What is compound interest and why is it important?"

prompt = f"""<|user|>
{question}
<|end|>
<|assistant|>
"""

inputs = tokenizer(prompt, return_tensors="pt").to(model.device)

outputs = model.generate(
    **inputs,
    max_new_tokens=300,
    do_sample=False,
    repetition_penalty=1.3,
    eos_token_id=tokenizer.eos_token_id,
    pad_token_id=tokenizer.eos_token_id,
)

response = tokenizer.decode(
    outputs[0][inputs["input_ids"].shape[1]:],
    skip_special_tokens=True
)
print(response)

With Unsloth (Faster Inference)

python
from unsloth import FastLanguageModel
import torch

model, tokenizer = FastLanguageModel.from_pretrained(
    model_name     = "somrajmondal/phi3-mini-finance-lora-fp16",
    max_seq_length = 1024,
    dtype          = None,
    load_in_4bit   = True,   # set False for full fp16
)
FastLanguageModel.for_inference(model)

question = "What is the difference between a stock and a bond?"

prompt = f"""<|user|>
{question}
<|end|>
<|assistant|>
"""

inputs = tokenizer(prompt, return_tensors="pt").to("cuda")
outputs = model.generate(
    **inputs,
    max_new_tokens=300,
    do_sample=False,
    repetition_penalty=1.3,
    eos_token_id=tokenizer.eos_token_id,
    pad_token_id=tokenizer.eos_token_id,
)
response = tokenizer.decode(
    outputs[0][inputs["input_ids"].shape[1]:],
    skip_special_tokens=True
)
print(response)

๐Ÿ’ฌ Prompt Format

This model uses the Phi-3 chat template. Always wrap your input like this:

<|user|>
Your finance question here
<|end|>
<|assistant|>

๐Ÿ“Š Example Outputs

Q: What is a P/E ratio?

The price-to-earnings (P/E) ratio measures a company's current share price relative to its earnings per share. A high P/E suggests investors expect future growth, while a low P/E may indicate an undervalued stock or slower expected growth.

Q: What is dollar cost averaging?

Dollar cost averaging is an investment strategy where you invest a fixed amount of money at regular intervals, regardless of market conditions. This reduces the impact of volatility and removes the need to time the market.

โš ๏ธ Limitations

  • โ€”Trained on only 5,000 rows โ€” may lack depth on niche financial topics
  • โ€”Not suitable for real financial advice โ€” always consult a professional
  • โ€”May occasionally produce incomplete answers on complex multi-part questions
  • โ€”Training loss of 2.18 indicates room for improvement with more epochs/data

๐Ÿ”ง Recommended Inference Settings

python
# For factual / accurate answers (recommended)
do_sample          = False
repetition_penalty = 1.3
max_new_tokens     = 300

# For more creative / detailed answers
do_sample   = True
temperature = 0.3
top_p       = 0.9

๐Ÿ“ฆ Training Framework


๐Ÿ“„ Citation

If you use this model, please cite the base model and dataset:

bibtex
@misc{phi3-mini-finance-lora,
  author    = {somrajmondal},
  title     = {Phi-3-mini Finance LoRA},
  year      = {2025},
  publisher = {HuggingFace},
  url       = {https://huggingface.co/somrajmondal/phi3-mini-finance-lora-fp16}
}