Fine-Tuning Model
Bases: HelicalBaseFineTuningModel
, Geneformer
GeneformerFineTuningModel.
Fine-tuning model for the Geneformer model.
Example
from helical.models.geneformer import GeneformerConfig, GeneformerFineTuningModel
import anndata as ad
# Load the data
ann_data = ad.read_h5ad("/home/matthew/helical-dev/helical/yolksac_human.h5ad")
# Get the column for fine-tuning
cell_types = list(ann_data.obs["cell_types"])
label_set = set(cell_types)
# Create a GeneformerConfig object
geneformer_config = GeneformerConfig(model_name="gf-12L-95M-i4096", batch_size=10)
# Create a GeneformerFineTuningModel object
geneformer_fine_tune = GeneformerFineTuningModel(geneformer_config=geneformer_config, fine_tuning_head="classification", output_size=len(label_set))
# Process the data
dataset = geneformer_fine_tune.process_data(ann_data[:10])
# Add column to the dataset
dataset = dataset.add_column('cell_types', cell_types)
# Create a dictionary to map cell types to ids
class_id_dict = dict(zip(label_set, [i for i in range(len(label_set))]))
def classes_to_ids(example):
example["cell_types"] = class_id_dict[example["cell_types"]]
return example
# Convert cell types to ids
dataset = dataset.map(classes_to_ids, num_proc=1)
# Fine-tune the model
geneformer_fine_tune.train(train_dataset=dataset, label="cell_types")
# Get logits from the fine-tuned model
outputs = geneformer_fine_tune.get_outputs(dataset)
print(outputs[:10])
# Get embeddings from the fine-tuned model
embeddings = geneformer_fine_tune.get_embeddings(dataset)
print(embeddings[:10])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
geneformer_config
|
GeneformerConfig
|
The Geneformer configs to fine-tune, the same as instantiating the standard Geneformer model. |
required |
fine_tuning_head
|
Literal['classification', 'regression'] | HelicalBaseFineTuningHead
|
The fine-tuning head that is appended to the model. This can either be a string (options available: "classification", "regression") specifying the task or a custom fine-tuning head inheriting from HelicalBaseFineTuningHead. |
required |
output_size
|
Optional[int]
|
The output size of the fine-tuning model. This is required if the |
None
|
Methods:
Name | Description |
---|---|
train |
Fine-tunes the Geneformer model. |
get_outputs |
Get outputs from the fine-tuned model on the given processed dataset. |
Source code in helical/models/geneformer/fine_tuning_model.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
|
train(train_dataset, optimizer=optim.AdamW, optimizer_params={'lr': 0.0001}, loss_function=loss.CrossEntropyLoss(), label='cell_types', epochs=1, freeze_layers=2, validation_dataset=None, lr_scheduler_params=None, silent=False)
Fine-tunes the Geneformer model for classification tasks.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_dataset
|
Dataset
|
A helical-processed dataset for fine-tuning. |
required |
optimizer
|
Optimizer
|
The optimizer to be used for training. |
torch.optim.AdamW
|
optimizer_params
|
dict
|
Parameters to be passed to the specified optimizer. This dictionary should NOT include model parameters. Example: optimizer_params = {'lr': 0.0001} |
{'lr': 0.0001}
|
loss_function
|
loss
|
The loss function to be used for training. |
torch.nn.CrossEntropyLoss()
|
label
|
str
|
The column in the dataset containing the training labels. Labels should be stored as unique integers per class. |
"cell_types"
|
epochs
|
int
|
The number of epochs to train the model. |
10
|
freeze_layers
|
int
|
The number of layers to freeze during training. |
2
|
validation_dataset
|
Dataset
|
A helical-processed dataset used for per-epoch validation. If not specified, no validation will be performed. |
None
|
lr_scheduler_params
|
dict
|
Parameters for the learning rate scheduler from the Transformers |
None
|
Source code in helical/models/geneformer/fine_tuning_model.py
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 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 |
|
get_outputs(dataset, silent=False)
Predicts the labels for a dataset using the fine-tuned model.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
Dataset
|
The processed dataset to generate outputs for. |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The predicted labels in the form of a numpy array |