@@ -9,4 +9,4 @@ This short introduction uses Keras to:
- And, finally, evaluate the accuracy of the model.
This is a Jupyter notebook file. Using Jupyter Notebooks you can run Python programs directly in the browser — a interactive way to learn and use TensorFlow. You can run this notebook in your THL JupyterHub account.
Just follow this [link](https://jhub.mylab.th-luebeck.dev/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgit.mylab.th-luebeck.de%2Fkia%2Ftutorial.git&urlpath=lab%2Ftree%2Ftutorial.git%2Fmnist-tutorial.ipynb&branch=master).
Just follow this [link](https://jhub.mylab.th-luebeck.dev/hub/user-redirect/git-pull?repo=https%3A%2F%2Fgit.mylab.th-luebeck.de%2Fkia%2Ftutorial.git&urlpath=lab%2Ftree%2Ftutorial.git%2Fzalando-tutorial.ipynb&branch=master).
Build the `tf.keras.Sequential` model by stacking layers. Choose an optimizer and loss function for training:
%% Cell type:code id: tags:
``` python
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)
])
```
%% Cell type:markdown id: tags:
For each example the model returns a vector of "[logits](https://developers.google.com/machine-learning/glossary#logits)" or "[log-odds](https://developers.google.com/machine-learning/glossary#log-odds)" scores, one for each class.
%% Cell type:code id: tags:
``` python
predictions=model(x_train[:1]).numpy()
predictions
```
%% Cell type:markdown id: tags:
The `tf.nn.softmax` function converts these logits to "probabilities" for each class:
%% Cell type:code id: tags:
``` python
tf.nn.softmax(predictions).numpy()
```
%% Cell type:markdown id: tags:
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.
%% Cell type:markdown id: tags:
The `losses.SparseCategoricalCrossentropy` loss takes a vector of logits and a `True` index and returns a scalar loss for each example.
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.math.log(1/10) ~= 2.3`.
%% Cell type:code id: tags:
``` python
loss_fn(y_train[:1],predictions).numpy()
```
%% Cell type:code id: tags:
``` python
model.compile(optimizer='adam',
loss=loss_fn,
metrics=['accuracy'])
```
%% Cell type:markdown id: tags:
The `Model.fit` method adjusts the model parameters to minimize the loss:
%% Cell type:code id: tags:
``` python
model.fit(x_train,y_train,epochs=5)
```
%% Cell type:markdown id: tags:
The `Model.evaluate` method checks the models performance, usually on a "[Validation-set](https://developers.google.com/machine-learning/glossary#validation-set)" or "[Test-set](https://developers.google.com/machine-learning/glossary#test-set)".
%% Cell type:code id: tags:
``` python
model.evaluate(x_test,y_test,verbose=2)
```
%% Cell type:markdown id: tags:
The image classifier is now trained to ~98% accuracy on this dataset. To learn more, read the [TensorFlow tutorials](https://www.tensorflow.org/tutorials/).
%% Cell type:markdown id: tags:
If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it: