Skip to content

Commit

Permalink
Use np.random.set_seed(42) and tf.set_random_seed(42) to make noteboo…
Browse files Browse the repository at this point in the history
…k's output constant, and simplify code in notebook 15
  • Loading branch information
ageron committed Jun 7, 2017
1 parent 6689c3a commit 9938d06
Show file tree
Hide file tree
Showing 3 changed files with 1,660 additions and 1,212 deletions.
34 changes: 18 additions & 16 deletions 12_distributed_tensorflow.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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(seed)\n",
" np.random.seed(seed)\n",
"\n",
"# To plot pretty figures\n",
"%matplotlib inline\n",
Expand Down Expand Up @@ -209,7 +211,7 @@
},
"outputs": [],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"with tf.device(\"/job:ps\"):\n",
" a = tf.Variable(1.0, name=\"a\")\n",
Expand Down Expand Up @@ -254,7 +256,7 @@
},
"outputs": [],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"with tf.device(tf.train.replica_device_setter(\n",
" ps_tasks=2,\n",
Expand Down Expand Up @@ -300,15 +302,15 @@
"output_type": "stream",
"text": [
"No more files to read\n",
"[array([[ 4. , 5. ],\n",
" [ 1. , -508.17480469]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[ 4.00000000e+00, 5.00000000e+00],\n",
" [ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[ 7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
]
}
],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"test_csv = open(\"my_test.csv\", \"w\")\n",
"test_csv.write(\"x1, x2 , target\\n\")\n",
Expand Down Expand Up @@ -393,15 +395,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[array([[ 1. , -508.17480469],\n",
" [ 7. , 8. ]], dtype=float32), array([0, 0], dtype=int32)]\n",
"[array([[ 4., 5.]], dtype=float32), array([1], dtype=int32)]\n",
"[array([[ 7., 8.],\n",
" [ 4., 5.]], dtype=float32), array([0, 1], dtype=int32)]\n",
"[array([[ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
]
}
],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"filename_queue = tf.FIFOQueue(capacity=10, dtypes=[tf.string], shapes=[()])\n",
"filename = tf.placeholder(tf.string)\n",
Expand Down Expand Up @@ -451,15 +453,15 @@
"name": "stdout",
"output_type": "stream",
"text": [
"[array([[ 1. , -508.17480469],\n",
" [ 4. , 5. ]], dtype=float32), array([0, 1], dtype=int32)]\n",
"[array([[ 4.00000000e+00, 5.00000000e+00],\n",
" [ 1.00000000e+00, 8.62997533e-19]], dtype=float32), array([1, 0], dtype=int32)]\n",
"[array([[ 7., 8.]], dtype=float32), array([0], dtype=int32)]\n",
"No more training instances\n"
]
}
],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"def read_and_push_instance(filename_queue, instance_queue):\n",
" reader = tf.TextLineReader(skip_header_lines=1)\n",
Expand Down Expand Up @@ -529,7 +531,7 @@
}
],
"source": [
"tf.reset_default_graph()\n",
"reset_graph()\n",
"\n",
"q = tf.FIFOQueue(capacity=10, dtypes=[tf.float32], shapes=[()])\n",
"v = tf.placeholder(tf.float32)\n",
Expand Down Expand Up @@ -577,7 +579,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 15,
"metadata": {
"collapsed": true,
"deletable": true,
Expand Down
Loading

0 comments on commit 9938d06

Please sign in to comment.