Build the tf.keras.Sequential model by stacking layers. Choose an optimizer and loss function for training:
```
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)
])
```
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
```
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.
The losses.SparseCategoricalCrossentropy loss takes a vector of logits and a True index and returns a scalar loss for each example.