From 2578ab36d121ad9439a600fa821a000084129c66 Mon Sep 17 00:00:00 2001 From: Dimitry Ishenko Date: Fri, 27 Mar 2020 11:28:51 -0400 Subject: [PATCH] Update week 2 assignments --- week2/preprocessed_mnist.py | 2 +- week2/v2/digits_classification.ipynb | 17 +- week2/v2/grading_utils.py | 6 +- week2/v2/ill-conditioned-demo.ipynb | 96 ++----- week2/v2/intro_to_tensorflow.ipynb | 15 +- week2/v2/mnist_with_keras.ipynb | 23 +- week2/v2/preprocessed_mnist.py | 2 +- week2/v2/tensorflow_examples_from_video.ipynb | 241 +++++------------- 8 files changed, 103 insertions(+), 299 deletions(-) diff --git a/week2/preprocessed_mnist.py b/week2/preprocessed_mnist.py index e04ea50..b838084 100644 --- a/week2/preprocessed_mnist.py +++ b/week2/preprocessed_mnist.py @@ -1,4 +1,4 @@ -import keras +from tensorflow import keras def load_dataset(flatten=False): diff --git a/week2/v2/digits_classification.ipynb b/week2/v2/digits_classification.ipynb index 9da64c7..cf7bb95 100644 --- a/week2/v2/digits_classification.ipynb +++ b/week2/v2/digits_classification.ipynb @@ -1,15 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set tf 1.x for colab\n", - "%tensorflow_version 1.x" - ] - }, { "cell_type": "markdown", "metadata": { @@ -38,7 +28,7 @@ "from sklearn.metrics import accuracy_score\n", "from matplotlib import pyplot as plt\n", "%matplotlib inline\n", - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", "print(\"We're using TF\", tf.__version__)\n", "\n", "import sys\n", @@ -176,7 +166,7 @@ "metadata": {}, "outputs": [], "source": [ - "import keras\n", + "from tensorflow import keras\n", "\n", "y_train_oh = keras.utils.to_categorical(y_train, 10)\n", "y_val_oh = keras.utils.to_categorical(y_val, 10)\n", @@ -192,6 +182,7 @@ "outputs": [], "source": [ "# run this again if you remake your graph\n", + "tf.disable_eager_execution()\n", "s = reset_tf_session()" ] }, @@ -398,4 +389,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/week2/v2/grading_utils.py b/week2/v2/grading_utils.py index a96058a..3780aa0 100644 --- a/week2/v2/grading_utils.py +++ b/week2/v2/grading_utils.py @@ -2,12 +2,8 @@ # -*- coding: utf-8 -*- -def get_tensor_shape(t): - return [d.value for d in t.shape] - - def get_tensors_shapes_string(tensors): res = [] for t in tensors: - res.extend([str(v) for v in get_tensor_shape(t)]) + res.extend([str(v) for v in t.shape]) return " ".join(res) diff --git a/week2/v2/ill-conditioned-demo.ipynb b/week2/v2/ill-conditioned-demo.ipynb index 0b20560..b473366 100644 --- a/week2/v2/ill-conditioned-demo.ipynb +++ b/week2/v2/ill-conditioned-demo.ipynb @@ -1,47 +1,25 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set tf 1.x for colab\n", - "%tensorflow_version 1.x" - ] - }, { "cell_type": "markdown", "metadata": {}, "source": [ "Read about ill-conditioning: http://cnl.salk.edu/~schraudo/teach/NNcourse/precond.html" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "name": "stderr", - "output_type": "stream", - "text": [ - "Using TensorFlow backend.\n" - ] - }, - { - "name": "stdout", - "output_type": "stream", - "text": [ - "We're using TF 1.3.0\n" - ] - } - ], + "outputs": [], "source": [ - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", "import sys\n", "sys.path.append(\"../..\")\n", "from keras_utils import reset_tf_session\n", + "tf.disable_eager_execution()\n", "s = reset_tf_session()\n", "print(\"We're using TF\", tf.__version__)\n", "from matplotlib import animation, rc\n", @@ -53,7 +31,7 @@ }, { "cell_type": "code", - "execution_count": 2, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -103,38 +81,18 @@ }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, - "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "plot_gd(x_scale=1.0, lr=0.1, steps=25)" ] }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, + "execution_count": null, "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# narrow valleys\n", "plot_gd(x_scale=0.5, lr=0.1, steps=25)" @@ -142,19 +100,9 @@ }, { "cell_type": "code", - "execution_count": 5, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, + "execution_count": null, "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# narrower valleys\n", "plot_gd(x_scale=0.2, lr=0.1, steps=25)" @@ -162,19 +110,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/html": [ - "" - ] - }, + "execution_count": null, "metadata": {}, - "output_type": "display_data" - } - ], + "outputs": [], "source": [ "# bigger learning rate then?\n", "# x is changed faster, but y changes are too big, leads to oscillation\n", @@ -203,4 +141,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/week2/v2/intro_to_tensorflow.ipynb b/week2/v2/intro_to_tensorflow.ipynb index 540379f..c2ef7ad 100644 --- a/week2/v2/intro_to_tensorflow.ipynb +++ b/week2/v2/intro_to_tensorflow.ipynb @@ -1,15 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set tf 1.x for colab\n", - "%tensorflow_version 1.x" - ] - }, { "cell_type": "markdown", "metadata": { @@ -48,10 +38,11 @@ "metadata": {}, "outputs": [], "source": [ - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", "import sys\n", "sys.path.append(\"../..\")\n", "from keras_utils import reset_tf_session\n", + "tf.disable_eager_execution()\n", "s = reset_tf_session()\n", "print(\"We're using TF\", tf.__version__)" ] @@ -581,4 +572,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/week2/v2/mnist_with_keras.ipynb b/week2/v2/mnist_with_keras.ipynb index 3262c8a..facb95b 100644 --- a/week2/v2/mnist_with_keras.ipynb +++ b/week2/v2/mnist_with_keras.ipynb @@ -1,15 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set tf 1.x for colab\n", - "%tensorflow_version 1.x" - ] - }, { "cell_type": "markdown", "metadata": { @@ -46,13 +36,12 @@ "%matplotlib inline\n", "import tensorflow as tf\n", "print(\"We're using TF\", tf.__version__)\n", - "import keras\n", + "from tensorflow import keras\n", "print(\"We are using Keras\", keras.__version__)\n", "\n", "import sys\n", "sys.path.append(\"../..\")\n", - "import keras_utils\n", - "from keras_utils import reset_tf_session" + "import keras_utils" ] }, { @@ -127,11 +116,11 @@ "outputs": [], "source": [ "# building a model with keras\n", - "from keras.layers import Dense, Activation\n", - "from keras.models import Sequential\n", + "from tensorflow.keras.layers import Dense, Activation\n", + "from tensorflow.keras.models import Sequential\n", "\n", "# we still need to clear a graph though\n", - "s = reset_tf_session()\n", + "K.clear_session()\n", "\n", "model = Sequential() # it is a feed-forward network without loops like in RNN\n", "model.add(Dense(256, input_shape=(784,))) # the first layer must specify the input shape (replacing placeholders)\n", @@ -259,4 +248,4 @@ }, "nbformat": 4, "nbformat_minor": 1 -} +} \ No newline at end of file diff --git a/week2/v2/preprocessed_mnist.py b/week2/v2/preprocessed_mnist.py index e04ea50..b838084 100644 --- a/week2/v2/preprocessed_mnist.py +++ b/week2/v2/preprocessed_mnist.py @@ -1,4 +1,4 @@ -import keras +from tensorflow import keras def load_dataset(flatten=False): diff --git a/week2/v2/tensorflow_examples_from_video.ipynb b/week2/v2/tensorflow_examples_from_video.ipynb index a2129d8..00c9098 100644 --- a/week2/v2/tensorflow_examples_from_video.ipynb +++ b/week2/v2/tensorflow_examples_from_video.ipynb @@ -1,15 +1,5 @@ { "cells": [ - { - "cell_type": "code", - "execution_count": null, - "metadata": {}, - "outputs": [], - "source": [ - "# set tf 1.x for colab\n", - "%tensorflow_version 1.x" - ] - }, { "cell_type": "markdown", "metadata": {}, @@ -19,7 +9,7 @@ }, { "cell_type": "code", - "execution_count": 1, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -28,26 +18,19 @@ }, { "cell_type": "code", - "execution_count": 2, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "1.3.0\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ - "import tensorflow as tf\n", + "import tensorflow.compat.v1 as tf\n", + "tf.disable_eager_execution()\n", "import numpy as np\n", "print(tf.__version__)" ] }, { "cell_type": "code", - "execution_count": 3, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -59,24 +42,16 @@ }, { "cell_type": "code", - "execution_count": 4, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "Tensor(\"matmul:0\", shape=(2, 2), dtype=float32)\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "print(c)" ] }, { "cell_type": "code", - "execution_count": 5, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -85,21 +60,9 @@ }, { "cell_type": "code", - "execution_count": 6, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 2., 2.],\n", - " [ 2., 2.]], dtype=float32)" - ] - }, - "execution_count": 6, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "s.run(tf.global_variables_initializer())\n", "s.run(c, feed_dict={a: np.ones((2, 2))})" @@ -107,7 +70,7 @@ }, { "cell_type": "code", - "execution_count": 7, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -119,18 +82,22 @@ "metadata": {}, "source": [ "# Video \"Our first model in TensorFlow\"" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Simple optimization (with simple prints)" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": 8, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -141,7 +108,7 @@ }, { "cell_type": "code", - "execution_count": 9, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -151,46 +118,18 @@ }, { "cell_type": "code", - "execution_count": 10, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "[]" - ] - }, - "execution_count": 10, - "metadata": {}, - "output_type": "execute_result" - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "tf.trainable_variables()" ] }, { "cell_type": "code", - "execution_count": 11, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "-0.865988 1.17177\n", - "-0.69279 0.749935\n", - "-0.554232 0.479959\n", - "-0.443386 0.307174\n", - "-0.354709 0.196591\n", - "-0.283767 0.125818\n", - "-0.227014 0.0805237\n", - "-0.181611 0.0515352\n", - "-0.145289 0.0329825\n", - "-0.116231 0.0211088\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "with tf.Session() as s: # in this way session will be closed automatically\n", " s.run(tf.global_variables_initializer())\n", @@ -204,11 +143,13 @@ "metadata": {}, "source": [ "### Simple optimization (with tf.Print)" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": 12, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -220,7 +161,7 @@ }, { "cell_type": "code", - "execution_count": 13, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -230,7 +171,7 @@ }, { "cell_type": "code", - "execution_count": 14, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -242,7 +183,7 @@ }, { "cell_type": "code", - "execution_count": 15, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -264,11 +205,13 @@ "metadata": {}, "source": [ "### Simple optimization (with TensorBoard logging)" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": 16, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -279,7 +222,7 @@ }, { "cell_type": "code", - "execution_count": 17, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -289,7 +232,7 @@ }, { "cell_type": "code", - "execution_count": 18, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -300,7 +243,7 @@ }, { "cell_type": "code", - "execution_count": 19, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -318,7 +261,9 @@ "metadata": {}, "source": [ "Run `tensorboard --logdir=./logs` in bash" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", @@ -326,14 +271,18 @@ "source": [ "This is what you can see in your browser **(not available in Coursera Notebooks)**\n", "" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "**If you're running on Google Colab you can still run TensorBoard!**" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", @@ -355,7 +304,7 @@ }, { "cell_type": "code", - "execution_count": 20, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -367,22 +316,15 @@ "metadata": {}, "source": [ "### Training a linear model" - ] + ], + "execution_count": null, + "outputs": [] }, { "cell_type": "code", - "execution_count": 21, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(1000, 3) (1000, 1)\n", - "[[ 0.09498027 0.48793618 0.39011257]]\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "# generate model data\n", "N = 1000\n", @@ -397,17 +339,9 @@ }, { "cell_type": "code", - "execution_count": 22, - "metadata": {}, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "(?, 1) (?, 1) ()\n" - ] - } - ], + "execution_count": null, + "metadata": {}, + "outputs": [], "source": [ "tf.reset_default_graph()\n", "\n", @@ -424,7 +358,7 @@ }, { "cell_type": "code", - "execution_count": 23, + "execution_count": null, "metadata": {}, "outputs": [], "source": [ @@ -434,24 +368,11 @@ }, { "cell_type": "code", - "execution_count": 24, + "execution_count": null, "metadata": { "scrolled": true }, - "outputs": [ - { - "name": "stdout", - "output_type": "stream", - "text": [ - "0.532865\n", - "0.0458802\n", - "0.0410158\n", - "0.040087\n", - "0.0399092\n", - "0.0398751\n" - ] - } - ], + "outputs": [], "source": [ "with tf.Session() as s:\n", " s.run(tf.global_variables_initializer())\n", @@ -464,20 +385,9 @@ }, { "cell_type": "code", - "execution_count": 25, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 0.11388827, 0.4882018 , 0.36716884]], dtype=float32)" - ] - }, - "execution_count": 25, + "execution_count": null, "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# found weights\n", "curr_weights.T" @@ -485,20 +395,9 @@ }, { "cell_type": "code", - "execution_count": 26, - "metadata": {}, - "outputs": [ - { - "data": { - "text/plain": [ - "array([[ 0.09498027, 0.48793618, 0.39011257]])" - ] - }, - "execution_count": 26, + "execution_count": null, "metadata": {}, - "output_type": "execute_result" - } - ], + "outputs": [], "source": [ "# true weights\n", "w.T" @@ -526,4 +425,4 @@ }, "nbformat": 4, "nbformat_minor": 2 -} +} \ No newline at end of file