{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "##### 교재페이지 (337-338)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "11) RNN with long sequences : Dynamic RNN"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From <ipython-input-5-652e55b71769>:21: BasicLSTMCell.__init__ (from tensorflow.python.ops.rnn_cell_impl) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "This class is equivalent as tf.keras.layers.LSTMCell, and will be replaced by that in Tensorflow 2.0.\n",
      "(3, 7, 7)\n",
      "array([[[1., 0., 0., 0., 0., 0., 0.],\n",
      "        [0., 1., 0., 0., 0., 0., 0.],\n",
      "        [0., 0., 1., 0., 0., 0., 0.],\n",
      "        [0., 0., 0., 1., 0., 0., 0.],\n",
      "        [0., 0., 0., 0., 1., 0., 0.],\n",
      "        [0., 0., 0., 0., 0., 1., 0.],\n",
      "        [0., 0., 0., 0., 0., 0., 1.]],\n",
      "\n",
      "       [[0., 1., 0., 0., 0., 0., 0.],\n",
      "        [0., 0., 1., 0., 0., 0., 0.],\n",
      "        [0., 0., 0., 1., 0., 0., 0.],\n",
      "        [0., 0., 0., 0., 1., 0., 0.],\n",
      "        [0., 0., 0., 0., 0., 1., 0.],\n",
      "        [0., 0., 0., 0., 0., 0., 1.],\n",
      "        [1., 0., 0., 0., 0., 0., 0.]],\n",
      "\n",
      "       [[0., 0., 0., 1., 0., 0., 0.],\n",
      "        [0., 0., 0., 0., 1., 0., 0.],\n",
      "        [0., 0., 0., 0., 0., 1., 0.],\n",
      "        [0., 0., 0., 0., 0., 0., 1.],\n",
      "        [1., 0., 0., 0., 0., 0., 0.],\n",
      "        [0., 1., 0., 0., 0., 0., 0.],\n",
      "        [0., 0., 1., 0., 0., 0., 0.]]], dtype=float32)\n",
      "WARNING:tensorflow:From <ipython-input-5-652e55b71769>:27: dynamic_rnn (from tensorflow.python.ops.rnn) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `keras.layers.RNN(cell)`, which is equivalent to this API\n",
      "WARNING:tensorflow:From C:\\Users\\jsdata00010\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\rnn.py:626: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "WARNING:tensorflow:From C:\\Users\\jsdata00010\\Anaconda3\\lib\\site-packages\\tensorflow\\python\\ops\\tensor_array_ops.py:162: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "array([[[ 0.00508039,  0.07126787],\n",
      "        [ 0.14738694, -0.00765729],\n",
      "        [ 0.21068951,  0.07178447],\n",
      "        [ 0.08762835, -0.02899389],\n",
      "        [ 0.04239224, -0.03299976],\n",
      "        [ 0.06962248,  0.01722379],\n",
      "        [ 0.02550831,  0.03635924]],\n",
      "\n",
      "       [[ 0.13887739, -0.04612161],\n",
      "        [ 0.20217769,  0.04597978],\n",
      "        [ 0.07956441, -0.0525011 ],\n",
      "        [ 0.        ,  0.        ],\n",
      "        [ 0.        ,  0.        ],\n",
      "        [ 0.        ,  0.        ],\n",
      "        [ 0.        ,  0.        ]],\n",
      "\n",
      "       [[-0.0606548 , -0.06562112],\n",
      "        [-0.04055252, -0.0350806 ],\n",
      "        [-0.00440498,  0.0213261 ],\n",
      "        [-0.02605668,  0.04391894],\n",
      "        [ 0.        ,  0.        ],\n",
      "        [ 0.        ,  0.        ],\n",
      "        [ 0.        ,  0.        ]]], dtype=float32)\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "import numpy as np\n",
    "from tensorflow.contrib import rnn\n",
    "import pprint\n",
    "pp = pprint.PrettyPrinter(indent=4)\n",
    "sess = tf.InteractiveSession()\n",
    "# One hot encoding for each char in 'matchup'\n",
    "m = [1, 0, 0, 0, 0, 0, 0]\n",
    "a = [0, 1, 0, 0, 0, 0, 0]\n",
    "t = [0, 0, 1, 0, 0, 0, 0]\n",
    "c = [0, 0, 0, 1, 0, 0, 0]\n",
    "h = [0, 0, 0, 0, 1, 0, 0]\n",
    "u = [0, 0, 0, 0, 0, 1, 0]\n",
    "p = [0, 0, 0, 0, 0, 0, 1]\n",
    "\n",
    "\n",
    "# One cell RNN input_dim (4) -> output_dim (2). sequence: 5\n",
    "hidden_size = 2\n",
    "sequence_length = 7\n",
    "batch = 3\n",
    "cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)\n",
    "x_data = np.array([[m,a,t,c,h,u,p],\n",
    "                  [a,t,c,h,u,p,m],\n",
    "                  [c,h,u,p,m,a,t]], dtype=np.float32)\n",
    "print(x_data.shape)\n",
    "pp.pprint(x_data)\n",
    "outputs, _states = tf.nn.dynamic_rnn(cell, x_data, sequence_length=[7,3,4], dtype=tf.float32)\n",
    "sess.run(tf.global_variables_initializer())\n",
    "pp.pprint(outputs.eval())"
   ]
  }
 ],
 "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.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
