Remove uncessary reuse_vars_dict in notebook 11

main
Aurélien Geron 2018-03-24 22:50:29 +01:00
parent f9ac449f97
commit a164ffc699
1 changed files with 55 additions and 159 deletions

View File

@ -31,9 +31,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 1, "execution_count": 1,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"# To support both python 2 and python 3\n", "# To support both python 2 and python 3\n",
@ -79,9 +77,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 2, "execution_count": 2,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def logit(z):\n", "def logit(z):\n",
@ -134,9 +130,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 4,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"import tensorflow as tf" "import tensorflow as tf"
@ -145,9 +139,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 5, "execution_count": 5,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -161,9 +153,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 6, "execution_count": 6,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"he_init = tf.contrib.layers.variance_scaling_initializer()\n", "he_init = tf.contrib.layers.variance_scaling_initializer()\n",
@ -188,9 +178,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 7, "execution_count": 7,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def leaky_relu(z, alpha=0.01):\n", "def leaky_relu(z, alpha=0.01):\n",
@ -226,9 +214,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 9, "execution_count": 9,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -239,9 +225,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 10, "execution_count": 10,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def leaky_relu(z, name=None):\n", "def leaky_relu(z, name=None):\n",
@ -260,9 +244,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 11, "execution_count": 11,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -276,9 +258,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 12, "execution_count": 12,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n", "X = tf.placeholder(tf.float32, shape=(None, n_inputs), name=\"X\")\n",
@ -288,9 +268,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 13, "execution_count": 13,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"dnn\"):\n", "with tf.name_scope(\"dnn\"):\n",
@ -302,9 +280,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 14, "execution_count": 14,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"loss\"):\n", "with tf.name_scope(\"loss\"):\n",
@ -315,9 +291,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 15, "execution_count": 15,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"learning_rate = 0.01\n", "learning_rate = 0.01\n",
@ -330,9 +304,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 16, "execution_count": 16,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"eval\"):\n", "with tf.name_scope(\"eval\"):\n",
@ -343,9 +315,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 17, "execution_count": 17,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
@ -404,9 +374,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 20, "execution_count": 20,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def elu(z, alpha=1):\n", "def elu(z, alpha=1):\n",
@ -441,9 +409,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 22, "execution_count": 22,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -454,9 +420,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 23, "execution_count": 23,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.elu, name=\"hidden1\")" "hidden1 = tf.layers.dense(X, n_hidden1, activation=tf.nn.elu, name=\"hidden1\")"
@ -479,9 +443,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 24, "execution_count": 24,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def selu(z,\n", "def selu(z,\n",
@ -543,9 +505,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 27,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"def selu(z,\n", "def selu(z,\n",
@ -571,9 +531,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 28, "execution_count": 28,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -668,9 +626,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 30, "execution_count": 30,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -702,9 +658,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 31, "execution_count": 31,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -723,9 +677,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 32, "execution_count": 32,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"from functools import partial\n", "from functools import partial\n",
@ -753,9 +705,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 33, "execution_count": 33,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -811,9 +761,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 34, "execution_count": 34,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"n_epochs = 20\n", "n_epochs = 20\n",
@ -912,9 +860,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 38, "execution_count": 38,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -946,9 +892,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 39, "execution_count": 39,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"learning_rate = 0.01" "learning_rate = 0.01"
@ -964,9 +908,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 40, "execution_count": 40,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"threshold = 1.0\n", "threshold = 1.0\n",
@ -988,9 +930,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 41, "execution_count": 41,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"eval\"):\n", "with tf.name_scope(\"eval\"):\n",
@ -1001,9 +941,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 42, "execution_count": 42,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
@ -1013,9 +951,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 43, "execution_count": 43,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"n_epochs = 20\n", "n_epochs = 20\n",
@ -1065,9 +1001,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 45, "execution_count": 45,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()" "reset_graph()"
@ -1076,9 +1010,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 46, "execution_count": 46,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"saver = tf.train.import_meta_graph(\"./my_model_final.ckpt.meta\")" "saver = tf.train.import_meta_graph(\"./my_model_final.ckpt.meta\")"
@ -1111,9 +1043,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 48, "execution_count": 48,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"from IPython.display import clear_output, Image, display, HTML\n", "from IPython.display import clear_output, Image, display, HTML\n",
@ -1175,9 +1105,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 50, "execution_count": 50,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"X = tf.get_default_graph().get_tensor_by_name(\"X:0\")\n", "X = tf.get_default_graph().get_tensor_by_name(\"X:0\")\n",
@ -1198,9 +1126,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 51, "execution_count": 51,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"for op in (X, y, accuracy, training_op):\n", "for op in (X, y, accuracy, training_op):\n",
@ -1217,9 +1143,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 52, "execution_count": 52,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"X, y, accuracy, training_op = tf.get_collection(\"my_important_ops\")" "X, y, accuracy, training_op = tf.get_collection(\"my_important_ops\")"
@ -1280,9 +1204,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 55, "execution_count": 55,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1363,9 +1285,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 57, "execution_count": 57,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1437,9 +1357,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 59, "execution_count": 59,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1489,8 +1407,7 @@
"source": [ "source": [
"reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n", "reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n",
" scope=\"hidden[123]\") # regular expression\n", " scope=\"hidden[123]\") # regular expression\n",
"reuse_vars_dict = dict([(var.op.name, var) for var in reuse_vars])\n", "restore_saver = tf.train.Saver(reuse_vars) # to restore layers 1-3\n",
"restore_saver = tf.train.Saver(reuse_vars_dict) # to restore layers 1-3\n",
"\n", "\n",
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
"saver = tf.train.Saver()\n", "saver = tf.train.Saver()\n",
@ -1527,9 +1444,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 61, "execution_count": 61,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1670,9 +1585,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 67, "execution_count": 67,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1706,9 +1619,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 68, "execution_count": 68,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"train\"): # not shown in the book\n", "with tf.name_scope(\"train\"): # not shown in the book\n",
@ -1721,9 +1632,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 69, "execution_count": 69,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
@ -1738,8 +1647,7 @@
"source": [ "source": [
"reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n", "reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n",
" scope=\"hidden[123]\") # regular expression\n", " scope=\"hidden[123]\") # regular expression\n",
"reuse_vars_dict = dict([(var.op.name, var) for var in reuse_vars])\n", "restore_saver = tf.train.Saver(reuse_vars) # to restore layers 1-3\n",
"restore_saver = tf.train.Saver(reuse_vars_dict) # to restore layers 1-3\n",
"\n", "\n",
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
"saver = tf.train.Saver()\n", "saver = tf.train.Saver()\n",
@ -1762,9 +1670,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 71, "execution_count": 71,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1783,9 +1689,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 72, "execution_count": 72,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"dnn\"):\n", "with tf.name_scope(\"dnn\"):\n",
@ -1804,9 +1708,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 73, "execution_count": 73,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"with tf.name_scope(\"loss\"):\n", "with tf.name_scope(\"loss\"):\n",
@ -1837,8 +1739,7 @@
"source": [ "source": [
"reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n", "reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n",
" scope=\"hidden[123]\") # regular expression\n", " scope=\"hidden[123]\") # regular expression\n",
"reuse_vars_dict = dict([(var.op.name, var) for var in reuse_vars])\n", "restore_saver = tf.train.Saver(reuse_vars) # to restore layers 1-3\n",
"restore_saver = tf.train.Saver(reuse_vars_dict) # to restore layers 1-3\n",
"\n", "\n",
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
"saver = tf.train.Saver()\n", "saver = tf.train.Saver()\n",
@ -1868,9 +1769,7 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 75, "execution_count": 75,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reset_graph()\n", "reset_graph()\n",
@ -1913,15 +1812,12 @@
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 76, "execution_count": 76,
"metadata": { "metadata": {},
"collapsed": true
},
"outputs": [], "outputs": [],
"source": [ "source": [
"reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n", "reuse_vars = tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES,\n",
" scope=\"hidden[123]\") # regular expression\n", " scope=\"hidden[123]\") # regular expression\n",
"reuse_vars_dict = dict([(var.op.name, var) for var in reuse_vars])\n", "restore_saver = tf.train.Saver(reuse_vars) # to restore layers 1-3\n",
"restore_saver = tf.train.Saver(reuse_vars_dict) # to restore layers 1-3\n",
"\n", "\n",
"init = tf.global_variables_initializer()\n", "init = tf.global_variables_initializer()\n",
"saver = tf.train.Saver()" "saver = tf.train.Saver()"
@ -4941,7 +4837,7 @@
"name": "python", "name": "python",
"nbconvert_exporter": "python", "nbconvert_exporter": "python",
"pygments_lexer": "ipython3", "pygments_lexer": "ipython3",
"version": "3.6.2" "version": "3.6.4"
}, },
"nav_menu": { "nav_menu": {
"height": "360px", "height": "360px",