<aside> 💡
We’ll start off by creating a virtual environment
python -m venv .venv
Activate the venv
source .venv/bin/activate
Ensure setuptools and wheel are the latest version
pip install -U setuptools wheel
</aside>
Now install TensorFlow
pip install tensorflow
We will be training a model to classify handwritten digits (0-9). I will try to explain the reasoning behind why each layer was chosen.
Start off by importing Tensorflow
import tensorflow as tf
print("TensorFlow version:", tf.__version__)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(128, activation='relu'),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Dense(10)
])
Now, load the dataset
mnist = tf.keras.datasets.mnist
Let’s load the train and test data
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
<aside> 💡
The pixel values of the images range from 0 through 255. We scale these values to a range of 0 to 1 by dividing the values by 255.0. This also converts the sample data from integers to floating-point numbers
</aside>
I will try to explain the reasoning behind each layer
<aside> 💡
Sequential is useful for stacking layers where each layer has one input tensor and one output tensor. Layers are functions with a known mathematical structure that can be reused and have trainable variables. Most TensorFlow models are composed of layers. This model uses the Flatten, Dense, and Dropout layers.
For each example, the model returns a vector of logits or log-odds scores, one for each class.
</aside>