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