{ "nbformat": 4, "nbformat_minor": 0, "metadata": { "colab": { "name": "RNN.ipynb", "version": "0.3.2", "provenance": [], "collapsed_sections": [], "include_colab_link": true }, "kernelspec": { "name": "python3", "display_name": "Python 3" } }, "cells": [ { "cell_type": "markdown", "metadata": { "id": "view-in-github", "colab_type": "text" }, "source": [ "\"Open" ] }, { "cell_type": "code", "metadata": { "id": "AohgMkPBeTer", "colab_type": "code", "outputId": "c288b4f5-7d66-4969-c1fe-4020947adaab", "colab": { "base_uri": "https://localhost:8080/", "height": 34 } }, "source": [ "import numpy as np\n", "import tensorflow as tf\n", "print(tf.__version__)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "1.13.1\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "PxS9xvyaaRF6", "colab_type": "text" }, "source": [ "### RNN을 직접 코딩하기" ] }, { "cell_type": "code", "metadata": { "id": "FHU2gUGleYRj", "colab_type": "code", "colab": {} }, "source": [ "n_inputs = 3\n", "n_neurons = 5" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "jdxT9O9wXUoI", "colab_type": "code", "outputId": "cd208090-d2f6-4d49-f839-24a55910389c", "colab": { "base_uri": "https://localhost:8080/", "height": 88 } }, "source": [ "ㅊ\n", "\n", "Wx = tf.Variable(tf.random_normal(shape=[n_inputs, n_neurons], dtype=tf.float32))\n", "Wy = tf.Variable(tf.random_normal(shape=[n_neurons, n_neurons], dtype=tf.float32))\n", "b = tf.Variable(tf.zeros([1, n_neurons], dtype=tf.float32))\n", "\n", "Y0 = tf.tanh(tf.matmul(X0, Wx) + b)\n", "Y1 = tf.tanh(tf.matmul(Y0, Wy) + tf.matmul(X1, Wx) + b)\n", "\n", "init=tf.global_variables_initializer()" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/framework/op_def_library.py:263: 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" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "KAbWkf1xYJOM", "colab_type": "code", "colab": {} }, "source": [ "X0_batch = np.array(\n", " [[0, 1, 2],\n", " [3, 4, 5],\n", " [6, 7, 8],\n", " [9, 0, 1]]\n", ")\n", "\n", "X1_batch = np.array(\n", " [[9, 8, 7],\n", " [0, 0, 0],\n", " [6, 5, 4],\n", " [3, 2, 1]]\n", ")\n", "\n", "with tf.Session() as sess:\n", " init.run()\n", " Y0_val, Y1_val = sess.run([Y0, Y1], feed_dict={X0:X0_batch, X1:X1_batch})" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "gWqbbCprZ8DA", "colab_type": "code", "outputId": "58507474-5fe9-4def-f764-64e093f08e82", "colab": { "base_uri": "https://localhost:8080/", "height": 85 } }, "source": [ "print(Y0_val)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[[-0.9999428 0.8142273 -0.89699703 -0.51680404 0.994335 ]\n", " [-1. 0.96428037 -0.46683785 -0.9999996 0.999998 ]\n", " [-1. 0.99356204 0.4174478 -1. 1. ]\n", " [-0.9979181 0.99981457 0.98542804 -1. -0.99998975]]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "Mx9OXfBfaCAQ", "colab_type": "code", "outputId": "1eee7853-87ce-47cf-c022-fa70f9a0cc25", "colab": { "base_uri": "https://localhost:8080/", "height": 85 } }, "source": [ "print(Y1_val)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[[-1. -0.9072738 0.9998315 -1. 1. ]\n", " [-0.03871869 -0.9949366 0.5307218 0.9179147 0.99971414]\n", " [-1. -0.98553795 0.99953645 -1. 1. ]\n", " [-1. -0.13850287 0.8804973 -0.99971765 0.9999961 ]]\n" ], "name": "stdout" } ] }, { "cell_type": "markdown", "metadata": { "id": "7FCdab2vaYcI", "colab_type": "text" }, "source": [ "### Tensorflow를 활용한 RNN 코딩하기" ] }, { "cell_type": "code", "metadata": { "id": "0Q8UGuN0agG5", "colab_type": "code", "colab": {} }, "source": [ "X0 = tf.placeholder(tf.float32, [None, n_inputs])\n", "X1 = tf.placeholder(tf.float32, [None, n_inputs])\n", "\n", "basic_rnn = tf.contrib.rnn.BasicRNNCell(num_units=n_neurons, reuse=True)\n", "output_seqs, states = tf.contrib.rnn.static_rnn(basic_rnn, [X0, X1], dtype=tf.float32)\n", "\n", "init=tf.global_variables_initializer()\n", "\n", "with tf.Session() as sess:\n", " init.run()\n", " output_seqs, states = sess.run([output_seqs, states], feed_dict={X0:X0_batch, X1:X1_batch})" ], "execution_count": 0, "outputs": [] }, { "cell_type": "code", "metadata": { "id": "K45Ku3cVbJ6o", "colab_type": "code", "outputId": "4a15e021-97fb-42ca-a448-22b43f7487d4", "colab": { "base_uri": "https://localhost:8080/", "height": 170 } }, "source": [ "print(output_seqs)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[array([[-0.11086969, -0.8644573 , 0.8264698 , -0.6449973 , 0.3823741 ],\n", " [ 0.7227251 , -0.9990468 , 0.993787 , -0.99651307, 0.64120334],\n", " [ 0.9593735 , -0.9999937 , 0.9997956 , -0.99997175, 0.80672956],\n", " [ 0.9981162 , 0.46572423, -0.538953 , -0.999847 , -0.99559104]],\n", " dtype=float32), array([[ 0.99731344, -0.9999985 , 0.99958646, -0.99999034, 0.9126751 ],\n", " [-0.17122112, -0.6988485 , -0.05396171, -0.03301296, 0.93879026],\n", " [ 0.94880253, -0.99978197, 0.96938735, -0.9995523 , 0.9801305 ],\n", " [ 0.94319993, -0.4077534 , 0.02760991, -0.97863674, -0.26508206]],\n", " dtype=float32)]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "prr6dy-OeKp9", "colab_type": "code", "outputId": "c9afaea4-65d3-4786-dece-81b2e6dcbb7c", "colab": { "base_uri": "https://localhost:8080/", "height": 85 } }, "source": [ "print(states)" ], "execution_count": 0, "outputs": [ { "output_type": "stream", "text": [ "[[ 0.99731344 -0.9999985 0.99958646 -0.99999034 0.9126751 ]\n", " [-0.17122112 -0.6988485 -0.05396171 -0.03301296 0.93879026]\n", " [ 0.94880253 -0.99978197 0.96938735 -0.9995523 0.9801305 ]\n", " [ 0.94319993 -0.4077534 0.02760991 -0.97863674 -0.26508206]]\n" ], "name": "stdout" } ] }, { "cell_type": "code", "metadata": { "id": "AsIunWIefdPs", "colab_type": "code", "colab": {} }, "source": [ "" ], "execution_count": 0, "outputs": [] } ] }