{ "cells": [ { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "view-in-github" }, "source": [ "\"Open" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "rX8mhOLljYeM" }, "source": [ "##### Copyright 2019 The TensorFlow Authors." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "cellView": "form", "colab": {}, "colab_type": "code", "id": "BZSlp3DAjdYf" }, "outputs": [], "source": [ "#@title Licensed under the Apache License, Version 2.0 (the \"License\");\n", "# you may not use this file except in compliance with the License.\n", "# You may obtain a copy of the License at\n", "#\n", "# https://www.apache.org/licenses/LICENSE-2.0\n", "#\n", "# Unless required by applicable law or agreed to in writing, software\n", "# distributed under the License is distributed on an \"AS IS\" BASIS,\n", "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n", "# See the License for the specific language governing permissions and\n", "# limitations under the License." ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "ZIAkIlfmCe1B" }, "source": [ "# The Hello World of Deep Learning with Neural Networks" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "fA93WUy1zzWf" }, "source": [ "Like every first app you should start with something super simple that shows the overall scaffolding for how your code works. \n", "\n", "In the case of creating neural networks, the sample I like to use is one where it learns the relationship between two numbers. So, for example, if you were writing code for a function like this, you already know the 'rules' — \n", "\n", "\n", "```\n", "float hw_function(float x){\n", " float y = (2 * x) - 1;\n", " return y;\n", "}\n", "```\n", "\n", "So how would you train a neural network to do the equivalent task? Using data! By feeding it with a set of Xs, and a set of Ys, it should be able to figure out the relationship between them. \n", "\n", "This is obviously a very different paradigm than what you might be used to, so let's step through it piece by piece.\n" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "DzbtdRcZDO9B" }, "source": [ "## Imports\n", "\n", "Let's start with our imports. Here we are importing TensorFlow and calling it tf for ease of use.\n", "\n", "We then import a library called numpy, which helps us to represent our data as lists easily and quickly.\n", "\n", "The framework for defining a neural network as a set of Sequential layers is called keras, so we import that too." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "X9uIpOS2zx7k" }, "outputs": [], "source": [ "import tensorflow as tf\n", "import numpy as np\n", "from tensorflow import keras" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "wwJGmDrQ0EoB" }, "source": [ "## Define and Compile the Neural Network\n", "\n", "Next we will create the simplest possible neural network. It has 1 layer, and that layer has 1 neuron, and the input shape to it is just 1 value." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "kQFAr_xo0M4T" }, "outputs": [], "source": [ "model = tf.keras.Sequential([keras.layers.Dense(units=1, input_shape=[1])])" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "KhjZjZ-c0Ok9" }, "source": [ "Now we compile our Neural Network. When we do so, we have to specify 2 functions, a loss and an optimizer.\n", "\n", "If you've seen lots of math for machine learning, here's where it's usually used, but in this case it's nicely encapsulated in functions for you. But what happens here — let's explain...\n", "\n", "We know that in our function, the relationship between the numbers is y=2x-1. \n", "\n", "When the computer is trying to 'learn' that, it makes a guess...maybe y=10x+10. The LOSS function measures the guessed answers against the known correct answers and measures how well or how badly it did.\n", "\n", "It then uses the OPTIMIZER function to make another guess. Based on how the loss function went, it will try to minimize the loss. At that point maybe it will come up with somehting like y=5x+5, which, while still pretty bad, is closer to the correct result (i.e. the loss is lower)\n", "\n", "It will repeat this for the number of EPOCHS which you will see shortly. But first, here's how we tell it to use 'MEAN SQUARED ERROR' for the loss and 'STOCHASTIC GRADIENT DESCENT' for the optimizer. You don't need to understand the math for these yet, but you can see that they work! :)\n", "\n", "Over time you will learn the different and appropriate loss and optimizer functions for different scenarios. \n" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "m8YQN1H41L-Y" }, "outputs": [], "source": [ "model.compile(optimizer='sgd', loss='mean_squared_error')" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "5QyOUhFw1OUX" }, "source": [ "## Providing the Data\n", "\n", "Next up we'll feed in some data. In this case we are taking 6 xs and 6ys. You can see that the relationship between these is that y=2x-1, so where x = -1, y=-3 etc. etc. \n", "\n", "A python library called 'Numpy' provides lots of array type data structures that are a defacto standard way of doing it. We declare that we want to use these by specifying the values as an np.array[]" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "4Dxk4q-jzEy4" }, "outputs": [], "source": [ "xs = np.array([-1.0, 0.0, 1.0, 2.0, 3.0, 4.0], dtype=float)\n", "ys = np.array([-3.0, -1.0, 1.0, 3.0, 5.0, 7.0], dtype=float)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "n_YcWRElnM_b" }, "source": [ "# Training the Neural Network" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "c-Jk4dG91dvD" }, "source": [ "The process of training the neural network, where it 'learns' the relationship between the Xs and Ys is in the **model.fit** call. This is where it will go through the loop we spoke about above, making a guess, measuring how good or bad it is (aka the loss), using the opimizer to make another guess etc. It will do it for the number of epochs you specify. When you run this code, you'll see the loss on the right hand side." ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "lpRrl7WK10Pq" }, "outputs": [], "source": [ "model.fit(xs, ys, epochs=500)" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "kaFIr71H2OZ-" }, "source": [ "Ok, now you have a model that has been trained to learn the relationship between X and Y. You can use the **model.predict** method to have it figure out the Y for a previously unknown X. So, for example, if X = 10, what do you think Y will be? Take a guess before you run this code:" ] }, { "cell_type": "code", "execution_count": 0, "metadata": { "colab": {}, "colab_type": "code", "id": "oxNzL4lS2Gui" }, "outputs": [], "source": [ "print(model.predict([10.0]))" ] }, { "cell_type": "markdown", "metadata": { "colab_type": "text", "id": "btF2CSFH2iEX" }, "source": [ "You might have thought 19, right? But it ended up being a little under. Why do you think that is? \n", "\n", "Remember that neural networks deal with probabilities, so given the data that we fed the NN with, it calculated that there is a very high probability that the relationship between X and Y is Y=2X-1, but with only 6 data points we can't know for sure. As a result, the result for 10 is very close to 19, but not necessarily 19. \n", "\n", "As you work with neural networks, you'll see this pattern recurring. You will almost always deal with probabilities, not certainties, and will do a little bit of coding to figure out what the result is based on the probabilities, particularly when it comes to classification.\n" ] } ], "metadata": { "colab": { "include_colab_link": true, "name": "Course 1 - Part 2 - Lesson 2 - Notebook.ipynb", "provenance": [], "toc_visible": true }, "kernelspec": { "display_name": "Python 3", "name": "python3" } }, "nbformat": 4, "nbformat_minor": 0 }