Solution: Optimizers
Let’s review the solution.
Loading dataset
We use the keras.dataset library to load and visualize the Fashion MNIST dataset.
from tensorflow import kerasfrom sklearn.model_selection import train_test_splitfrom jax import numpy as jnpimport numpy as npfrom matplotlib import pyplot(X_train, Y_train), (X_test, Y_test) = keras.datasets.fashion_mnist.load_data()X = np.concatenate((X_train, X_test))Y = np.concatenate((Y_train, Y_test))train_size = 0.8X_train, X_test, Y_train, Y_test = train_test_split(X, Y, train_size=0.04, test_size=0.01, random_state=2019)for i in range(9):pyplot.subplot(330 + 1 + i)pyplot.imshow(X_train[i])pyplot.savefig("output/image.png")X_train, X_test, Y_train, Y_test = jnp.array(X_train, dtype=jnp.float32),\jnp.array(X_test, dtype=jnp.float32),\jnp.array(Y_train, dtype=jnp.float32),\jnp.array(Y_test, dtype=jnp.float32)X_train, X_test = X_train.reshape(-1,28,28,1), X_test.reshape(-1,28,28,1)X_train, X_test = X_train/255.0, X_test/255.0classes = jnp.unique(Y_train)print('X_train', X_train.shape, 'X_test',X_test.shape,)print('Y_train',Y_train.shape, 'Y_test',Y_test.shape)
In the code above:
Lines 1–5: We import the
keraslibrary from TensorFlow to load the dataset, thetrain_test_splitmethod fromsklearn.mode_selectionto split the dataset, and the JAX version of NumPy to perform numerical operations. Also, we import thenumpyandmatplotliblibraries for visualization.Lines 7–9: We load the Fashion MNIST dataset and combine the training and test datasets.
Lines 11–12: We define the
train_sizeas0.8and split the train and test dataset.Lines 14–16: We use the
forloop to create a plot of9images to display in the output.Line 17: We save the image in the
outputfolder to show in the output of the playground.Lines 19–23: We convert the train and test data to the JAX arrays.
Line 24: We reshape the dataset with the ...