{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import tensorflow as tf"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Data management\n",
    "- tf.placeholder\n",
    "- tf.FIFOQueue\n",
    "- tf.data"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ```tf.placeholder```"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ```tf.FIFOQueue```\n",
    "- It's also operation in tensorflow\n",
    "- first-in, first-out queue\n",
    "\n",
    "\n",
    "\n",
    "------------\n",
    "1. [An introduction to TensorFLow queuing and threading](http://adventuresinmachinelearning.com/introduction-tensorflow-queuing/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The FIFOQueue - first in, first out\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[None, 1.0]\n",
      "[None, 1.0]\n",
      "[None, 1.0]\n",
      "[None, 2.0]\n",
      "[None, 2.0]\n",
      "[None, 2.0]\n",
      "[None, 3.0]\n",
      "[None, 3.0]\n",
      "[None, 3.0]\n",
      "[None, 4.0]\n",
      "[None, 4.0]\n"
     ]
    }
   ],
   "source": [
    "def first_fifo():\n",
    "    q = tf.FIFOQueue(3,\"float\")\n",
    "    #init = q.enqueue_many(([0.,0.,0.],[5.,5.,5.]))\n",
    "    init = q.enqueue_many(tf.convert_to_tensor([0.,0.,0.],dtype=\"float\"))\n",
    "    x = q.dequeue()\n",
    "    y = x+1\n",
    "    q_inc = q.enqueue([y])\n",
    "    with tf.Session() as sess:\n",
    "        sess.run(init)\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "        print(sess.run([q_inc,y]))\n",
    "first_fifo()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fifo_queue_no_coord():\n",
    "    dummpy_input = tf.random_normal([3],mean=0,stddev=1)\n",
    "    dummpy_input = tf.Print(dummpy_input, data=[dummpy_input],\n",
    "                                message=\"New dummpy inputs have been created: \", summarize=6)\n",
    "\n",
    "    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)\n",
    "    enqueue_op = q.enqueue_many(dummpy_input)\n",
    "    data = q.dequeue()\n",
    "    data = tf.Print(data,data=[q.size()],message=\"This is how many in q: \")\n",
    "    fg = data + 1\n",
    "    with tf.Session() as sess:\n",
    "        sess.run(enqueue_op)\n",
    "        print(sess.run(fg))\n",
    "        print(sess.run(fg))\n",
    "        print(sess.run(fg))\n",
    "        print(sess.run(fg))\n",
    "        print(\"Can not run here!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#fifo_queue_no_coord()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "\n",
    "## QueueRunners and the Coordinator"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "def fifo_queue_with_coord():\n",
    "    dummpy_input = tf.random_normal([3],mean=0,stddev=1)\n",
    "    dummpy_input = tf.Print(dummpy_input, data=[dummpy_input],\n",
    "                                message=\"New dummpy inputs have been created: \", summarize=6)\n",
    "\n",
    "    q = tf.FIFOQueue(capacity=3, dtypes=tf.float32)\n",
    "    enqueue_op = q.enqueue_many(dummpy_input)\n",
    "    qr = tf.train.QueueRunner(q,[enqueue_op]*1)\n",
    "    tf.train.add_queue_runner(qr)\n",
    "    data = q.dequeue()\n",
    "    data = tf.Print(data,data=[q.size()],message=\"This is how many in q: \")\n",
    "    fg = data + 1\n",
    "    with tf.Session() as sess:\n",
    "        coord = tf.train.Coordinator()\n",
    "        threads = tf.train.start_queue_runners(coord=coord)\n",
    "        sess.run(enqueue_op)\n",
    "        sess.run(fg)\n",
    "        sess.run(fg)\n",
    "        sess.run(fg)\n",
    "        sess.run(fg)\n",
    "        print(\"We run here!\")\n",
    "        coord.request_stop()\n",
    "        coord.join(threads)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "fifo_queue_with_coord()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# ```tf.data```\n",
    "The TensorFlow Dataset framework has two main componets:\n",
    "- The Dataset: where the data resides\n",
    "- An associated Iterator: to get data\n",
    "\n",
    "Once you've loaded the data into the Dataset object, you can string together various oprations to apply to the data, such as:\n",
    "- batch()\n",
    "- map()\n",
    "- repeat()\n",
    "- shuffle()"
   ]
  },
  {
   "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
}
