|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "## AutoCast encoder-processor-decoder model API Exploration\n", |
| 9 | + "\n", |
| 10 | + "This notebook aims to explore the end-to-end API.\n" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "1", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "### Example dataaset\n", |
| 19 | + "\n", |
| 20 | + "We use the `AdvectionDiffusion` dataset as an example dataset to illustrate training and evaluation of models. This dataset simulates the advection-diffusion equation in 2D." |
| 21 | + ] |
| 22 | + }, |
| 23 | + { |
| 24 | + "cell_type": "code", |
| 25 | + "execution_count": null, |
| 26 | + "id": "2", |
| 27 | + "metadata": {}, |
| 28 | + "outputs": [], |
| 29 | + "source": [ |
| 30 | + "\n", |
| 31 | + "from autoemulate.simulations.reaction_diffusion import ReactionDiffusion as Sim\n", |
| 32 | + "\n", |
| 33 | + "sim = Sim(return_timeseries=True, log_level=\"error\")\n", |
| 34 | + "\n", |
| 35 | + "def generate_split(simulator: Sim, n_train: int = 1, n_valid: int = 1, n_test: int = 1):\n", |
| 36 | + " \"\"\"Generate training, validation, and test splits from the simulator.\"\"\"\n", |
| 37 | + " train = simulator.forward_samples_spatiotemporal(n_train)\n", |
| 38 | + " valid = simulator.forward_samples_spatiotemporal(n_valid)\n", |
| 39 | + " test = simulator.forward_samples_spatiotemporal(n_test)\n", |
| 40 | + " return {\"train\": train, \"valid\": valid, \"test\": test}\n", |
| 41 | + "\n", |
| 42 | + "\n", |
| 43 | + "combined_data = generate_split(sim)" |
| 44 | + ] |
| 45 | + }, |
| 46 | + { |
| 47 | + "cell_type": "markdown", |
| 48 | + "id": "3", |
| 49 | + "metadata": {}, |
| 50 | + "source": [ |
| 51 | + "### Read combined data into datamodule\n" |
| 52 | + ] |
| 53 | + }, |
| 54 | + { |
| 55 | + "cell_type": "code", |
| 56 | + "execution_count": null, |
| 57 | + "id": "4", |
| 58 | + "metadata": {}, |
| 59 | + "outputs": [], |
| 60 | + "source": [ |
| 61 | + "from auto_cast.data.datamodule import SpatioTemporalDataModule\n", |
| 62 | + "\n", |
| 63 | + "n_steps_input = 4\n", |
| 64 | + "n_steps_output = 1\n", |
| 65 | + "datamodule = SpatioTemporalDataModule(\n", |
| 66 | + " data=combined_data, data_path=None, n_steps_input=n_steps_input, n_steps_output=n_steps_output, batch_size=16\n", |
| 67 | + ")" |
| 68 | + ] |
| 69 | + }, |
| 70 | + { |
| 71 | + "cell_type": "markdown", |
| 72 | + "id": "5", |
| 73 | + "metadata": {}, |
| 74 | + "source": [ |
| 75 | + "### Example batch\n" |
| 76 | + ] |
| 77 | + }, |
| 78 | + { |
| 79 | + "cell_type": "code", |
| 80 | + "execution_count": null, |
| 81 | + "id": "6", |
| 82 | + "metadata": {}, |
| 83 | + "outputs": [], |
| 84 | + "source": [ |
| 85 | + "batch = next(iter(datamodule.train_dataloader()))\n", |
| 86 | + "\n", |
| 87 | + "# batch" |
| 88 | + ] |
| 89 | + }, |
| 90 | + { |
| 91 | + "cell_type": "code", |
| 92 | + "execution_count": null, |
| 93 | + "id": "7", |
| 94 | + "metadata": {}, |
| 95 | + "outputs": [], |
| 96 | + "source": [ |
| 97 | + "from auto_cast.decoders.channels_last import ChannelsLast\n", |
| 98 | + "from auto_cast.encoders.permute_concat import PermuteConcat\n", |
| 99 | + "from auto_cast.models.encoder_decoder import EncoderDecoder\n", |
| 100 | + "from auto_cast.models.encoder_processor_decoder import EncoderProcessorDecoder\n", |
| 101 | + "from auto_cast.nn.fno import FNOProcessor\n", |
| 102 | + "\n", |
| 103 | + "batch = next(iter(datamodule.train_dataloader()))\n", |
| 104 | + "n_channels = batch.input_fields.shape[-1]\n", |
| 105 | + "processor = FNOProcessor(\n", |
| 106 | + " in_channels=n_channels * n_steps_input,\n", |
| 107 | + " out_channels=n_channels * n_steps_output,\n", |
| 108 | + " n_modes=(16, 16),\n", |
| 109 | + " hidden_channels=64,\n", |
| 110 | + ")\n", |
| 111 | + "encoder = PermuteConcat(with_constants=False)\n", |
| 112 | + "decoder = ChannelsLast(output_channels=n_channels, time_steps=n_steps_output)\n", |
| 113 | + "\n", |
| 114 | + "model = EncoderProcessorDecoder.from_encoder_processor_decoder(\n", |
| 115 | + " encoder_decoder=EncoderDecoder(encoder=encoder, decoder=decoder),\n", |
| 116 | + " processor=processor,\n", |
| 117 | + ")" |
| 118 | + ] |
| 119 | + }, |
| 120 | + { |
| 121 | + "cell_type": "code", |
| 122 | + "execution_count": null, |
| 123 | + "id": "8", |
| 124 | + "metadata": {}, |
| 125 | + "outputs": [], |
| 126 | + "source": [ |
| 127 | + "model(batch).shape" |
| 128 | + ] |
| 129 | + }, |
| 130 | + { |
| 131 | + "cell_type": "markdown", |
| 132 | + "id": "9", |
| 133 | + "metadata": {}, |
| 134 | + "source": [ |
| 135 | + "### Run trainer\n" |
| 136 | + ] |
| 137 | + }, |
| 138 | + { |
| 139 | + "cell_type": "code", |
| 140 | + "execution_count": null, |
| 141 | + "id": "10", |
| 142 | + "metadata": {}, |
| 143 | + "outputs": [], |
| 144 | + "source": [ |
| 145 | + "import lightning as L\n", |
| 146 | + "\n", |
| 147 | + "# device = \"mps\" # \"cpu\"\n", |
| 148 | + "device = \"cpu\"\n", |
| 149 | + "trainer = L.Trainer(max_epochs=1, accelerator=device, log_every_n_steps=10)\n", |
| 150 | + "trainer.fit(model, datamodule.train_dataloader(), datamodule.val_dataloader())" |
| 151 | + ] |
| 152 | + }, |
| 153 | + { |
| 154 | + "cell_type": "markdown", |
| 155 | + "id": "11", |
| 156 | + "metadata": {}, |
| 157 | + "source": [ |
| 158 | + "### Run the evaluation" |
| 159 | + ] |
| 160 | + }, |
| 161 | + { |
| 162 | + "cell_type": "code", |
| 163 | + "execution_count": null, |
| 164 | + "id": "12", |
| 165 | + "metadata": {}, |
| 166 | + "outputs": [], |
| 167 | + "source": [ |
| 168 | + "trainer.test(model, datamodule.test_dataloader())" |
| 169 | + ] |
| 170 | + }, |
| 171 | + { |
| 172 | + "cell_type": "markdown", |
| 173 | + "id": "13", |
| 174 | + "metadata": {}, |
| 175 | + "source": [ |
| 176 | + "### Example rollout" |
| 177 | + ] |
| 178 | + }, |
| 179 | + { |
| 180 | + "cell_type": "code", |
| 181 | + "execution_count": null, |
| 182 | + "id": "14", |
| 183 | + "metadata": {}, |
| 184 | + "outputs": [], |
| 185 | + "source": [ |
| 186 | + "# A single element is the full trajectory\n", |
| 187 | + "batch = next(iter(datamodule.rollout_test_dataloader()))" |
| 188 | + ] |
| 189 | + }, |
| 190 | + { |
| 191 | + "cell_type": "code", |
| 192 | + "execution_count": null, |
| 193 | + "id": "15", |
| 194 | + "metadata": {}, |
| 195 | + "outputs": [], |
| 196 | + "source": [ |
| 197 | + "# First n_steps_input are inputs\n", |
| 198 | + "print(batch.input_fields.shape)\n", |
| 199 | + "# Remaining n_steps_output are outputs\n", |
| 200 | + "print(batch.output_fields.shape)" |
| 201 | + ] |
| 202 | + }, |
| 203 | + { |
| 204 | + "cell_type": "code", |
| 205 | + "execution_count": null, |
| 206 | + "id": "16", |
| 207 | + "metadata": {}, |
| 208 | + "outputs": [], |
| 209 | + "source": [ |
| 210 | + "# Run rollout on one trajectory\n", |
| 211 | + "preds, trues = model.rollout(batch)" |
| 212 | + ] |
| 213 | + }, |
| 214 | + { |
| 215 | + "cell_type": "code", |
| 216 | + "execution_count": null, |
| 217 | + "id": "17", |
| 218 | + "metadata": {}, |
| 219 | + "outputs": [], |
| 220 | + "source": [ |
| 221 | + "print(preds.shape)" |
| 222 | + ] |
| 223 | + }, |
| 224 | + { |
| 225 | + "cell_type": "code", |
| 226 | + "execution_count": null, |
| 227 | + "id": "18", |
| 228 | + "metadata": {}, |
| 229 | + "outputs": [], |
| 230 | + "source": [ |
| 231 | + "print(trues.shape)\n" |
| 232 | + ] |
| 233 | + } |
| 234 | + ], |
| 235 | + "metadata": { |
| 236 | + "kernelspec": { |
| 237 | + "display_name": ".venv", |
| 238 | + "language": "python", |
| 239 | + "name": "python3" |
| 240 | + }, |
| 241 | + "language_info": { |
| 242 | + "codemirror_mode": { |
| 243 | + "name": "ipython", |
| 244 | + "version": 3 |
| 245 | + }, |
| 246 | + "file_extension": ".py", |
| 247 | + "mimetype": "text/x-python", |
| 248 | + "name": "python", |
| 249 | + "nbconvert_exporter": "python", |
| 250 | + "pygments_lexer": "ipython3", |
| 251 | + "version": "3.12.12" |
| 252 | + } |
| 253 | + }, |
| 254 | + "nbformat": 4, |
| 255 | + "nbformat_minor": 5 |
| 256 | +} |
0 commit comments