Fine-Tuning Model
Bases: HelicalBaseFineTuningModel
, UCE
Fine-tuning model for the UCE model.
Example
from helical.models.uce import UCEConfig, UCEFineTuningModel
import anndata as ad
# Load the data
ann_data = ad.read_h5ad("dataset.h5ad")
# Get unique output labels
label_set = set(cell_types)
# Create the fine-tuning model with the desired configs
configurer=UCEConfig(batch_size=10)
uce_fine_tune = UCEFineTuningModel(uce_config=configurer, fine_tuning_head="classification", output_size=len(label_set))
# Process the data for training
dataset = uce_fine_tune.process_data(ann_data)
# Get the desired label class
cell_types = list(ann_data.obs.cell_type)
# Create a dictionary mapping the classes to unique integers for training
class_id_dict = dict(zip(label_set, [i for i in range(len(label_set))]))
for i in range(len(cell_types)):
cell_types[i] = class_id_dict[cell_types[i]]
# Fine-tune
uce_fine_tune.train(train_input_data=dataset, train_labels=cell_types)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uce_config
|
UCE
|
The UCE configs for fine-tuning model, the same configs that would be used to instantiate the standard UCE 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 fine_tuning_head is a string specified task. For a classification task this is number of unique classes. |
None
|
Methods:
Name | Description |
---|---|
train |
Fine-tunes the UCE model with different head modules. |
get_outputs |
Get the outputs of the fine-tuned model on a UCE processed dataset. |
Source code in helical/models/uce/fine_tuning_model.py
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 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 |
|
train(train_input_data, train_labels, validation_input_data=None, validation_labels=None, optimizer=optim.AdamW, optimizer_params={'lr': 0.0001}, loss_function=loss.CrossEntropyLoss(), epochs=1, lr_scheduler_params=None)
Fine-tunes the UCE model with different head modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
train_input_data
|
Dataset
|
A helical UCE processed dataset for fine-tuning |
required |
train_labels
|
ndarray
|
The labels for the training data. These should be stored as unique per class integers. |
required |
validation_input_data
|
Dataset
|
A helical UCE processed dataset for per epoch validation. If this is not specified, no validation will be performed. |
None
|
validation_labels
|
ndarray
|
The labels for the validation data. These should be stored as unique per class integers. |
None,
|
optimizer
|
optim
|
The optimizer to be used for training. |
torch.optim.AdamW
|
optimizer_params
|
dict
|
The optimizer parameters to be used for the optimizer specified. This list should NOT include model parameters. e.g. optimizer_params={'lr': 0.0001} |
{'lr': 0.0001}
|
loss_function
|
loss
|
The loss function to be used. |
torch.nn.CrossEntropyLoss()
|
epochs
|
int
|
The number of epochs to train the model |
10
|
lr_scheduler_params
|
dict
|
The learning rate scheduler parameters for the transformers get_scheduler method. The optimizer will be taken from the optimizer input and should not be included in the learning scheduler parameters. If not specified, no scheduler will be used. e.g. lr_scheduler_params={'name': 'linear', 'num_warmup_steps': 0, 'num_training_steps': 5} |
None
|
Source code in helical/models/uce/fine_tuning_model.py
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 |
|
get_outputs(dataset)
Get the outputs of the fine-tuned model on a dataset.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dataset
|
UCEDataset
|
The dataset to get the outputs for. This is the dataset returned from the |
required |
Returns:
Type | Description |
---|---|
ndarray
|
The outputs of the model as a numpy array. |