Commit b19f8d2b authored by Nane Kratzke's avatar Nane Kratzke
Browse files

Ray

parent e9b41f73
......@@ -2,7 +2,6 @@
These short tutorials use Keras and Tensorflow to demonstrate neural network-based machine learning:
- The classical MNIST tutorial (`mnist-tutorial.ipynb`)
- The "Zalando" tutorial (`zalando-tutorial.ipynb`)
The tutorials are provided as Jupyter notebook files. Using Jupyter Notebooks you can run Python programs directly in the browser — an interactive way to dive into machine learning.
......
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "3wF5wszaj97Y"
},
"source": [
"# TensorFlow 2 quickstart for beginners"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "04QgGZc9bF5D"
},
"source": [
"This short introduction uses [Keras](https://www.tensorflow.org/guide/keras/overview) to:\n",
"\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.\n",
"\n",
"**To follow this tutorial, run this notebook in a Jupyter Notebook by selecting `Run -> Run all cells` in the menu.**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "0trJmd6DjqBZ",
"tags": []
},
"outputs": [],
"source": [
"import tensorflow as tf"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "7NAbSZiaoJ4z"
},
"source": [
"Load and prepare the [MNIST dataset](http://yann.lecun.com/exdb/mnist/). Convert the samples from integers to floating-point numbers:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "7FP5258xjs-v"
},
"outputs": [],
"source": [
"mnist = tf.keras.datasets.mnist\n",
"\n",
"(x_train, y_train), (x_test, y_test) = mnist.load_data()\n",
"x_train, x_test = x_train / 255.0, x_test / 255.0"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "BPZ68wASog_I"
},
"source": [
"Build the `tf.keras.Sequential` model by stacking layers. Choose an optimizer and loss function for training:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "h3IKyzTCDNGo"
},
"outputs": [],
"source": [
"model = tf.keras.models.Sequential([\n",
" tf.keras.layers.Flatten(input_shape=(28, 28)),\n",
" tf.keras.layers.Dense(128, activation='relu'),\n",
" tf.keras.layers.Dropout(0.2),\n",
" tf.keras.layers.Dense(10)\n",
"])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "l2hiez2eIUz8"
},
"source": [
"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",
"execution_count": null,
"metadata": {
"id": "OeOrNdnkEEcR"
},
"outputs": [],
"source": [
"predictions = model(x_train[:1]).numpy()\n",
"predictions"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "tgjhDQGcIniO"
},
"source": [
"The `tf.nn.softmax` function converts these logits to \"probabilities\" for each class: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "zWSRnQ0WI5eq"
},
"outputs": [],
"source": [
"tf.nn.softmax(predictions).numpy()"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "he5u_okAYS4a"
},
"source": [
"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\n",
"provide an exact and numerically stable loss calculation for all models when using a softmax output. "
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "hQyugpgRIyrA"
},
"source": [
"The `losses.SparseCategoricalCrossentropy` loss takes a vector of logits and a `True` index and returns a scalar loss for each example."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "RSkzdv8MD0tT"
},
"outputs": [],
"source": [
"loss_fn = tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SfR4MsSDU880"
},
"source": [
"This loss is equal to the negative log probability of the true class:\n",
"It is zero if the model is sure of the correct class.\n",
"\n",
"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",
"execution_count": null,
"metadata": {
"id": "NJWqEVrrJ7ZB"
},
"outputs": [],
"source": [
"loss_fn(y_train[:1], predictions).numpy()"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "9foNKHzTD2Vo"
},
"outputs": [],
"source": [
"model.compile(optimizer='adam',\n",
" loss=loss_fn,\n",
" metrics=['accuracy'])"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "ix4mEL65on-w"
},
"source": [
"The `Model.fit` method adjusts the model parameters to minimize the loss: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "y7suUbJXVLqP"
},
"outputs": [],
"source": [
"model.fit(x_train, y_train, epochs=5)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "4mDAAPFqVVgn"
},
"source": [
"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",
"execution_count": null,
"metadata": {
"id": "F7dTAzgHDUh7"
},
"outputs": [],
"source": [
"model.evaluate(x_test, y_test, verbose=2)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T4JfEh7kvx6m"
},
"source": [
"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",
"metadata": {
"id": "Aj8NrlzlJqDG"
},
"source": [
"If you want your model to return a probability, you can wrap the trained model, and attach the softmax to it:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "rYb6DrEH0GMv"
},
"outputs": [],
"source": [
"probability_model = tf.keras.Sequential([\n",
" model,\n",
" tf.keras.layers.Softmax()\n",
"])"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "cnqOZtUp1YR_"
},
"outputs": [],
"source": [
"probability_model(x_test[:5])"
]
}
],
"metadata": {
"colab": {
"collapsed_sections": [
"rX8mhOLljYeM"
],
"name": "beginner.ipynb",
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
This diff is collapsed.
......@@ -656,7 +656,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
......@@ -670,7 +670,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.4"
"version": "3.8.10"
}
},
"nbformat": 4,
......
Markdown is supported
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