{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# TensorFlow Reproducibility"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from __future__ import division, print_function, unicode_literals\n",
    "\n",
    "import numpy as np\n",
    "import tensorflow as tf\n",
    "from tensorflow import keras"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Checklist"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "1. Do not run TensorFlow on the GPU.\n",
    "2. Beware of multithreading, and make TensorFlow single-threaded.\n",
    "3. Set all the random seeds.\n",
    "4. Eliminate any other source of variability."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Do Not Run TensorFlow on the GPU"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Some operations (like `tf.reduce_sum()`) have favor performance over precision, and their outputs may vary slightly across runs. To get reproducible results, make sure TensorFlow runs on the CPU:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Beware of Multithreading"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Because floats have limited precision, the order of execution matters:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "2. * 5. / 7."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "2. / 7. * 5."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should make sure TensorFlow runs your ops on a single thread:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "config = tf.ConfigProto(intra_op_parallelism_threads=1,\n",
    "                        inter_op_parallelism_threads=1)\n",
    "\n",
    "with tf.Session(config=config) as sess:\n",
    "    #... this will run single threaded\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The thread pools for all sessions are created when you create the first session, so all sessions in the rest of this notebook will be single-threaded:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "with tf.Session() as sess:\n",
    "    #... also single-threaded!\n",
    "    pass"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Set all the random seeds!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python's built-in `hash()` function"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(set(\"Try restarting the kernel and running this again\"))\n",
    "print(set(\"Try restarting the kernel and running this again\"))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Since Python 3.3, the result will be different every time, unless you start Python with the `PYTHONHASHSEED` environment variable set to `0`:"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```shell\n",
    "PYTHONHASHSEED=0 python\n",
    "```\n",
    "\n",
    "```pycon\n",
    ">>> print(set(\"Now the output is stable across runs\"))\n",
    "{'n', 'b', 'h', 'o', 'i', 'a', 'r', 't', 'p', 'N', 's', 'c', ' ', 'l', 'e', 'w', 'u'}\n",
    ">>> exit()\n",
    "```\n",
    "\n",
    "```shell\n",
    "PYTHONHASHSEED=0 python\n",
    "```\n",
    "```pycon\n",
    ">>> print(set(\"Now the output is stable across runs\"))\n",
    "{'n', 'b', 'h', 'o', 'i', 'a', 'r', 't', 'p', 'N', 's', 'c', ' ', 'l', 'e', 'w', 'u'}\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Alternatively, you could set this environment variable system-wide, but that's probably not a good idea, because this automatic randomization was [introduced for security reasons](http://ocert.org/advisories/ocert-2011-003.html)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately, setting the environment variable from within Python (e.g., using `os.environ[\"PYTHONHASHSEED\"]=\"0\"`) will not work, because Python reads it upon startup. For Jupyter notebooks, you have to start the Jupyter server like this:\n",
    "\n",
    "```shell\n",
    "PYTHONHASHSEED=0 jupyter notebook\n",
    "```"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "if os.environ.get(\"PYTHONHASHSEED\") != \"0\":\n",
    "    raise Exception(\"You must set PYTHONHASHSEED=0 when starting the Jupyter server to get reproducible results.\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Python Random Number Generators (RNGs)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import random\n",
    "\n",
    "random.seed(42)\n",
    "print(random.random())\n",
    "print(random.random())\n",
    "\n",
    "print()\n",
    "\n",
    "random.seed(42)\n",
    "print(random.random())\n",
    "print(random.random())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### NumPy RNGs"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "\n",
    "np.random.seed(42)\n",
    "print(np.random.rand())\n",
    "print(np.random.rand())\n",
    "\n",
    "print()\n",
    "\n",
    "np.random.seed(42)\n",
    "print(np.random.rand())\n",
    "print(np.random.rand())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### TensorFlow RNGs"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "TensorFlow's behavior is more complex because of two things:\n",
    "* you create a graph, and then you execute it. The random seed must be set before you create the random operations.\n",
    "* there are two seeds: one at the graph level, and one at the individual random operation level."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n",
    "\n",
    "tf.set_random_seed(42)\n",
    "rnd = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Every time you reset the graph, you need to set the seed again:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "tf.set_random_seed(42)\n",
    "rnd = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you create your own graph, it will ignore the default graph's seed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "tf.set_random_seed(42)\n",
    "\n",
    "graph = tf.Graph()\n",
    "with graph.as_default():\n",
    "    rnd = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session(graph=graph):\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session(graph=graph):\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You must set its own seed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [],
   "source": [
    "graph = tf.Graph()\n",
    "with graph.as_default():\n",
    "    tf.set_random_seed(42)\n",
    "    rnd = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session(graph=graph):\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session(graph=graph):\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you set the seed after the random operation is created, the seed has no effet:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "rnd = tf.random_uniform(shape=[])\n",
    "\n",
    "tf.set_random_seed(42) # BAD, NO EFFECT!\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "tf.set_random_seed(42) # BAD, NO EFFECT!\n",
    "with tf.Session() as sess:\n",
    "    print(rnd.eval())\n",
    "    print(rnd.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### A note about operation seeds"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can also set a seed for each individual random operation. When you do, it is combined with the graph seed into the final seed used by that op. The following table summarizes how this works:\n",
    "\n",
    "| Graph seed | Op seed |  Resulting seed                |\n",
    "|------------|---------|--------------------------------|\n",
    "| None       | None    | Random                         |\n",
    "| graph_seed | None    | f(graph_seed, op_index)        |\n",
    "| None       | op_seed | f(default_graph_seed, op_seed) |\n",
    "| graph_seed | op_seed | f(graph_seed, op_seed)         |\n",
    "\n",
    "* `f()` is a deterministic function.\n",
    "* `op_index = graph._last_id` when there is a graph seed, different random ops without op seeds will have different outputs. However, each of them will have the same sequence of outputs at every run.\n",
    "\n",
    "In eager mode, there is a global seed instead of graph seed (since there is no graph in eager mode)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "rnd1 = tf.random_uniform(shape=[], seed=42)\n",
    "rnd2 = tf.random_uniform(shape=[], seed=42)\n",
    "rnd3 = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "In the following example, you may think that all random ops will have the same random seed, but `rnd3` will actually have a different seed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "tf.set_random_seed(42)\n",
    "\n",
    "rnd1 = tf.random_uniform(shape=[], seed=42)\n",
    "rnd2 = tf.random_uniform(shape=[], seed=42)\n",
    "rnd3 = tf.random_uniform(shape=[])\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "\n",
    "print()\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())\n",
    "    print(rnd1.eval())\n",
    "    print(rnd2.eval())\n",
    "    print(rnd3.eval())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Estimators API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "**Tip**: in a Jupyter notebook, you probably want to set the random seeds regularly so that you can come back and run the notebook from there (instead of from the beginning) and still get reproducible outputs."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(42)\n",
    "np.random.seed(42)\n",
    "tf.set_random_seed(42)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you use the Estimators API, make sure to create a `RunConfig` and set its `tf_random_seed`, then pass it to the constructor of your estimator:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "my_config = tf.estimator.RunConfig(tf_random_seed=42)\n",
    "\n",
    "feature_cols = [tf.feature_column.numeric_column(\"X\", shape=[28 * 28])]\n",
    "dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300, 100], n_classes=10,\n",
    "                                     feature_columns=feature_cols,\n",
    "                                     config=my_config)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Let's try it on MNIST:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [],
   "source": [
    "(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()\n",
    "X_train = X_train.astype(np.float32).reshape(-1, 28*28) / 255.0\n",
    "y_train = y_train.astype(np.int32)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Unfortunately, the `numpy_input_fn` does not allow us to set the seed when `shuffle=True`, so we must shuffle the data ourself and set `shuffle=False`."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "indices = np.random.permutation(len(X_train))\n",
    "X_train_shuffled = X_train[indices]\n",
    "y_train_shuffled = y_train[indices]\n",
    "\n",
    "input_fn = tf.estimator.inputs.numpy_input_fn(\n",
    "    x={\"X\": X_train_shuffled}, y=y_train_shuffled, num_epochs=10, batch_size=32, shuffle=False)\n",
    "dnn_clf.train(input_fn=input_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final loss should be exactly 0.46282205."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Instead of using the `numpy_input_fn()` function (which cannot reproducibly shuffle the dataset at each epoch), you can create your own input function using the Data API and set its shuffling seed:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "def create_dataset(X, y=None, n_epochs=1, batch_size=32,\n",
    "                   buffer_size=1000, seed=None):\n",
    "    dataset = tf.data.Dataset.from_tensor_slices(({\"X\": X}, y))\n",
    "    dataset = dataset.repeat(n_epochs)\n",
    "    dataset = dataset.shuffle(buffer_size, seed=seed)\n",
    "    return dataset.batch(batch_size)\n",
    "\n",
    "input_fn=lambda: create_dataset(X_train, y_train, seed=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "random.seed(42)\n",
    "np.random.seed(42)\n",
    "tf.set_random_seed(42)\n",
    "\n",
    "my_config = tf.estimator.RunConfig(tf_random_seed=42)\n",
    "\n",
    "feature_cols = [tf.feature_column.numeric_column(\"X\", shape=[28 * 28])]\n",
    "dnn_clf = tf.estimator.DNNClassifier(hidden_units=[300, 100], n_classes=10,\n",
    "                                     feature_columns=feature_cols,\n",
    "                                     config=my_config)\n",
    "dnn_clf.train(input_fn=input_fn)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The final loss should be exactly 1.0556093."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "```python\n",
    "indices = np.random.permutation(len(X_train))\n",
    "X_train_shuffled = X_train[indices]\n",
    "y_train_shuffled = y_train[indices]\n",
    "\n",
    "input_fn = tf.estimator.inputs.numpy_input_fn(\n",
    "    x={\"X\": X_train_shuffled}, y=y_train_shuffled,\n",
    "    num_epochs=10, batch_size=32, shuffle=False)\n",
    "dnn_clf.train(input_fn=input_fn)\n",
    "```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Keras API"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you use the Keras API, all you need to do is set the random seed any time you clear the session:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {},
   "outputs": [],
   "source": [
    "keras.backend.clear_session()\n",
    "\n",
    "random.seed(42)\n",
    "np.random.seed(42)\n",
    "tf.set_random_seed(42)\n",
    "\n",
    "model = keras.models.Sequential([\n",
    "    keras.layers.Dense(300, activation=\"relu\"),\n",
    "    keras.layers.Dense(100, activation=\"relu\"),\n",
    "    keras.layers.Dense(10, activation=\"softmax\"),\n",
    "])\n",
    "model.compile(loss=\"sparse_categorical_crossentropy\", optimizer=\"sgd\",\n",
    "              metrics=[\"accuracy\"])\n",
    "model.fit(X_train, y_train, epochs=10)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should get exactly 97.16% accuracy on the training set at the end of training."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Eliminate other sources of variability"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "For example, `os.listdir()` returns file names in an order that depends on how the files were indexed by the file system:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    with open(\"my_test_foo_{}\".format(i), \"w\"):\n",
    "        pass\n",
    "\n",
    "[f for f in os.listdir() if f.startswith(\"my_test_foo_\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in range(10):\n",
    "    with open(\"my_test_bar_{}\".format(i), \"w\"):\n",
    "        pass\n",
    "\n",
    "[f for f in os.listdir() if f.startswith(\"my_test_bar_\")]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You should sort the file names before you use them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "filenames = os.listdir()\n",
    "filenames.sort()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "[f for f in filenames if f.startswith(\"my_test_foo_\")]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "for f in os.listdir():\n",
    "    if f.startswith(\"my_test_foo_\") or f.startswith(\"my_test_bar_\"):\n",
    "        os.remove(f)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "I hope you enjoyed this notebook. If you do not get reproducible results, or if they are different than mine, then please [file an issue](https://github.com/ageron/handson-ml2/issues) on github, specifying what version of Python, TensorFlow, and NumPy you are using, as well as your O.S. version. Thank you!"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "If you want to learn more about Deep Learning and TensorFlow, check out my book [Hands-On Machine Learning with Scitkit-Learn and TensorFlow](http://homl.info/amazon), O'Reilly. You can also follow me on twitter [@aureliengeron](https://twitter.com/aureliengeron) or watch my videos on YouTube at [youtube.com/c/AurelienGeron](https://www.youtube.com/c/AurelienGeron)."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
