File size: 8,434 Bytes
54cd552 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 | """
Phase 2: Downstream Task - Fine-tune for Classification
Demonstrates cell type annotation and other sequence classification tasks.
Usage:
python examples/2_finetune_classification.py
"""
import torch
import numpy as np
from torch.utils.data import Dataset, DataLoader
from transformers import AutoModelForSequenceClassification, Trainer, TrainingArguments
class GeneExpressionDataset(Dataset):
"""
Simple dataset for gene expression classification.
In practice, this would load from h5ad or other single-cell formats.
"""
def __init__(self, input_ids, labels, max_length=2048):
self.input_ids = input_ids
self.labels = labels
self.max_length = max_length
def __len__(self):
return len(self.input_ids)
def __getitem__(self, idx):
input_id = self.input_ids[idx]
label = self.labels[idx]
return {
"input_ids": torch.tensor(input_id, dtype=torch.long),
"labels": torch.tensor(label, dtype=torch.long),
}
def create_mock_data(n_samples=1000, n_features=2048, n_classes=5):
"""Create mock single-cell data for demonstration."""
print("Creating mock dataset...")
# Create random ranked gene sequences
input_ids = np.random.randint(2, 25426, (n_samples, n_features))
# Create random labels (e.g., cell types)
labels = np.random.randint(0, n_classes, n_samples)
# Split into train/val/test
train_size = int(0.7 * n_samples)
val_size = int(0.15 * n_samples)
train_ids = input_ids[:train_size]
train_labels = labels[:train_size]
val_ids = input_ids[train_size:train_size + val_size]
val_labels = labels[train_size:train_size + val_size]
test_ids = input_ids[train_size + val_size:]
test_labels = labels[train_size + val_size:]
print(f"β Dataset created:")
print(f" - Train: {len(train_ids)} samples")
print(f" - Val: {len(val_ids)} samples")
print(f" - Test: {len(test_ids)} samples")
print(f" - Classes: {n_classes}")
return (
GeneExpressionDataset(train_ids, train_labels),
GeneExpressionDataset(val_ids, val_labels),
GeneExpressionDataset(test_ids, test_labels),
)
def main():
print("=" * 80)
print("GeneMamba Phase 2: Downstream Classification")
print("=" * 80)
# ============================================================
# Step 1: Load pretrained model with classification head
# ============================================================
print("\n[Step 1] Loading pretrained model with classification head...")
num_classes = 5
try:
model = AutoModelForSequenceClassification.from_pretrained(
"GeneMamba-24l-512d",
num_labels=num_classes,
trust_remote_code=True,
local_files_only=True,
)
except Exception as e:
print(f"Note: Could not load from hub ({e})")
print("Using local initialization...")
# Initialize locally
from configuration_genemamba import GeneMambaConfig
from modeling_genemamba import GeneMambaForSequenceClassification
config = GeneMambaConfig(
vocab_size=25426,
hidden_size=512,
num_hidden_layers=24,
num_labels=num_classes,
)
model = GeneMambaForSequenceClassification(config)
print(f"β Model loaded")
print(f" - Classification head: input={model.config.hidden_size} β output={num_classes}")
# ============================================================
# Step 2: Prepare data
# ============================================================
print("\n[Step 2] Preparing dataset...")
train_dataset, val_dataset, test_dataset = create_mock_data(
n_samples=1000,
n_features=2048,
n_classes=num_classes,
)
# ============================================================
# Step 3: Set up training arguments
# ============================================================
print("\n[Step 3] Setting up training...")
output_dir = "./classification_results"
training_args = TrainingArguments(
output_dir=output_dir,
num_train_epochs=3,
per_device_train_batch_size=16,
per_device_eval_batch_size=16,
learning_rate=2e-5,
weight_decay=0.01,
warmup_steps=100,
logging_steps=50,
eval_strategy="epoch",
save_strategy="epoch",
load_best_model_at_end=True,
metric_for_best_model="accuracy",
report_to="none", # Disable W&B logging
seed=42,
)
print(f"β Training config:")
print(f" - Output dir: {output_dir}")
print(f" - Epochs: {training_args.num_train_epochs}")
print(f" - Batch size: {training_args.per_device_train_batch_size}")
print(f" - Learning rate: {training_args.learning_rate}")
# ============================================================
# Step 4: Train using Trainer
# ============================================================
print("\n[Step 4] Training model...")
from sklearn.metrics import accuracy_score, f1_score, precision_score, recall_score
def compute_metrics(eval_pred):
"""Compute evaluation metrics."""
predictions, labels = eval_pred
predictions = np.argmax(predictions, axis=1)
return {
"accuracy": accuracy_score(labels, predictions),
"f1": f1_score(labels, predictions, average="weighted", zero_division=0),
"precision": precision_score(labels, predictions, average="weighted", zero_division=0),
"recall": recall_score(labels, predictions, average="weighted", zero_division=0),
}
trainer = Trainer(
model=model,
args=training_args,
train_dataset=train_dataset,
eval_dataset=val_dataset,
compute_metrics=compute_metrics,
)
train_result = trainer.train()
print(f"β Training complete!")
print(f" - Final training loss: {train_result.training_loss:.4f}")
# ============================================================
# Step 5: Evaluate on test set
# ============================================================
print("\n[Step 5] Evaluating on test set...")
test_results = trainer.evaluate(test_dataset)
print(f"β Test Results:")
for metric, value in test_results.items():
if isinstance(value, float):
print(f" - {metric}: {value:.4f}")
# ============================================================
# Step 6: Make predictions
# ============================================================
print("\n[Step 6] Making predictions...")
predictions = trainer.predict(test_dataset)
predicted_classes = np.argmax(predictions.predictions, axis=1)
print(f"β Predictions made:")
print(f" - Predicted classes: {len(predicted_classes)} samples")
print(f" - Class distribution: {np.bincount(predicted_classes)}")
# ============================================================
# Step 7: Save model
# ============================================================
print("\n[Step 7] Saving model...")
save_dir = "./my_genemamba_classifier"
model.save_pretrained(save_dir)
print(f"β Model saved to '{save_dir}'")
# ============================================================
# Step 8: Load and test saved model
# ============================================================
print("\n[Step 8] Testing model reloading...")
loaded_model = AutoModelForSequenceClassification.from_pretrained(
save_dir,
trust_remote_code=True,
)
loaded_model.eval()
# Test on a single batch
with torch.no_grad():
sample_input = torch.randint(2, 25426, (1, 2048))
output = loaded_model(sample_input)
logits = output.logits
prediction = torch.argmax(logits, dim=1)
print(f"β Loaded model test prediction: class {prediction.item()}")
print("\n" + "=" * 80)
print("Phase 2 Complete! Model ready for deployment.")
print("=" * 80)
return model, trainer
if __name__ == "__main__":
model, trainer = main()
|