{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# CS 20 : TensorFlow for Deep Learning Research\n", "## Lecture 05 : Variable sharing and managing experiments\n", "### Randomization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Setup" ] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1.12.0\n" ] } ], "source": [ "from __future__ import absolute_import, division, print_function\n", "import tensorflow as tf\n", "\n", "print(tf.__version__)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 1 : Session keeps track of the random state" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.85811085\n", "-0.20793143\n" ] } ], "source": [ "c = tf.random_normal(shape = [], seed = 2)\n", "\n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " print(sess.run(c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 2 : Each new session will start the random state all over again" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.85811085\n", "-0.85811085\n", "-0.20793143\n" ] } ], "source": [ "tf.reset_default_graph()\n", "\n", "c = tf.random_normal(shape = [], seed = 2)\n", "\n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " \n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " print(sess.run(c))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 3 : With operation level random seed, each op keeps its own seed" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-0.85811085\n", "-0.85811085\n" ] } ], "source": [ "tf.reset_default_graph()\n", "\n", "c = tf.random_normal(shape = [], seed = 2)\n", "d = tf.random_normal(shape = [], seed = 2)\n", "\n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " print(sess.run(d))" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Example 4 : Graph level random seed" ] }, { "cell_type": "code", "execution_count": 5, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.4236197\n", "0.8052349\n" ] } ], "source": [ "tf.reset_default_graph()\n", "tf.set_random_seed(seed = 2)\n", "\n", "c = tf.random_normal(shape = [])\n", "d = tf.random_normal(shape = [])\n", "\n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " print(sess.run(d))" ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-1.4236197\n", "0.8052349\n" ] } ], "source": [ "tf.reset_default_graph()\n", "tf.set_random_seed(seed = 2)\n", "\n", "c = tf.random_normal(shape = [])\n", "d = tf.random_normal(shape = [])\n", "\n", "with tf.Session() as sess:\n", " print(sess.run(c))\n", " print(sess.run(d))" ] } ], "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.8" }, "varInspector": { "cols": { "lenName": 16, "lenType": 16, "lenVar": 40 }, "kernels_config": { "python": { "delete_cmd_postfix": "", "delete_cmd_prefix": "del ", "library": "var_list.py", "varRefreshCmd": "print(var_dic_list())" }, "r": { "delete_cmd_postfix": ") ", "delete_cmd_prefix": "rm(", "library": "var_list.r", "varRefreshCmd": "cat(var_dic_list()) " } }, "types_to_exclude": [ "module", "function", "builtin_function_or_method", "instance", "_Feature" ], "window_display": false } }, "nbformat": 4, "nbformat_minor": 2 }