diff --git a/09_up_and_running_with_tensorflow.ipynb b/09_up_and_running_with_tensorflow.ipynb index 7cfc20b..f009bab 100644 --- a/09_up_and_running_with_tensorflow.ipynb +++ b/09_up_and_running_with_tensorflow.ipynb @@ -55,11 +55,13 @@ "\n", "# Common imports\n", "import numpy as np\n", - "import numpy.random as rnd\n", "import os\n", "\n", "# to make this notebook's output stable across runs\n", - "rnd.seed(42)\n", + "def reset_graph(seed=42):\n", + " tf.reset_default_graph()\n", + " tf.set_random_seed(42)\n", + " np.random.seed(42)\n", "\n", "# To plot pretty figures\n", "%matplotlib inline\n", @@ -103,7 +105,7 @@ "source": [ "import tensorflow as tf\n", "\n", - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "x = tf.Variable(3, name=\"x\")\n", "y = tf.Variable(4, name=\"y\")\n", @@ -114,7 +116,9 @@ "cell_type": "code", "execution_count": 3, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -142,7 +146,9 @@ "cell_type": "code", "execution_count": 5, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -169,7 +175,9 @@ "cell_type": "code", "execution_count": 7, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -197,7 +205,9 @@ "cell_type": "code", "execution_count": 9, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -208,7 +218,9 @@ "cell_type": "code", "execution_count": 10, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -235,7 +247,9 @@ "cell_type": "code", "execution_count": 12, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -246,7 +260,9 @@ "cell_type": "code", "execution_count": 13, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -273,7 +289,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "x1 = tf.Variable(1)\n", "x1.graph is tf.get_default_graph()" @@ -379,7 +395,7 @@ "import numpy as np\n", "from sklearn.datasets import fetch_california_housing\n", "\n", - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "housing = fetch_california_housing()\n", "m, n = housing.data.shape\n", @@ -398,7 +414,9 @@ "cell_type": "code", "execution_count": 20, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -531,7 +549,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_epochs = 1000\n", "learning_rate = 0.01\n", @@ -562,7 +580,9 @@ "cell_type": "code", "execution_count": 26, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -581,7 +601,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Same as above except for the `gradients = ...` line:" ] @@ -596,7 +619,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_epochs = 1000\n", "learning_rate = 0.01\n", @@ -613,7 +636,9 @@ "cell_type": "code", "execution_count": 28, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -624,7 +649,9 @@ "cell_type": "code", "execution_count": 29, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -648,7 +675,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "How could you find the partial derivatives of the following function with regards to `a` and `b`?" ] @@ -657,7 +687,9 @@ "cell_type": "code", "execution_count": 30, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -672,7 +704,9 @@ "cell_type": "code", "execution_count": 31, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -683,11 +717,13 @@ "cell_type": "code", "execution_count": 32, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "a = tf.Variable(0.2, name=\"a\")\n", "b = tf.Variable(0.3, name=\"b\")\n", @@ -701,7 +737,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's compute the function at $a=0.2$ and $b=0.3$, and the partial derivatives at that point with regards to $a$ and with regards to $b$:" ] @@ -710,7 +749,9 @@ "cell_type": "code", "execution_count": 33, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -740,7 +781,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_epochs = 1000\n", "learning_rate = 0.01\n", @@ -757,7 +798,9 @@ "cell_type": "code", "execution_count": 35, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -769,7 +812,9 @@ "cell_type": "code", "execution_count": 36, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -809,7 +854,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_epochs = 1000\n", "learning_rate = 0.01\n", @@ -826,7 +871,9 @@ "cell_type": "code", "execution_count": 38, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -838,7 +885,9 @@ "cell_type": "code", "execution_count": 39, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -899,7 +948,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "A = tf.placeholder(tf.float32, shape=(None, 3))\n", "B = A + 5\n", @@ -914,7 +963,9 @@ "cell_type": "code", "execution_count": 42, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -941,8 +992,6 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", - "\n", "n_epochs = 1000\n", "learning_rate = 0.01" ] @@ -951,10 +1000,14 @@ "cell_type": "code", "execution_count": 44, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ + "reset_graph()\n", + "\n", "X = tf.placeholder(tf.float32, shape=(None, n + 1), name=\"X\")\n", "y = tf.placeholder(tf.float32, shape=(None, 1), name=\"y\")" ] @@ -963,7 +1016,9 @@ "cell_type": "code", "execution_count": 45, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -981,7 +1036,9 @@ "cell_type": "code", "execution_count": 46, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -992,7 +1049,9 @@ "cell_type": "code", "execution_count": 47, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1004,15 +1063,17 @@ "cell_type": "code", "execution_count": 48, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ "def fetch_batch(epoch, batch_index, batch_size):\n", - " rnd.seed(epoch * n_batches + batch_index) # not shown in the book\n", - " indices = rnd.randint(m, size=batch_size) # not shown\n", - " X_batch = scaled_housing_data_plus_bias[indices] # not shown\n", - " y_batch = housing.target.reshape(-1, 1)[indices] # not shown\n", + " np.random.seed(epoch * n_batches + batch_index) # not shown in the book\n", + " indices = np.random.randint(m, size=batch_size) # not shown\n", + " X_batch = scaled_housing_data_plus_bias[indices] # not shown\n", + " y_batch = housing.target.reshape(-1, 1)[indices] # not shown\n", " return X_batch, y_batch\n", "\n", "with tf.Session() as sess:\n", @@ -1030,7 +1091,9 @@ "cell_type": "code", "execution_count": 49, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1057,7 +1120,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_epochs = 1000 # not shown in the book\n", "learning_rate = 0.01 # not shown\n", @@ -1104,7 +1167,9 @@ "cell_type": "code", "execution_count": 52, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1117,7 +1182,9 @@ "cell_type": "code", "execution_count": 53, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1126,7 +1193,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "If you want to have a saver that loads and restores `theta` with a different name, such as `\"weights\"`:" ] @@ -1135,7 +1205,9 @@ "cell_type": "code", "execution_count": 54, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1144,7 +1216,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "By default the saver also saves the graph structure itself in a second file with the extension `.meta`. You can use the function `tf.train.import_meta_graph()` to restore the graph structure. This function loads the graph into the default graph and returns a `Saver` that can then be used to restore the graph state (i.e., the variable values):" ] @@ -1153,11 +1228,14 @@ "cell_type": "code", "execution_count": 55, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph() # notice that we start with an empty graph.\n", + "reset_graph()\n", + "# notice that we start with an empty graph.\n", "\n", "saver = tf.train.import_meta_graph(\"/tmp/my_model_final.ckpt.meta\") # this loads the graph structure\n", "theta = tf.get_default_graph().get_tensor_by_name(\"theta:0\") # not shown in the book\n", @@ -1171,7 +1249,9 @@ "cell_type": "code", "execution_count": 56, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1180,7 +1260,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "This means that you can import a pretrained model without having to have the corresponding Python code to build the graph. This is very handy when you keep tweaking and saving your model: you can load a previously saved model without having to search for the version of the code that built it." ] @@ -1278,7 +1361,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "from datetime import datetime\n", "\n", @@ -1291,7 +1374,9 @@ "cell_type": "code", "execution_count": 60, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1314,7 +1399,9 @@ "cell_type": "code", "execution_count": 61, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1326,7 +1413,9 @@ "cell_type": "code", "execution_count": 62, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1364,7 +1453,9 @@ "cell_type": "code", "execution_count": 64, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1375,7 +1466,9 @@ "cell_type": "code", "execution_count": 65, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1402,7 +1495,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "now = datetime.utcnow().strftime(\"%Y%m%d%H%M%S\")\n", "root_logdir = \"tf_logs\"\n", @@ -1421,7 +1514,9 @@ "cell_type": "code", "execution_count": 67, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1434,7 +1529,9 @@ "cell_type": "code", "execution_count": 68, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1517,7 +1614,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "a1 = tf.Variable(0, name=\"a\") # name == \"a\"\n", "a2 = tf.Variable(0, name=\"a\") # name == \"a_1\"\n", @@ -1562,7 +1659,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "n_features = 3\n", "X = tf.placeholder(tf.float32, shape=(None, n_features), name=\"X\")\n", @@ -1601,7 +1698,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " w_shape = (int(X.get_shape()[1]), 1)\n", @@ -1620,7 +1717,9 @@ "cell_type": "code", "execution_count": 75, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1647,7 +1746,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " with tf.name_scope(\"relu\"):\n", @@ -1662,7 +1761,9 @@ "cell_type": "code", "execution_count": 77, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1677,7 +1778,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## Sharing Variables" ] @@ -1702,7 +1806,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X, threshold):\n", " with tf.name_scope(\"relu\"):\n", @@ -1728,7 +1832,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " with tf.name_scope(\"relu\"):\n", @@ -1745,7 +1849,9 @@ "cell_type": "code", "execution_count": 80, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1758,11 +1864,13 @@ "cell_type": "code", "execution_count": 81, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "with tf.variable_scope(\"relu\"):\n", " threshold = tf.get_variable(\"threshold\", shape=(),\n", @@ -1773,7 +1881,9 @@ "cell_type": "code", "execution_count": 82, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1785,7 +1895,9 @@ "cell_type": "code", "execution_count": 83, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1804,7 +1916,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " with tf.variable_scope(\"relu\", reuse=True):\n", @@ -1827,7 +1939,9 @@ "cell_type": "code", "execution_count": 85, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1845,7 +1959,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " with tf.variable_scope(\"relu\"):\n", @@ -1871,11 +1985,13 @@ "cell_type": "code", "execution_count": 87, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "def relu(X):\n", " threshold = tf.get_variable(\"threshold\", shape=(),\n", @@ -1898,7 +2014,9 @@ "cell_type": "code", "execution_count": 88, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -1926,7 +2044,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "with tf.variable_scope(\"my_scope\"):\n", " x0 = tf.get_variable(\"x\", shape=(), initializer=tf.constant_initializer(0.))\n", @@ -1951,7 +2069,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "The first `variable_scope()` block first creates the shared variable `x0`, named `my_scope/x`. For all operations other than shared variables (including non-shared variables), the variable scope acts like a regular name scope, which is why the two variables `x1` and `x2` have a name with a prefix `my_scope/`. Note however that TensorFlow makes their names unique by adding an index: `my_scope/x_1` and `my_scope/x_2`.\n", "\n", @@ -1980,7 +2101,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "text = np.array(\"Do you want some café?\".split())\n", "text_tensor = tf.constant(text)\n", @@ -2163,7 +2284,9 @@ "cell_type": "code", "execution_count": 95, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2198,7 +2321,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "$3 + (3 + 4 \\epsilon) = 6 + 4\\epsilon$" ] @@ -2207,7 +2333,9 @@ "cell_type": "code", "execution_count": 96, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2216,7 +2344,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "$(3 + 4ε)\\times(5 + 7ε) = 3 \\times 5 + 3 \\times 7ε + 4ε \\times 5 + 4ε \\times 7ε = 15 + 21ε + 20ε + 28ε^2 = 15 + 41ε + 28 \\times 0 = 15 + 41ε$" ] @@ -2225,7 +2356,9 @@ "cell_type": "code", "execution_count": 97, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2236,7 +2369,9 @@ "cell_type": "code", "execution_count": 98, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2250,7 +2385,9 @@ "cell_type": "code", "execution_count": 99, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2269,7 +2406,9 @@ "cell_type": "code", "execution_count": 100, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2280,7 +2419,9 @@ "cell_type": "code", "execution_count": 101, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2301,7 +2442,9 @@ "cell_type": "code", "execution_count": 102, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2385,7 +2528,7 @@ }, "outputs": [], "source": [ - "tf.reset_default_graph()\n", + "reset_graph()\n", "\n", "x = tf.Variable(3., name=\"x\")\n", "y = tf.Variable(4., name=\"y\")\n", @@ -2435,14 +2578,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "## 12. Logistic Regression with Mini-Batch Gradient Descent using TensorFlow" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "First, let's create the moons dataset using Scikit-Learn's `make_moons()` function:" ] @@ -2451,19 +2600,24 @@ "cell_type": "code", "execution_count": 104, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ "from sklearn.datasets import make_moons\n", "\n", "m = 1000\n", - "X_moons, y_moons = make_moons(m, noise=0.1)" + "X_moons, y_moons = make_moons(m, noise=0.1, random_state=42)" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's take a peek at the dataset:" ] @@ -2472,7 +2626,9 @@ "cell_type": "code", "execution_count": 105, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2484,7 +2640,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "We must not forget to add an extra bias feature ($x_0 = 1$) to every instance. For this, we just need to add a column full of 1s on the left of the input matrix $\\mathbf{X}$:" ] @@ -2493,7 +2652,9 @@ "cell_type": "code", "execution_count": 106, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2502,7 +2663,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's check:" ] @@ -2511,7 +2675,9 @@ "cell_type": "code", "execution_count": 107, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2520,7 +2686,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Looks good. Now let's reshape `y_train` to make it a column vector (i.e. a 2D array with a single column):" ] @@ -2529,7 +2698,9 @@ "cell_type": "code", "execution_count": 108, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2538,7 +2709,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now let's split the data into a training set and a test set:" ] @@ -2547,7 +2721,9 @@ "cell_type": "code", "execution_count": 109, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2561,7 +2737,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Ok, now let's create a small function to generate training batches. In this implementation we will just pick random instances from the training set for each batch. This means that a single batch may contain the same instance multiple times, and also a single epoch may not cover all the training instances (in fact it will generally cover only about two thirds of the instances). However, in practice this is not an issue and it simplifies the code:" ] @@ -2570,7 +2749,9 @@ "cell_type": "code", "execution_count": 110, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2583,7 +2764,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's look at a small batch:" ] @@ -2592,7 +2776,9 @@ "cell_type": "code", "execution_count": 111, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2604,7 +2790,9 @@ "cell_type": "code", "execution_count": 112, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2613,14 +2801,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Great! Now that the data is ready to be fed to the model, we need to build that model. Let's start with a simple implementation, then we will add all the bells and whistles." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "First let's reset the default graph." ] @@ -2629,16 +2823,21 @@ "cell_type": "code", "execution_count": 113, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph()" + "reset_graph()" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "The _moons_ dataset has two input features, since each instance is a point on a plane (i.e., 2-Dimensional):" ] @@ -2647,7 +2846,9 @@ "cell_type": "code", "execution_count": 114, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2656,7 +2857,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now let's build the Logistic Regression model. As we saw in chapter 4, this model first computes a weighted sum of the inputs (just like the Linear Regression model), and then it applies the sigmoid function to the result, which gives us the estimated probability for the positive class:\n", "\n", @@ -2665,7 +2869,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Recall that $\\mathbf{\\theta}$ is the parameter vector, containing the bias term $\\theta_0$ and the weights $\\theta_1, \\theta_2, \\dots, \\theta_n$. The input vector $\\mathbf{x}$ contains a constant term $x_0 = 1$, as well as all the input features $x_1, x_2, \\dots, x_n$.\n", "\n", @@ -2680,7 +2887,9 @@ "cell_type": "code", "execution_count": 115, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2693,7 +2902,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "In fact, TensorFlow has a nice function `tf.sigmoid()` that we can use to simplify the last line of the previous code:" ] @@ -2702,7 +2914,9 @@ "cell_type": "code", "execution_count": 116, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2711,7 +2925,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "As we saw in chapter 4, the log loss is a good cost function to use for Logistic Regression:\n", "\n", @@ -2724,7 +2941,9 @@ "cell_type": "code", "execution_count": 117, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2734,7 +2953,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "But we might as well use TensorFlow's `tf.losses.log_loss()` function:" ] @@ -2743,7 +2965,9 @@ "cell_type": "code", "execution_count": 118, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2752,7 +2976,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "The rest is pretty standard: let's create the optimizer and tell it to minimize the cost function:" ] @@ -2761,7 +2988,9 @@ "cell_type": "code", "execution_count": 119, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2772,7 +3001,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "All we need now (in this minimal version) is the variable initializer:" ] @@ -2781,7 +3013,9 @@ "cell_type": "code", "execution_count": 120, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2790,14 +3024,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "And we are ready to train the model and use it for predictions!" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "There's really nothing special about this code, it's virtually the same as the one we used earlier for Linear Regression:" ] @@ -2806,7 +3046,9 @@ "cell_type": "code", "execution_count": 121, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2830,14 +3072,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Note: we don't use the epoch number when generating batches, so we could just have a single `for` loop rather than 2 nested `for` loops, but it's convenient to think of training time in terms of number of epochs (i.e., roughly the number of times the algorithm went through the training set)." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "For each instance in the test set, `y_proba_val` contains the estimated probability that it belongs to the positive class, according to the model. For example, here are the first 5 estimated probabilities:" ] @@ -2846,7 +3094,9 @@ "cell_type": "code", "execution_count": 122, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2855,7 +3105,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "To classify each instance, we can go for maximum likelihood: classify as positive any instance whose estimated probability is greater or equal to 0.5:" ] @@ -2864,7 +3117,9 @@ "cell_type": "code", "execution_count": 123, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2874,14 +3129,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Depending on the use case, you may want to choose a different threshold than 0.5: make it higher if you want high precision (but lower recall), and make it lower if you want high recall (but lower precision). See chapter 3 for more details." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's compute the model's precision and recall:" ] @@ -2890,7 +3151,9 @@ "cell_type": "code", "execution_count": 124, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2903,7 +3166,9 @@ "cell_type": "code", "execution_count": 125, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2912,7 +3177,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's plot these predictions to see what they look like:" ] @@ -2921,7 +3189,9 @@ "cell_type": "code", "execution_count": 126, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2934,14 +3204,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Well, that looks pretty bad, doesn't it? But let's not forget that the Logistic Regression model has a linear decision boundary, so this is actually close to the best we can do with this model (unless we add more features, as we will show in a second)." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now let's start over, but this time we will add all the bells and whistles, as listed in the exercise:\n", "* Define the graph within a `logistic_regression()` function that can be reused easily.\n", @@ -2954,7 +3230,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Before we start, we will add 4 more features to the inputs: ${x_1}^2$, ${x_2}^2$, ${x_1}^3$ and ${x_2}^3$. This was not part of the exercise, but it will demonstrate how adding features can improve the model. We will do this manually, but you could also add them using `sklearn.preprocessing.PolynomialFeatures`." ] @@ -2963,7 +3242,9 @@ "cell_type": "code", "execution_count": 127, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2981,7 +3262,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "This is what the \"enhanced\" training set looks like:" ] @@ -2990,7 +3274,9 @@ "cell_type": "code", "execution_count": 128, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -2999,7 +3285,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Ok, next let's reset the default graph:" ] @@ -3008,16 +3297,21 @@ "cell_type": "code", "execution_count": 129, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ - "tf.reset_default_graph()" + "reset_graph()" ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now let's define the `logistic_regression()` function to create the graph. We will leave out the definition of the inputs `X` and the targets `y`. We could include them here, but leaving them out will make it easier to use this function in a wide range of use cases (e.g. perhaps we will want to add some preprocessing steps for the inputs before we feed them to the Logistic Regression model)." ] @@ -3026,7 +3320,9 @@ "cell_type": "code", "execution_count": 130, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3053,7 +3349,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Let's create a little function to get the name of the log directory to save the summaries for Tensorboard:" ] @@ -3062,7 +3361,9 @@ "cell_type": "code", "execution_count": 131, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3079,7 +3380,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Next, let's create the graph, using the `logistic_regression()` function. We will also create the `FileWriter` to save the summaries to the log directory for Tensorboard:" ] @@ -3088,7 +3392,9 @@ "cell_type": "code", "execution_count": 132, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3105,7 +3411,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "At last we can train the model! We will start by checking whether a previous training session was interrupted, and if so we will load the checkpoint and continue training from the epoch number we saved. In this example we just save the epoch number to a separate file, but in chapter 11 we will see how to store the training step directly as part of the model, using a non-trainable variable called `global_step` that we pass to the optimizer's `minimize()` method.\n", "\n", @@ -3116,7 +3425,9 @@ "cell_type": "code", "execution_count": 133, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3158,7 +3469,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Once again, we can make predictions by just classifying as positive all the instances whose estimated probability is greater or equal to 0.5:" ] @@ -3167,7 +3481,9 @@ "cell_type": "code", "execution_count": 134, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3178,7 +3494,9 @@ "cell_type": "code", "execution_count": 135, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3189,7 +3507,9 @@ "cell_type": "code", "execution_count": 136, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3200,7 +3520,9 @@ "cell_type": "code", "execution_count": 137, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3213,14 +3535,20 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now that's much, much better! Apparently the new features really helped a lot." ] }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Try starting the tensorboard server, find the latest run and look at the learning curve (i.e., how the loss evaluated on the test set evolves as a function of the epoch number):\n", "\n", @@ -3231,7 +3559,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "Now you can play around with the hyperparameters (e.g. the `batch_size` or the `learning_rate`) and run training again and again, comparing the learning curves. You can even automate this process by implementing grid search or randomized search. Below is a simple implementation of a randomized search on both the batch size and the learning rate. For the sake of simplicity, the checkpoint mechanism was removed." ] @@ -3240,7 +3571,9 @@ "cell_type": "code", "execution_count": 138, "metadata": { - "collapsed": false + "collapsed": false, + "deletable": true, + "editable": true }, "outputs": [], "source": [ @@ -3250,7 +3583,7 @@ "\n", "for search_iteration in range(n_search_iterations):\n", " batch_size = np.random.randint(1, 100)\n", - " learning_rate = reciprocal(0.0001, 0.1).rvs()\n", + " learning_rate = reciprocal(0.0001, 0.1).rvs(random_state=search_iteration)\n", "\n", " n_inputs = 2 + 4\n", " logdir = log_dir(\"logreg\")\n", @@ -3261,7 +3594,7 @@ " print(\" learning_rate:\", learning_rate)\n", " print(\" training: \", end=\"\")\n", "\n", - " tf.reset_default_graph()\n", + " reset_graph()\n", "\n", " X = tf.placeholder(tf.float32, shape=(None, n_inputs + 1), name=\"X\")\n", " y = tf.placeholder(tf.float32, shape=(None, 1), name=\"y\")\n", @@ -3300,7 +3633,10 @@ }, { "cell_type": "markdown", - "metadata": {}, + "metadata": { + "deletable": true, + "editable": true + }, "source": [ "The `reciprocal()` function from SciPy's `stats` module returns a random distribution that is commonly used when you have no idea of the optimal scale of a hyperparameter. See the exercise solutions for chapter 2 for more details. " ] @@ -3309,7 +3645,9 @@ "cell_type": "code", "execution_count": null, "metadata": { - "collapsed": true + "collapsed": true, + "deletable": true, + "editable": true }, "outputs": [], "source": []