Spaces:
Build error
Build error
File size: 6,885 Bytes
e487255 |
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 250 251 252 253 254 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"This notebook shows how to use TabPFN for tabular prediction with a scikit learn wrapper.\n",
"\n",
"classifier = TabPFNClassifier(device='cpu')\n",
"classifier.fit(train_xs, train_ys)\n",
"prediction_ = classifier.predict(test_xs)\n",
"\n",
"The fit function does not perform any computations, but only saves the training data. Computations are only done at inference time, when calling predict.\n",
"Note that the presaved models were trained for up to 100 features, 10 classes and 1000 samples. While the model does not have a hard bound on the number of samples, the features and classes are restricted and larger sizes lead to an error."
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"%load_ext autoreload\n",
"\n",
"%autoreload 2"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import time\n",
"import torch\n",
"import numpy as np\n",
"import os\n",
"import random\n",
"\n",
"from model_builder import get_model, get_default_spec, save_model, load_model\n",
"from scripts.transformer_prediction_interface import transformer_predict, get_params_from_config, TabPFNClassifier\n",
"\n",
"from datasets import load_openml_list, open_cc_dids, open_cc_valid_dids\n",
"\n",
"from scripts import tabular_metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"base_path = '.'"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Load datasets"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"jupyter": {
"outputs_hidden": true
},
"tags": []
},
"outputs": [],
"source": [
"max_samples = 10000\n",
"bptt = 10000\n",
"\n",
"cc_test_datasets_multiclass, cc_test_datasets_multiclass_df = load_openml_list(open_cc_dids, multiclass=True, shuffled=True, filter_for_nan=False, max_samples = max_samples, num_feats=100, return_capped=True)\n",
"cc_valid_datasets_multiclass, cc_valid_datasets_multiclass_df = load_openml_list(open_cc_valid_dids, multiclass=True, shuffled=True, filter_for_nan=False, max_samples = max_samples, num_feats=100, return_capped=True)\n",
"\n",
"# Loading longer OpenML Datasets for generalization experiments (optional)\n",
"# test_datasets_multiclass, test_datasets_multiclass_df = load_openml_list(test_dids_classification, multiclass=True, shuffled=True, filter_for_nan=False, max_samples = 10000, num_feats=100, return_capped=True)\n",
"\n",
"random.seed(0)\n",
"random.shuffle(cc_valid_datasets_multiclass)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from datasets import get_openml_classification"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataset = openml.datasets.get_dataset(31)\n",
"X, y, categorical_indicator, attribute_names = dataset.get_data(\n",
" dataset_format=\"array\", target=dataset.default_target_attribute\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def get_datasets(selector, task_type, suite='cc'):\n",
" if task_type == 'binary':\n",
" ds = valid_datasets_binary if selector == 'valid' else test_datasets_binary\n",
" else:\n",
" if suite == 'openml':\n",
" ds = valid_datasets_multiclass if selector == 'valid' else test_datasets_multiclass\n",
" elif suite == 'cc':\n",
" ds = cc_valid_datasets_multiclass if selector == 'valid' else cc_test_datasets_multiclass\n",
" else:\n",
" raise Exception(\"Unknown suite\")\n",
" return ds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"model_string, longer, task_type = '', 1, 'multiclass'\n",
"eval_positions = [1000]\n",
"bptt = 2000\n",
" \n",
"test_datasets, valid_datasets = get_datasets('test', task_type, suite='cc'), get_datasets('valid', task_type, suite='cc')"
]
},
{
"cell_type": "markdown",
"metadata": {
"jp-MarkdownHeadingCollapsed": true,
"tags": []
},
"source": [
"### Select a dataset for prediction"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"[(i, test_datasets[i][0]) for i in range(len(test_datasets))]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"evaluation_dataset_index = 4 # Index of the dataset to predict\n",
"ds = test_datasets[evaluation_dataset_index]\n",
"print(f'Evaluation dataset name: {ds[0]} shape {ds[1].shape}')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"xs, ys = ds[1].clone(), ds[2].clone()\n",
"eval_position = xs.shape[0] // 2\n",
"train_xs, train_ys = xs[0:eval_position], ys[0:eval_position]\n",
"test_xs, test_ys = xs[eval_position:], ys[eval_position:]"
]
},
{
"cell_type": "markdown",
"metadata": {
"tags": []
},
"source": [
"### Predict using a Fitted and Tuned Model"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"classifier = TabPFNClassifier(device='cpu')\n",
"classifier.fit(train_xs, train_ys)\n",
"prediction_ = classifier.predict_proba(test_xs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"roc, ce = tabular_metrics.auc_metric(test_ys, prediction_), tabular_metrics.cross_entropy(test_ys, prediction_)\n",
"'AUC', float(roc), 'Cross Entropy', float(ce)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.13"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
|