Mnist

jaxmnistexamplesflax

Flax MNIST Example

Open In Colab

Demonstration notebook for https://github.com/google/flax/tree/main/examples/mnist

The Flax Notebook Workflow:

  1. Run the entire notebook end-to-end and check out the outputs.
    • This will open Python files in the right-hand editor!
    • You'll be able to interactively explore metrics in TensorBoard.
  2. Change config and train for different hyperparameters. Check out the updated TensorBoard plots.
  3. Update the code in train.py. Thanks to %autoreload, any changes you make in the file will automatically appear in the notebook. Some ideas to get you started:
    • Change the model.
    • Log some per-batch metrics during training.
    • Add new hyperparameters to configs/default.py and use them in train.py.
  4. At any time, feel free to paste code from train.py into the notebook and modify it directly there!

Setup

[1]

     |███▊                            | 10kB 21.5MB/s eta 0:00:01
     |███████▍                        | 20kB 12.7MB/s eta 0:00:01
     |███████████                     | 30kB 9.4MB/s eta 0:00:01
     |██████████████▉                 | 40kB 8.4MB/s eta 0:00:01
     |██████████████████▌             | 51kB 5.2MB/s eta 0:00:01
     |██████████████████████▏         | 61kB 5.2MB/s eta 0:00:01
     |█████████████████████████▉      | 71kB 5.7MB/s eta 0:00:01
     |█████████████████████████████▋  | 81kB 6.3MB/s eta 0:00:01
     |████████████████████████████████| 92kB 4.4MB/s 
     |████████████████████████████████| 634kB 8.5MB/s 
     |████████████████████████████████| 102kB 6.0MB/s 
     |████████████████████████████████| 61kB 6.3MB/s 
  Building wheel for flax (setup.py) ... done
  Building wheel for jax (setup.py) ... done
[2]
[3]
mount_gdrive
Cloning into 'flaxrepo'...
remote: Enumerating objects: 330, done.
remote: Counting objects: 100% (330/330), done.
remote: Compressing objects: 100% (298/298), done.
remote: Total 330 (delta 58), reused 126 (delta 14), pack-reused 0
Receiving objects: 100% (330/330), 1.81 MiB | 6.59 MiB/s, done.
Resolving deltas: 100% (58/58), done.
<IPython.core.display.Javascript object>
<IPython.core.display.Javascript object>
[4]
/content/examples/mnist

Imports / Helpers

[5]
[6]
[7]

Dataset

[8]
INFO:absl:Load pre-computed DatasetInfo (eg: splits, num examples,...) from GCS: mnist/3.0.1
INFO:absl:Load dataset info from /tmp/tmpqyu1t56xtfds
INFO:absl:Field info.citation from disk and from code do not match. Keeping the one from code.
INFO:absl:Generating dataset mnist (/root/tensorflow_datasets/mnist/3.0.1)
Downloading and preparing dataset mnist/3.0.1 (download: 11.06 MiB, generated: 21.00 MiB, total: 32.06 MiB) to /root/tensorflow_datasets/mnist/3.0.1...
WARNING:absl:Dataset mnist is hosted on GCS. It will automatically be downloaded to your
local data directory. If you'd instead prefer to read directly from our public
GCS bucket (recommended if you're running on GCP), you can instead pass
`try_gcs=True` to `tfds.load` or set `data_dir=gs://tfds-data/datasets`.

HBox(children=(FloatProgress(value=0.0, description='Dl Completed...', max=4.0, style=ProgressStyle(descriptio…
INFO:absl:Load dataset info from /root/tensorflow_datasets/mnist/3.0.1.incompleteJ6TJES
INFO:absl:Field info.citation from disk and from code do not match. Keeping the one from code.
INFO:absl:Constructing tf.data.Dataset for split train, from /root/tensorflow_datasets/mnist/3.0.1


Dataset mnist downloaded and prepared to /root/tensorflow_datasets/mnist/3.0.1. Subsequent calls will reuse this data.
INFO:absl:Constructing tf.data.Dataset for split test, from /root/tensorflow_datasets/mnist/3.0.1
[9]
Output

Training

[ ]
[11]
INFO:absl:Load dataset info from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Field info.citation from disk and from code do not match. Keeping the one from code.
INFO:absl:Reusing dataset mnist (/root/tensorflow_datasets/mnist/3.0.1)
INFO:absl:Constructing tf.data.Dataset for split train, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Constructing tf.data.Dataset for split test, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:train epoch: 1, loss: 0.2352, accuracy: 92.94
INFO:absl:eval epoch: 1, loss: 0.0592, accuracy: 98.00
INFO:absl:train epoch: 2, loss: 0.0584, accuracy: 98.15
INFO:absl:eval epoch: 2, loss: 0.0575, accuracy: 98.14
INFO:absl:train epoch: 3, loss: 0.0423, accuracy: 98.66
INFO:absl:eval epoch: 3, loss: 0.0357, accuracy: 98.78
INFO:absl:Load dataset info from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Field info.citation from disk and from code do not match. Keeping the one from code.
INFO:absl:Reusing dataset mnist (/root/tensorflow_datasets/mnist/3.0.1)
INFO:absl:Constructing tf.data.Dataset for split train, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Constructing tf.data.Dataset for split test, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:train epoch: 1, loss: 0.2745, accuracy: 91.83
INFO:absl:eval epoch: 1, loss: 0.0478, accuracy: 98.36
INFO:absl:train epoch: 2, loss: 0.0508, accuracy: 98.42
INFO:absl:eval epoch: 2, loss: 0.0382, accuracy: 98.81
INFO:absl:train epoch: 3, loss: 0.0374, accuracy: 98.85
INFO:absl:eval epoch: 3, loss: 0.0264, accuracy: 99.09
INFO:absl:Load dataset info from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Field info.citation from disk and from code do not match. Keeping the one from code.
INFO:absl:Reusing dataset mnist (/root/tensorflow_datasets/mnist/3.0.1)
INFO:absl:Constructing tf.data.Dataset for split train, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:Constructing tf.data.Dataset for split test, from /root/tensorflow_datasets/mnist/3.0.1
INFO:absl:train epoch: 1, loss: 0.2676, accuracy: 91.85
INFO:absl:eval epoch: 1, loss: 0.0485, accuracy: 98.57
INFO:absl:train epoch: 2, loss: 0.0483, accuracy: 98.54
INFO:absl:eval epoch: 2, loss: 0.0461, accuracy: 98.64
INFO:absl:train epoch: 3, loss: 0.0341, accuracy: 98.91
INFO:absl:eval epoch: 3, loss: 0.0396, accuracy: 98.74
[12]
upload_data

Inference

[13]
0.0126
[14]
Output