Commit 2e30cfd9 authored by Nane Kratzke's avatar Nane Kratzke
Browse files

Zalando Tutorial

parent 7efa56cd
......@@ -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).
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "rX8mhOLljYeM"
},
"source": [
"##### Copyright 2019 The TensorFlow Authors."
]
},
{
"cell_type": "markdown",
"metadata": {
......@@ -28,16 +19,9 @@
"\n",
"1. Build a neural network that classifies images.\n",
"2. Train this neural network.\n",
"3. And, finally, evaluate the accuracy of the model."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hiH7AC-NTniF"
},
"source": [
"To follow this tutorial, run the notebook in a Jupyter Notebook by selecting `Run -> Run all cells` in the menu."
"3. And, finally, evaluate the accuracy of the model.\n",
"\n",
"**To follow this tutorial, run this notebook in a Jupyter Notebook by selecting `Run -> Run all cells` in the menu.**"
]
},
{
......
%% Cell type:markdown id: tags:
##### Copyright 2019 The TensorFlow Authors.
%% Cell type:markdown id: tags:
# TensorFlow 2 quickstart for beginners
%% Cell type:markdown id: tags:
This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:
1. Build a neural network that classifies images.
2. Train this neural network.
3. And, finally, evaluate the accuracy of the model.
%% Cell type:markdown id: tags:
To follow this tutorial, run the notebook in a Jupyter Notebook by selecting `Run -> Run all cells` in the menu.
**To follow this tutorial, run this notebook in a Jupyter Notebook by selecting `Run -> Run all cells` in the menu.**
%% Cell type:code id: tags:
``` python
import tensorflow as tf
```
%% Cell type:markdown id: tags:
Load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). Convert the samples from integers to floating-point numbers:
%% Cell type:code id: tags:
``` python
mnist = tf.keras.datasets.mnist
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train, x_test = x_train / 255.0, x_test / 255.0
```
%% Cell type:markdown id: tags:
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.
%% Cell type:code id: tags:
``` python
loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
```
%% Cell type:markdown id: tags:
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:
%% Cell type:code id: tags:
``` python
probability_model = tf.keras.Sequential([
model,
tf.keras.layers.Softmax()
])
```
%% Cell type:code id: tags:
``` python
probability_model(x_test[:5])
```
......
This diff is collapsed.
This diff is collapsed.
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment