First we install the Python modules we will need:
! pip install comet_ml tensorflow numpy --upgrade --quiet
And import them:
(import-as "tensorflow" "tf")
(import-as "numpy" "np")
(import "comet_ml")
(import "PIL.Image")
(import "base64")
(import "IPython")
(IPython)
To use Comet to track your experiments, copy your API key from: https://www.comet.com/account-settings/apiKeys
and paste it below between the quotes. (Otherwise, skip this cell).
(import "os")
(set-item! os.environ "COMET_API_KEY" "<PASTE-COMET-API-KEY-HERE>")
Next, we get the MNIST dataset:
(define mnist tf.keras.datasets.mnist)
(define dataset (mnist.load_data))
(define x_train (get-item (get-item dataset 0) 0))
(define y_train (get-item (get-item dataset 0) 1))
(define x_test (get-item (get-item dataset 1) 0))
(define y_test (get-item (get-item dataset 1) 1))
Hack to allow Python 3.9 to work:
(set-attr! base64 "encodestring" base64.encodebytes)
Let's take a look at an input matrix:
(PIL.Image.fromarray (get-item x_train 0))
What is the target for the above?
(get-item y_train 0)
5
For this network, we'll scale the inputs to be between 0 and 1:
(set! x_train (/ x_train 255.0))
(set! x_test (/ x_test 255.0))
(define loss_fn (tf.keras.losses.SparseCategoricalCrossentropy (dict '((from_logits : #t)))))
Ok, let's train a model!
(let* ((optimizer (choose "adam" "rmsprop" "sgd"))
(dropout_rate (choose 0.0 0.1 0.2 0.4))
(activation (choose "relu" "sigmoid"))
(hidden_layer_size (choose 10 20 30))
(options (dict `((optimizer : ,optimizer)(loss : ,loss_fn)(metrics : ,(vector "accuracy")))))
(epochs 5)
(experiment (comet_ml.Experiment (dict '((project_name : "calysto-scheme")))))
(model (tf.keras.models.Sequential
(vector
(tf.keras.layers.Flatten (dict '((input_shape : (28 28)))))
(tf.keras.layers.Dense hidden_layer_size (dict `((activation : ,activation))))
(tf.keras.layers.Dropout dropout_rate)
(tf.keras.layers.Dense 10)
))))
(print experiment.url)
(model.compile options)
(model.summary)
(experiment.log_parameters (dict `((optimizer : ,optimizer)
(dropout_rate : ,dropout_rate)
(activation : ,activation)
(hidden_layer_size : ,hidden_layer_size)
(epochs : ,epochs)
)) (dict))
(experiment.set_model_graph model)
(let ((history (model.fit x_train y_train (dict `((epochs : ,epochs)))))
(step 0))
(map (lambda (key)
(set! step 0)
(map (lambda (v) (experiment.log_metric key v step) (set! step (+ step 1)))
(get-item history.history key)))
history.history))
(experiment.end)
)
"https://www.comet.com/dsblank/calysto-scheme/5ab05a53032f4438a5aa27c97f461bbf" Model: "sequential_4" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten_4 (Flatten) (None, 784) 0 dense_8 (Dense) (None, 10) 7850 dropout_4 (Dropout) (None, 10) 0 dense_9 (Dense) (None, 10) 110 ================================================================= Total params: 7,960 Trainable params: 7,960 Non-trainable params: 0 _________________________________________________________________ Epoch 1/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.5386 - accuracy: 0.8441 Epoch 2/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.2936 - accuracy: 0.9161 Epoch 3/5 1875/1875 [==============================] - 2s 1ms/step - loss: 0.2651 - accuracy: 0.9243 Epoch 4/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2498 - accuracy: 0.9290 Epoch 5/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.2391 - accuracy: 0.9322
Nice! Now let's do it again, picking different hyperparameters:
(choose)
"https://www.comet.com/dsblank/calysto-scheme/dc1f0e7f33844b89827218c9d099d78d" Model: "sequential_5" _________________________________________________________________ Layer (type) Output Shape Param # ================================================================= flatten_5 (Flatten) (None, 784) 0 dense_10 (Dense) (None, 20) 15700 dropout_5 (Dropout) (None, 20) 0 dense_11 (Dense) (None, 10) 210 ================================================================= Total params: 15,910 Trainable params: 15,910 Non-trainable params: 0 _________________________________________________________________ Epoch 1/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.3972 - accuracy: 0.8883 Epoch 2/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.2279 - accuracy: 0.9354 Epoch 3/5 1875/1875 [==============================] - 3s 2ms/step - loss: 0.1949 - accuracy: 0.9438 Epoch 4/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.1764 - accuracy: 0.9487 Epoch 5/5 1875/1875 [==============================] - 3s 1ms/step - loss: 0.1609 - accuracy: 0.9540
Continue until there are no more options.