Tensor Flow
Tensor Flow
5
HCIA-AI TensorFlow Programming Basics
1. Build a neural network that classifies images.
2. Train this neural network.
3. And, finally, evaluate the accuracy of the model.
2. Load and prepare the MNIST dataset. Convert the samples from integers to floating-
point numbers:
mnist = tf.keras.datasets.mnist
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)
])
4. For each example the model returns a vector of "logits" or "log-odds" scores, one
for each class.
predictions = model(x_train[:1]).numpy()
predictions
1
5. The tf.nn.softmax function converts these logits to "probabilities" for each class:
tf.nn.softmax(predictions).numpy()
Note: It is possible to bake this tf.nn.softmax in as the activation function for the last layer of the
network. While this can make the model output more directly interpretable, this approach is discouraged as
it's impossible to provide an exact and numerically stable loss calculation for all models when using a
softmax output.
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
This loss is equal to the negative log probability of the true class: It is zero if the model
is sure of the correct class.
This untrained model gives probabilities close to random (1/10 for each class), so the
initial loss should be close to -tf.log(1/10) ~= 2.3.
loss_fn(y_train[:1], predictions).numpy()
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
The Model.fit method adjusts the model parameters to minimize the loss: