NeuroBERT-Tiny: Compact BERT Power for Real-Time AI Applications ๐ค
NeuroBERT-Tiny is an ultra-lightweight, real-time NLP model optimized for edge devices like microcontrollers, IoT sensors, and low-power embedded systems.
Derived from Google's BERT architecture, it provides efficient contextual language understanding in highly resource-constrained environments.
With a quantized size of ~15MB and ~5M parameters, it’s designed for ultra-low-latency and offline operation, making it perfect for privacy-first applications with minimal connectivity. Hugging Face
✨ Key Features
- Ultra-Lightweight: ~15MB footprint fits devices with extremely limited storage.
- Contextual Understanding: Captures semantic relationships with a highly compact architecture.
- Offline Capability: Fully functional without internet access.
- Real-Time Inference: Optimized for low-power CPUs, mobile NPUs, and microcontrollers.
- Versatile Applications: Supports masked language modeling (MLM), intent detection, text classification, and named entity recognition (NER).
๐ Supported NLP Tasks
Task | Description | Hugging Face Pipeline |
---|---|---|
Masked Language Modeling | Predict missing words in sentences | fill-mask |
Text Classification | Classify text into predefined categories | text-classification |
Intent Detection | Identify user intent from input | text-classification |
Named Entity Recognition | Detect and classify named entities in text | ner |
⚙️ Installation
Ensure your environment supports Python 3.6+ and has ~15MB of storage for model weights.
pip install transformers torch datasets scikit-learn pandas seqeval
๐ฅ Loading the Model
You can load the model directly using the Hugging Face Transformers library:
from transformers import AutoModelForMaskedLM, AutoTokenizer
model_name = "boltuix/NeuroBERT-Tiny"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForMaskedLM.from_pretrained(model_name)
๐ Quickstart Examples
1. Masked Language Modeling
Predict missing words in sentences:
from transformers import pipeline
mask_filler = pipeline("fill-mask", model="boltuix/NeuroBERT-Tiny", tokenizer="boltuix/NeuroBERT-Tiny")
sentence = "The smart fan will [MASK] automatically when it gets hot."
results = mask_filler(sentence)
for r in results:
print(f"Prediction: {r['token_str']}, Score: {r['score']:.4f}")
# Example Output:
# Prediction: turn, Score: 0.4010
# Prediction: switch, Score: 0.1915
# Prediction: shut, Score: 0.1460
# Prediction: activate, Score: 0.0866
# Prediction: run, Score: 0.0621
2. Intent Classification
Fine-tune and classify text into intents (e.g., greeting, turn_off_fan):
# Install required packages before running:
# pip install transformers datasets scikit-learn pandas
import pandas as pd
from sklearn.model_selection import train_test_split
from datasets import Dataset
from transformers import (
AutoTokenizer, AutoModelForSequenceClassification, Trainer,
TrainingArguments, EarlyStoppingCallback
)
from sklearn.metrics import accuracy_score
# Step 1: Define dataset with intents
intents_data = {
"greeting": [
"Hello", "Hi there", "Hey", "Good morning", "Good evening", "How are you?",
"Nice to meet you", "Howdy", "Hey there", "Yo",
"Hi, assistant", "Greetings", "Hi buddy", "Good day", "Welcome back",
"Hello again", "Hey assistant", "Hi friend", "Nice seeing you", "What's up"
],
"turn_off_fan": [
"Turn off the fan", "Please switch off the fan", "Can you turn the fan off?",
"Stop the fan", "Fan off", "Shut down the fan", "Disable the fan",
"I want the fan off", "Kill the fan", "Turn the fan off now",
"Cut the fan", "Fan should be off", "Make the fan stop", "Fan off please",
"Deactivate fan", "Turn that fan off", "Power down the fan", "Stop spinning fan",
"Fan needs to go off", "Turn off ceiling fan"
],
"turn_on_light": [
"Turn on the light", "Switch on the lights", "Light on please",
"Enable the lights", "Lights up", "Please turn on light",
"Make it bright", "Illuminate the room", "Power on the light",
"Start the lights", "I need lights on", "Activate light",
"Turn lights on", "Can you switch on light?", "Lights, please",
"Turn that light on", "Wake up the lights", "Brighten the room",
"Let there be light", "Make room visible"
],
"weather_query": [
"What's the weather today?", "Will it rain?", "Tell me the weather forecast",
"How's the weather?", "Give me today's weather", "Is it sunny?",
"Will it be cloudy?", "Weather update", "Forecast for today",
"Any chance of rain?", "Show me weather", "Is it going to snow?",
"Do I need an umbrella?", "Weather news", "Will it be hot today?",
"Is it cold outside?", "Weather check", "Current weather status",
"What's the temperature?", "Temperature outside now?"
],
"goodbye": [
"Goodbye", "Bye", "See you later", "Catch you later",
"Talk to you soon", "Farewell", "I'm leaving", "Take care",
"Until next time", "Later", "See ya", "Bye-bye", "Peace out",
"Gotta go", "End chat", "That's all", "Over and out",
"Catch you next time", "Talk later", "Quit now"
]
}
# Flatten dataset
examples = [(text, intent) for intent, texts in intents_data.items() for text in texts]
df = pd.DataFrame(examples, columns=["text", "label"])
# Encode labels
label2id = {label: idx for idx, label in enumerate(df["label"].unique())}
id2label = {idx: label for label, idx in label2id.items()}
df["label_id"] = df["label"].map(label2id)
# Split into train and validation
train_df, val_df = train_test_split(df, test_size=0.2, stratify=df["label_id"], random_state=42)
train_dataset = Dataset.from_pandas(train_df[["text", "label_id"]])
val_dataset = Dataset.from_pandas(val_df[["text", "label_id"]])
# Step 2: Load model and tokenizer
model_name = "boltuix/NeuroBERT-Tiny"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(
model_name,
num_labels=len(label2id),
id2label=id2label,
label2id=label2id
)
# Step 3: Tokenization
def tokenize(batch):
tokenized_inputs = tokenizer(batch["text"], truncation=True, padding=True)
tokenized_inputs["labels"] = batch["label_id"]
return tokenized_inputs
train_dataset = train_dataset.map(tokenize, batched=True)
val_dataset = val_dataset.map(tokenize, batched=True)
# Step 4: Define training arguments
training_args = TrainingArguments(
output_dir="./intent_model",
eval_strategy="epoch",
save_strategy="epoch",
learning_rate=2e-5,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
num_train_epochs=5,
weight_decay=0.01,
load_best_model_at_end=True,
metric_for_best_model="accuracy"
)
# Metrics
def compute_metrics(eval_pred):
predictions = eval_pred.predictions.argmax(-1)
return {"accuracy": accuracy_score(eval_pred.label_ids, predictions)}
# Step 5: Trainer setup
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
compute_metrics=compute_metrics,
tokenizer=tokenizer,
callbacks=[EarlyStoppingCallback(early_stopping_patience=2)]
)
# Step 6: Train and Save
trainer.train()
trainer.save_model("./fine-tuned-NeuroBERT-Tiny-intents")
# Step 7: Inference
from transformers import pipeline
model_path = "./fine-tuned-NeuroBERT-Tiny-intents"
classifier = pipeline("text-classification", model=model_path)
test_sentences = [
"Hello",
"Can you turn off the fan?",
"Turn on the light",
"What's the weather today?",
"Bye"
]
for text in test_sentences:
result = classifier(text)[0]
print(f"๐ง You: {text}")
print(f"๐ค Bot ({result['label']} - {result['score']:.2f}): Intent recognized\n")
3. Named Entity Recognition (NER)
Fine-tune and identify named entities in text:
from transformers import (
AutoTokenizer, AutoModelForTokenClassification,
DataCollatorForTokenClassification, Trainer,
TrainingArguments, EarlyStoppingCallback, pipeline
)
from datasets import Dataset
import numpy as np
# Define 10 IoT sample data points
samples = [
{"tokens": ["Turn", "on", "the", "kitchen", "light"], "ner_tags": [0, 0, 0, 1, 2]},
{"tokens": ["Switch", "off", "bedroom", "fan"], "ner_tags": [0, 0, 1, 2]},
{"tokens": ["Open", "the", "garage", "door"], "ner_tags": [0, 0, 1, 2]},
{"tokens": ["Close", "the", "window"], "ner_tags": [0, 0, 1]},
{"tokens": ["Set", "thermostat", "to", "22", "degrees"], "ner_tags": [0, 1, 0, 0, 0]},
{"tokens": ["Play", "jazz", "in", "living", "room"], "ner_tags": [0, 1, 0, 1, 2]},
{"tokens": ["Dim", "the", "dining", "room", "lights"], "ner_tags": [0, 0, 1, 2, 2]},
{"tokens": ["Lock", "the", "front", "door"], "ner_tags": [0, 0, 1, 2]},
{"tokens": ["Start", "the", "coffee", "machine"], "ner_tags": [0, 0, 1, 2]},
{"tokens": ["Turn", "off", "garden", "sprinkler"], "ner_tags": [0, 0, 1, 2]},
]
# Define labels
label_list = ["O", "DEVICE", "ACTION"] # 0 = O, 1 = DEVICE, 2 = ACTION
label2id = {l: i for i, l in enumerate(label_list)}
id2label = {i: l for l, i in label2id.items()}
# Adjust label indices for consistency
for sample in samples:
sample["ner_tags"] = [int(tag) for tag in sample["ner_tags"]]
# Convert to Hugging Face dataset
dataset = Dataset.from_list(samples).train_test_split(test_size=0.2)
tokenizer = AutoTokenizer.from_pretrained("boltuix/NeuroBERT-Tiny")
# Load model
model = AutoModelForTokenClassification.from_pretrained(
"boltuix/NeuroBERT-Tiny",
num_labels=len(label_list),
id2label=id2label,
label2id=label2id
)
# Tokenize and align labels
def tokenize_and_align_labels(examples):
tokenized = tokenizer(examples["tokens"], truncation=True, padding=True, is_split_into_words=True)
labels = []
for i, label in enumerate(examples["ner_tags"]):
word_ids = tokenized.word_ids(batch_index=i)
previous_word_idx = None
label_ids = []
for word_idx in word_ids:
if word_idx is None:
label_ids.append(-100)
elif word_idx != previous_word_idx:
label_ids.append(label[word_idx])
else:
label_ids.append(label[word_idx])
previous_word_idx = word_idx
labels.append(label_ids)
tokenized["labels"] = labels
return tokenized
# Tokenize
tokenized_ds = dataset.map(tokenize_and_align_labels, batched=True)
data_collator = DataCollatorForTokenClassification(tokenizer)
# Training args
training_args = TrainingArguments(
output_dir="./ner_model",
eval_strategy="epoch",
save_strategy="epoch",
per_device_train_batch_size=2,
num_train_epochs=10,
learning_rate=5e-5,
weight_decay=0.01,
logging_steps=5,
load_best_model_at_end=True
)
# Train the model
trainer = Trainer(
model=model,
args=training_args,
train_dataset=tokenized_ds["train"],
eval_dataset=tokenized_ds["test"],
tokenizer=tokenizer,
data_collator=data_collator,
callbacks=[EarlyStoppingCallback(early_stopping_patience=2)]
)
trainer.train()
trainer.save_model("neurobert-tiny-iot-ner")
# Inference
ner = pipeline("token-classification", model="neurobert-tiny-iot-ner", tokenizer=tokenizer, aggregation_strategy="simple")
print(ner("Turn on the garden lights when someone enters the backyard."))
๐งช Evaluation
NeuroBERT-Tiny was evaluated on a masked language modeling task using 10 IoT-related sentences. The model predicts the top-5 tokens for each masked word, and a test passes if the expected word is in the top-5 predictions.
Sample Results:
Sentence: "She is a [MASK] at the local hospital."
Expected: nurse
Top-5 Predictions: doctor, nurse, surgeon, technician, assistant
Result: ✅ PASS
Sentence: "Turn off the lights after [MASK] minutes."
Expected: five
Top-5 Predictions: ten, two, three, fifteen, twenty
Result: ❌ FAIL
Total Passed: ~7/10 (depends on fine-tuning, slightly lower than NeuroBERT-Mini due to smaller size).
๐ก Use Cases
- Smart Home Devices: Parse commands like “Turn [MASK] the coffee machine” (predicts “on”) or “The fan will turn [MASK]” (predicts “off”).
- IoT Sensors: Interpret sensor contexts, e.g., “The drone collects data using onboard [MASK]” (predicts “sensors”).
- Wearables: Real-time intent detection, e.g., “The music pauses when someone [MASK] the room” (predicts “enters”).
- Mobile Apps: Offline chatbots or semantic search, e.g., “She is a [MASK] at the hospital” (predicts “nurse”).
- Voice Assistants: Local command parsing, e.g., “Please [MASK] the door” (predicts “shut”).
๐ฅ️ Hardware Requirements
- Processors: Low-power CPUs, mobile NPUs, or microcontrollers (e.g., ESP32, Raspberry Pi Zero)
- Storage: ~15MB for model weights (quantized for minimal footprint)
- Memory: ~50MB RAM for inference
- Environment: Offline or low-connectivity settings
๐ Training Data
NeuroBERT-Tiny was trained on a custom IoT dataset focused on IoT terminology, smart home commands, and sensor-related contexts. This enhances performance on tasks like command parsing and device control. Fine-tuning on domain-specific data is highly recommended for optimal results due to the model’s smaller size.
๐ง Fine-Tuning Guide
To adapt NeuroBERT-Tiny for custom IoT tasks (e.g., specific smart home commands):
- Prepare Dataset: Collect labeled data (e.g., commands with intents or masked sentences).
- Fine-Tune with Hugging Face: Use the Transformers library to fine-tune the model on your dataset.
- Deploy: Export the fine-tuned model to ONNX or TensorFlow Lite for edge devices.
⚖️ Comparison to Other Models
Model | Parameters | Size | Edge/IoT Focus | Tasks Supported |
---|---|---|---|---|
NeuroBERT-Tiny | ~5M | ~15MB | Medium | MLM, NER, Classification |
NeuroBERT-Mini | ~10M | ~35MB | High | MLM, NER, Classification, QA |
DistilBERT | ~66M | ~200MB | Moderate | MLM, NER, Classification |
TinyBERT | ~14M | ~50MB | Moderate | MLM, Classification |
NeuroBERT-Tiny is the most compact model, ideal for ultra-low-resource edge devices, but may require more extensive fine-tuning compared to NeuroBERT-Mini.
๐ License
MIT License: Free to use, modify, and distribute for personal and commercial purposes. See LICENSE for details.
๐ Credits
- Base Model: google-bert/bert-base-uncased
- Optimized By: boltuix, quantized for edge AI applications
- Library: Hugging Face Transformers team for model hosting and tools
๐ฌ Support & Community
For issues, questions, or contributions:
- Visit the Hugging Face model page: boltuix/NeuroBERT-Tiny
- Open an issue on the repository
- Join discussions on Hugging Face or contribute via pull requests
- Check the Transformers documentation for guidance
We welcome community feedback to enhance NeuroBERT-Tiny for IoT and edge applications!
❓ FAQ
Q1: What tasks does NeuroBERT-Tiny support?
A1: It supports masked language modeling, text classification, intent detection, and named entity recognition.
Q2: Is NeuroBERT-Tiny suitable for real-time applications?
A2: Yes, it's optimized for ultra-low-latency inference on low-power edge devices.
Q3: Can I fine-tune NeuroBERT-Tiny on my own dataset?
A3: Absolutely! The model is designed for easy fine-tuning using Hugging Face’s Transformers library, though larger datasets may be needed due to its compact size.
Q4: What programming languages and frameworks are supported?
A4: The model works primarily with Python via the Transformers library. For deployment, it can be converted to ONNX, TensorFlow Lite, or CoreML for integration into various platforms (mobile, embedded systems).
Q5: How does NeuroBERT-Tiny handle multi-language input?
A5: It is trained mainly on English datasets. For multilingual support, fine-tuning on other languages or using multilingual variants is recommended.
๐ Next Steps
- Download and try it out: Explore the model at Hugging Face
- Fine-tune on your domain: Customize it with your IoT or edge-related data
- Integrate on edge devices: Convert and deploy with ONNX or TFLite for low-latency offline NLP
- Contribute: Share your enhancements or datasets to improve the model ecosystem
๐ Notes on Fine-Tuning and Dataset Requirements for NeuroBERT-Tiny
-
๐ง Fine-tuning is necessary:
The base NeuroBERT-Tiny model is pretrained on general language tasks but not fine-tuned on specific tasks like NER or classification.
You must fine-tune it on labeled task-specific data to achieve usable results. -
๐ Dataset size and quality:
-
For quick tests, small datasets (tens to hundreds of samples) can work but accuracy will be limited.
-
For decent accuracy, aim for 2,000–5,000 labeled examples (e.g., 2k–5k for NER, 3k+ for classification).
-
Larger, diverse datasets improve model generalization and performance, critical for NeuroBERT-Tiny’s smaller architecture.
-
-
⏳ Training considerations:
-
More epochs (e.g., 10–20) and smaller learning rates help, but overfitting is a risk on small datasets.
-
Use a validation set to monitor progress.
-
Apply early stopping or checkpoints to save the best model.
-
-
๐ ️ Custom datasets:
-
If public datasets don’t fit your needs (e.g., IoT domain), create your own labeled dataset.
-
Quality annotation and clear labeling are crucial, especially for NeuroBERT-Tiny’s limited capacity.
-
-
๐ง Model architecture:
-
NeuroBERT-Tiny is an ultra-compact BERT variant designed for the most constrained edge devices.
-
Requires more data and fine-tuning steps than larger models to achieve comparable accuracy.
-
Highly optimized for speed and size, with trade-offs in maximum accuracy.
-
-
๐ Recommended minimum dataset sizes (approximate):
-
NER: 2,000–5,000 annotated sentences
-
Classification: 3,000–7,000 labeled examples
-
-
๐ฏ Summary:
Fine-tuning on a sufficiently large, high-quality dataset is ๐ to improve NeuroBERT-Tiny’s performance. For domain-specific tasks, custom datasets are recommended. Always validate and use early stopping for best results.
๐ Thank You for Using NeuroBERT-Tiny!
Empowering smarter IoT and edge AI applications with ultra-efficient, context-aware NLP — right where it matters most.
Comments
Post a Comment