{ "cells": [ { "cell_type": "markdown", "id": "300f96f9", "metadata": {}, "source": [ "# Pretrain and Create Model for Classification Based Tasks" ] }, { "cell_type": "code", "execution_count": 1, "id": "b2580ba1", "metadata": {}, "outputs": [], "source": [ "from stFormer.classifier.Classifier import Classifier" ] }, { "cell_type": "markdown", "id": "f0b1ec1f", "metadata": {}, "source": [ "## 1.1 Classify From Pretrained Model" ] }, { "cell_type": "markdown", "id": "31d4f877", "metadata": {}, "source": [ "We take out Subtype based information to evaluate classification fine-tuning and evaluation\n", "\n", "1. **Data Loading & Splitting** \n", " - Load `train_ds` from `dataset_path`. \n", " - If `eval_dataset_path` provided, load `eval_ds`; \n", " otherwise do a `train_test_split(test_size, seed=42)`.\n", "\n", "2. **`model_init` Function** \n", " - Loads base model & config from `model_checkpoint`. \n", " - Overrides `num_labels` to match `self.label_mapping`. \n", " - Optionally freezes the first `self.freeze_layers` encoder layers.\n", " - Adds a classification head onto BERT pretreained model if loading from masked learning objective\n", "\n", "3. **Tokenizer & Data Collator** \n", " - `AutoTokenizer.from_pretrained(...)` with `padding=\"max_length\"` \n", " - `DataCollatorWithPadding` to pad to `tokenizer.model_max_length`.\n", "\n", "4. **Classification**\n", " - `Evaluation metrics` compute metrics to determine training/test loss and accuracy\n", " - `training args` takes dictionary of BERT training arguments for hyperparameter selection and model updating\n", "\n", "5. **Best Checkpoint Selection and Saving**\n", " - Saves model checkpoints to output directory based upon ``eval strategy` \n", " - Returns final `trainer` model and saves final model to `output_directory`\n" ] }, { "cell_type": "code", "execution_count": null, "id": "1508f6cd", "metadata": {}, "outputs": [], "source": [ "classifier = Classifier(\n", " metadata_column = 'Tissue',\n", " mode='spot',\n", " classifier_type = 'sequence', #for class predictions\n", " token_dictionary_file='output/spot/token_dictionary.pickle',\n", " rare_threshold=0.1, #remove rare data types (less than 10% of samples)\n", " max_examples_per_class=10000, #option to downsample\n", " nproc=24,\n", ")\n", "ds_path, map_path = classifier.prepare_data(\n", " input_data = 'annotated.dataset/',\n", " output_directory = 'tmp/clasifier',\n", " output_prefix = 'Tissue_Classifier',\n", " )" ] }, { "cell_type": "markdown", "id": "9e4c9e22", "metadata": {}, "source": [ "In this example we utilize the model that was trained with a masked learning objective. While this is definitely possible, we suggest utilizing another Bert model that was trained using a classification task and then fine-tune on specific task" ] }, { "cell_type": "code", "execution_count": null, "id": "07976680", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Using model checkpoint: output/spot/spot_model\n", "Number of labels from data: 3\n", "Label mapping: {'Brain': 0, 'Breast': 1, 'Skin': 2}\n", "Linear(in_features=256, out_features=3, bias=True)\n", "Max label: tensor(2)\n", "Label mapping size: 3\n" ] }, { "data": { "text/html": [ "\n", "
| Step | \n", "Training Loss | \n", "Validation Loss | \n", "Accuracy | \n", "Precision | \n", "Recall | \n", "F1 | \n", "
|---|---|---|---|---|---|---|
| 100 | \n", "1.083700 | \n", "1.001198 | \n", "0.539333 | \n", "0.416546 | \n", "0.536904 | \n", "0.438580 | \n", "
| 200 | \n", "0.786700 | \n", "0.569715 | \n", "0.796833 | \n", "0.823148 | \n", "0.796179 | \n", "0.780779 | \n", "
| 300 | \n", "0.689500 | \n", "0.648199 | \n", "0.687167 | \n", "0.729824 | \n", "0.686503 | \n", "0.641001 | \n", "
| 400 | \n", "0.586700 | \n", "0.423288 | \n", "0.868833 | \n", "0.878322 | \n", "0.868952 | \n", "0.868393 | \n", "
| 500 | \n", "0.490300 | \n", "0.976297 | \n", "0.581667 | \n", "0.611228 | \n", "0.579323 | \n", "0.547443 | \n", "
| 600 | \n", "0.505300 | \n", "0.524849 | \n", "0.801667 | \n", "0.823287 | \n", "0.800729 | \n", "0.798036 | \n", "
| 700 | \n", "0.365200 | \n", "0.249833 | \n", "0.940833 | \n", "0.942768 | \n", "0.940911 | \n", "0.940833 | \n", "
| 800 | \n", "0.330000 | \n", "0.908484 | \n", "0.667667 | \n", "0.694001 | \n", "0.667420 | \n", "0.620839 | \n", "
| 900 | \n", "0.291800 | \n", "0.830796 | \n", "0.706167 | \n", "0.740024 | \n", "0.704866 | \n", "0.680967 | \n", "
| 1000 | \n", "0.248000 | \n", "0.150817 | \n", "0.967167 | \n", "0.967391 | \n", "0.967184 | \n", "0.967184 | \n", "
| 1100 | \n", "0.185900 | \n", "0.732362 | \n", "0.754667 | \n", "0.783978 | \n", "0.754984 | \n", "0.746594 | \n", "
| 1200 | \n", "0.193700 | \n", "0.144109 | \n", "0.968833 | \n", "0.969112 | \n", "0.968875 | \n", "0.968854 | \n", "
| 1300 | \n", "0.165200 | \n", "0.136830 | \n", "0.967167 | \n", "0.967243 | \n", "0.967176 | \n", "0.967176 | \n", "
| 1400 | \n", "0.138800 | \n", "0.169318 | \n", "0.958500 | \n", "0.959216 | \n", "0.958587 | \n", "0.958527 | \n", "
| 1500 | \n", "0.161700 | \n", "0.140191 | \n", "0.969833 | \n", "0.970116 | \n", "0.969880 | \n", "0.969856 | \n", "
"
],
"text/plain": [
"