{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## TF summary\n",
    "- Serializing the data\n",
    "    - add summary operatons to the graph\n",
    "    - run summary op\n",
    "    - write summaries to file\n",
    "- Launching TensorBoard"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "loss_holder = tf.placeholder(tf.float32,name='loss')\n",
    "scale_sum = tf.summary.scalar('loss',loss_holder)\n",
    "#scale_sum = tf.summary.histogram('his',loss_holder)\n",
    "#print scale_sum"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 0\n",
      "1 1\n",
      "2 2\n",
      "3 3\n",
      "4 4\n",
      "5 5\n",
      "6 6\n",
      "7 7\n",
      "8 8\n",
      "9 9\n",
      "10 10\n",
      "11 11\n",
      "12 12\n",
      "13 13\n",
      "14 14\n",
      "15 15\n",
      "16 16\n",
      "17 17\n",
      "18 18\n",
      "19 19\n",
      "20 20\n",
      "21 21\n",
      "22 22\n",
      "23 23\n",
      "24 24\n",
      "25 25\n",
      "26 26\n",
      "27 27\n",
      "28 28\n",
      "29 29\n",
      "30 30\n",
      "31 31\n",
      "32 32\n",
      "33 33\n",
      "34 34\n",
      "35 35\n",
      "36 36\n",
      "37 37\n",
      "38 38\n",
      "39 39\n",
      "40 40\n",
      "41 41\n",
      "42 42\n",
      "43 43\n",
      "44 44\n",
      "45 45\n",
      "46 46\n",
      "47 47\n",
      "48 48\n",
      "49 49\n",
      "50 50\n",
      "51 51\n",
      "52 52\n",
      "53 53\n",
      "54 54\n",
      "55 55\n",
      "56 56\n",
      "57 57\n",
      "58 58\n",
      "59 59\n",
      "60 60\n",
      "61 61\n",
      "62 62\n",
      "63 63\n",
      "64 64\n",
      "65 65\n",
      "66 66\n",
      "67 67\n",
      "68 68\n",
      "69 69\n",
      "70 70\n",
      "71 71\n",
      "72 72\n",
      "73 73\n",
      "74 74\n",
      "75 75\n",
      "76 76\n",
      "77 77\n",
      "78 78\n",
      "79 79\n",
      "80 80\n",
      "81 81\n",
      "82 82\n",
      "83 83\n",
      "84 84\n",
      "85 85\n",
      "86 86\n",
      "87 87\n",
      "88 88\n",
      "89 89\n",
      "90 90\n",
      "91 91\n",
      "92 92\n",
      "93 93\n",
      "94 94\n",
      "95 95\n",
      "96 96\n",
      "97 97\n",
      "98 98\n",
      "99 99\n"
     ]
    }
   ],
   "source": [
    "losses = range(100)\n",
    "sess = tf.Session() \n",
    "summary = tf.summary.FileWriter('summary')\n",
    "for i,l in enumerate(losses):\n",
    "    print i,l\n",
    "    s = sess.run(scale_sum,feed_dict={loss_holder:l})\n",
    "    summary.add_summary(s,i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "k = tf.placeholder(tf.float32)\n",
    "\n",
    "# Make a normal distribution, with a shifting mean\n",
    "mean_moving_normal = tf.random_normal(shape=[1000], mean=(5*k), stddev=1)\n",
    "# Record that distribution into a histogram summary\n",
    "tf.summary.histogram(\"normal/moving_mean\", mean_moving_normal)\n",
    "\n",
    "# Setup a session and summary writer\n",
    "sess = tf.Session()\n",
    "writer = tf.summary.FileWriter(\"summary\")\n",
    "\n",
    "summaries = tf.summary.merge_all()\n",
    "\n",
    "# Setup a loop and write the summaries to disk\n",
    "N = 400\n",
    "for step in range(N):\n",
    "  k_val = step/float(N)\n",
    "  summ = sess.run(summaries, feed_dict={k: k_val})\n",
    "  writer.add_summary(summ, global_step=step)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 55,
   "metadata": {},
   "outputs": [],
   "source": [
    "tf.reset_default_graph()\n",
    "\n",
    "filename = 'test.png'\n",
    "image_raw_data = tf.gfile.FastGFile(filename).read()\n",
    "image = tf.image.decode_jpeg(image_raw_data)\n",
    "tf.summary.image(\"image\", tf.expand_dims(image,axis=0))\n",
    "\n",
    "# Setup a session and summary writer\n",
    "sess = tf.Session()\n",
    "writer = tf.summary.FileWriter(\"summary\")\n",
    "\n",
    "summaries = tf.summary.merge_all()\n",
    "\n",
    "# Setup a loop and write the summaries to disk\n",
    "\n",
    "summ = sess.run(summaries)\n",
    "writer.add_summary(summ, global_step=1000)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 2",
   "language": "python",
   "name": "python2"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
