|
| 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.advection_diffusion import AdvectionDiffusion\n", |
| 32 | + "\n", |
| 33 | + "sim = AdvectionDiffusion(return_timeseries=True, log_level=\"error\")\n", |
| 34 | + "\n", |
| 35 | + "def generate_split(\n", |
| 36 | + " simulator: AdvectionDiffusion, n_train: int = 4, n_valid: int = 2, n_test: int = 2\n", |
| 37 | + "):\n", |
| 38 | + " \"\"\"Generate training, validation, and test splits from the simulator.\"\"\"\n", |
| 39 | + " train = simulator.forward_samples_spatiotemporal(n_train)\n", |
| 40 | + " valid = simulator.forward_samples_spatiotemporal(n_valid)\n", |
| 41 | + " test = simulator.forward_samples_spatiotemporal(n_test)\n", |
| 42 | + " return {\"train\": train, \"valid\": valid, \"test\": test}\n", |
| 43 | + "\n", |
| 44 | + "\n", |
| 45 | + "combined_data = generate_split(sim)" |
| 46 | + ] |
| 47 | + }, |
| 48 | + { |
| 49 | + "cell_type": "markdown", |
| 50 | + "id": "3", |
| 51 | + "metadata": {}, |
| 52 | + "source": [ |
| 53 | + "### Read combined data into datamodule\n" |
| 54 | + ] |
| 55 | + }, |
| 56 | + { |
| 57 | + "cell_type": "code", |
| 58 | + "execution_count": null, |
| 59 | + "id": "4", |
| 60 | + "metadata": {}, |
| 61 | + "outputs": [], |
| 62 | + "source": [ |
| 63 | + "from auto_cast.data.datamodule import SpatioTemporalDataModule\n", |
| 64 | + "\n", |
| 65 | + "datamodule = SpatioTemporalDataModule(\n", |
| 66 | + " data=combined_data, data_path=None, n_steps_input=4, n_steps_output=1, 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 | + "processor = FNOProcessor(\n", |
| 104 | + " in_channels=1, out_channels=1, n_modes=(16, 16, 1), hidden_channels=64\n", |
| 105 | + ")\n", |
| 106 | + "encoder = PermuteConcat(with_constants=False)\n", |
| 107 | + "decoder = ChannelsLast()\n", |
| 108 | + "\n", |
| 109 | + "model = EncoderProcessorDecoder.from_encoder_processor_decoder(\n", |
| 110 | + " encoder_decoder=EncoderDecoder(encoder=encoder, decoder=decoder),\n", |
| 111 | + " processor=processor,\n", |
| 112 | + ")" |
| 113 | + ] |
| 114 | + }, |
| 115 | + { |
| 116 | + "cell_type": "markdown", |
| 117 | + "id": "8", |
| 118 | + "metadata": {}, |
| 119 | + "source": [ |
| 120 | + "### Run trainer\n" |
| 121 | + ] |
| 122 | + }, |
| 123 | + { |
| 124 | + "cell_type": "code", |
| 125 | + "execution_count": null, |
| 126 | + "id": "9", |
| 127 | + "metadata": {}, |
| 128 | + "outputs": [], |
| 129 | + "source": [ |
| 130 | + "import lightning as L\n", |
| 131 | + "\n", |
| 132 | + "device = \"mps\" # \"cpu\"\n", |
| 133 | + "trainer = L.Trainer(max_epochs=5, accelerator=device, log_every_n_steps=10)\n", |
| 134 | + "trainer.fit(model, datamodule.train_dataloader(), datamodule.val_dataloader())" |
| 135 | + ] |
| 136 | + }, |
| 137 | + { |
| 138 | + "cell_type": "markdown", |
| 139 | + "id": "10", |
| 140 | + "metadata": {}, |
| 141 | + "source": [ |
| 142 | + "### Run the evaluation" |
| 143 | + ] |
| 144 | + }, |
| 145 | + { |
| 146 | + "cell_type": "code", |
| 147 | + "execution_count": null, |
| 148 | + "id": "11", |
| 149 | + "metadata": {}, |
| 150 | + "outputs": [], |
| 151 | + "source": [ |
| 152 | + "trainer.test(model, datamodule.test_dataloader())" |
| 153 | + ] |
| 154 | + } |
| 155 | + ], |
| 156 | + "metadata": { |
| 157 | + "kernelspec": { |
| 158 | + "display_name": ".venv", |
| 159 | + "language": "python", |
| 160 | + "name": "python3" |
| 161 | + }, |
| 162 | + "language_info": { |
| 163 | + "codemirror_mode": { |
| 164 | + "name": "ipython", |
| 165 | + "version": 3 |
| 166 | + }, |
| 167 | + "file_extension": ".py", |
| 168 | + "mimetype": "text/x-python", |
| 169 | + "name": "python", |
| 170 | + "nbconvert_exporter": "python", |
| 171 | + "pygments_lexer": "ipython3", |
| 172 | + "version": "3.12.12" |
| 173 | + } |
| 174 | + }, |
| 175 | + "nbformat": 4, |
| 176 | + "nbformat_minor": 5 |
| 177 | +} |
0 commit comments