daily pastebin goal
29%
SHARE
TWEET

Untitled

a guest May 17th, 2018 107 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. {
  2.  "cells": [
  3.   {
  4.    "cell_type": "code",
  5.    "execution_count": 1,
  6.    "metadata": {},
  7.    "outputs": [
  8.     {
  9.      "name": "stderr",
  10.      "output_type": "stream",
  11.      "text": [
  12.       "d:\\anaconda3\\lib\\site-packages\\h5py\\__init__.py:34: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.\n",
  13.       "  from ._conv import register_converters as _register_converters\n"
  14.      ]
  15.     }
  16.    ],
  17.    "source": [
  18.     "import tensorflow as tf\n",
  19.     "import numpy as np\n",
  20.     "import os\n",
  21.     "\n",
  22.     "os.chdir('D:/myworks/deeplearning/DeepLearningZeroToAll-master')"
  23.    ]
  24.   },
  25.   {
  26.    "cell_type": "code",
  27.    "execution_count": 2,
  28.    "metadata": {},
  29.    "outputs": [],
  30.    "source": [
  31.     "tf.enable_eager_execution()"
  32.    ]
  33.   },
  34.   {
  35.    "cell_type": "code",
  36.    "execution_count": 8,
  37.    "metadata": {},
  38.    "outputs": [],
  39.    "source": [
  40.     "num=tf.constant([[5]])"
  41.    ]
  42.   },
  43.   {
  44.    "cell_type": "code",
  45.    "execution_count": 11,
  46.    "metadata": {},
  47.    "outputs": [
  48.     {
  49.      "name": "stdout",
  50.      "output_type": "stream",
  51.      "text": [
  52.       "hello, [[4.]]\n"
  53.      ]
  54.     }
  55.    ],
  56.    "source": [
  57.     "x = [[2.]]\n",
  58.     "m = tf.matmul(x, x)\n",
  59.     "print(\"hello, {}\".format(m))"
  60.    ]
  61.   },
  62.   {
  63.    "cell_type": "code",
  64.    "execution_count": 9,
  65.    "metadata": {},
  66.    "outputs": [
  67.     {
  68.      "data": {
  69.       "text/plain": [
  70.        "<tf.Tensor: id=5, shape=(1, 1), dtype=int32, numpy=array([[25]])>"
  71.       ]
  72.      },
  73.      "execution_count": 9,
  74.      "metadata": {},
  75.      "output_type": "execute_result"
  76.     }
  77.    ],
  78.    "source": [
  79.     "tf.matmul(num,num)"
  80.    ]
  81.   },
  82.   {
  83.    "cell_type": "code",
  84.    "execution_count": 2,
  85.    "metadata": {},
  86.    "outputs": [],
  87.    "source": [
  88.     "h=[1,0,0,0]\n",
  89.     "e=[0,1,0,0]\n",
  90.     "l=[0,0,1,0]\n",
  91.     "o=[0,0,0,1]"
  92.    ]
  93.   },
  94.   {
  95.    "cell_type": "code",
  96.    "execution_count": 3,
  97.    "metadata": {},
  98.    "outputs": [
  99.     {
  100.      "name": "stdout",
  101.      "output_type": "stream",
  102.      "text": [
  103.       "WARNING:tensorflow:From d:\\anaconda3\\lib\\site-packages\\tensorflow\\contrib\\learn\\python\\learn\\datasets\\base.py:198: retry (from tensorflow.contrib.learn.python.learn.datasets.base) is deprecated and will be removed in a future version.\n",
  104.       "Instructions for updating:\n",
  105.       "Use the retry module or similar alternatives.\n"
  106.      ]
  107.     }
  108.    ],
  109.    "source": [
  110.     "hidden_size=2\n",
  111.     "\n",
  112.     "cell=tf.contrib.rnn.BasicRNNCell(num_units=hidden_size)"
  113.    ]
  114.   },
  115.   {
  116.    "cell_type": "code",
  117.    "execution_count": 9,
  118.    "metadata": {},
  119.    "outputs": [],
  120.    "source": [
  121.     "x_data=np.array([[[1,0,0,0]]],dtype=np.float32)\n"
  122.    ]
  123.   },
  124.   {
  125.    "cell_type": "code",
  126.    "execution_count": 17,
  127.    "metadata": {},
  128.    "outputs": [],
  129.    "source": [
  130.     "\n",
  131.     "tf.set_random_seed(777)  # reproducibility\n",
  132.     "\n",
  133.     "idx2char = ['h', 'i', 'e', 'l', 'o']\n",
  134.     "# Teach hello: hihell -> ihello\n",
  135.     "x_data = [[0, 1, 0, 2, 3, 3]]   # hihell\n",
  136.     "x_one_hot = [[[1, 0, 0, 0, 0],   # h 0\n",
  137.     "              [0, 1, 0, 0, 0],   # i 1\n",
  138.     "              [1, 0, 0, 0, 0],   # h 0\n",
  139.     "              [0, 0, 1, 0, 0],   # e 2\n",
  140.     "              [0, 0, 0, 1, 0],   # l 3\n",
  141.     "              [0, 0, 0, 1, 0]]]  # l 3\n",
  142.     "\n",
  143.     "y_data = [[1, 0, 2, 3, 3, 4]]    # ihello\n",
  144.     "\n",
  145.     "num_classes = 5\n",
  146.     "input_dim = 5  # one-hot size\n",
  147.     "hidden_size = 5  # output from the LSTM. 5 to directly predict one-hot\n",
  148.     "batch_size = 1   # one sentence\n",
  149.     "sequence_length = 6  # |ihello| == 6\n",
  150.     "learning_rate = 0.1\n"
  151.    ]
  152.   },
  153.   {
  154.    "cell_type": "code",
  155.    "execution_count": 18,
  156.    "metadata": {},
  157.    "outputs": [],
  158.    "source": [
  159.     "# Lab 12 RNN\n",
  160.     "\n",
  161.     "X = tf.placeholder(\n",
  162.     "    tf.float32, [None, sequence_length, input_dim])  # X one-hot\n",
  163.     "Y = tf.placeholder(tf.int32, [None, sequence_length])  # Y label\n",
  164.     "\n",
  165.     "cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)\n",
  166.     "initial_state = cell.zero_state(batch_size, tf.float32)\n",
  167.     "outputs, _states = tf.nn.dynamic_rnn(\n",
  168.     "    cell, X, initial_state=initial_state, dtype=tf.float32)\n"
  169.    ]
  170.   },
  171.   {
  172.    "cell_type": "code",
  173.    "execution_count": null,
  174.    "metadata": {},
  175.    "outputs": [],
  176.    "source": [
  177.     "# Lab 12 RNN\n",
  178.     "\n",
  179.     "X = tf.placeholder(\n",
  180.     "    tf.float32, [None, sequence_length, input_dim])  # X one-hot\n",
  181.     "Y = tf.placeholder(tf.int32, [None, sequence_length])  # Y label\n",
  182.     "\n",
  183.     "cell = tf.contrib.rnn.BasicLSTMCell(num_units=hidden_size, state_is_tuple=True)\n",
  184.     "initial_state = cell.zero_state(batch_size, tf.float32)\n",
  185.     "outputs, _states = tf.nn.dynamic_rnn(\n",
  186.     "    cell, X, initial_state=initial_state, dtype=tf.float32)\n",
  187.     "\n",
  188.     "# FC layer\n",
  189.     "X_for_fc = tf.reshape(outputs, [-1, hidden_size])\n",
  190.     "# fc_w = tf.get_variable(\"fc_w\", [hidden_size, num_classes])\n",
  191.     "# fc_b = tf.get_variable(\"fc_b\", [num_classes])\n",
  192.     "# outputs = tf.matmul(X_for_fc, fc_w) + fc_b\n",
  193.     "outputs = tf.contrib.layers.fully_connected(\n",
  194.     "    inputs=X_for_fc, num_outputs=num_classes, activation_fn=None)\n",
  195.     "\n",
  196.     "# reshape out for sequence_loss\n",
  197.     "outputs = tf.reshape(outputs, [batch_size, sequence_length, num_classes])\n",
  198.     "\n",
  199.     "weights = tf.ones([batch_size, sequence_length])\n",
  200.     "sequence_loss = tf.contrib.seq2seq.sequence_loss(\n",
  201.     "    logits=outputs, targets=Y, weights=weights)\n",
  202.     "loss = tf.reduce_mean(sequence_loss)\n",
  203.     "train = tf.train.AdamOptimizer(learning_rate=learning_rate).minimize(loss)\n",
  204.     "\n",
  205.     "prediction = tf.argmax(outputs, axis=2)\n",
  206.     "\n",
  207.     "with tf.Session() as sess:\n",
  208.     "    sess.run(tf.global_variables_initializer())\n",
  209.     "    for i in range(50):\n",
  210.     "        l, _ = sess.run([loss, train], feed_dict={X: x_one_hot, Y: y_data})\n",
  211.     "        result = sess.run(prediction, feed_dict={X: x_one_hot})\n",
  212.     "        print(i, \"loss:\", l, \"prediction: \", result, \"true Y: \", y_data)\n",
  213.     "\n",
  214.     "        # print char using dic\n",
  215.     "        result_str = [idx2char[c] for c in np.squeeze(result)]\n",
  216.     "        print(\"\\tPrediction str: \", ''.join(result_str))\n"
  217.    ]
  218.   },
  219.   {
  220.    "cell_type": "code",
  221.    "execution_count": null,
  222.    "metadata": {},
  223.    "outputs": [],
  224.    "source": []
  225.   },
  226.   {
  227.    "cell_type": "code",
  228.    "execution_count": null,
  229.    "metadata": {},
  230.    "outputs": [],
  231.    "source": []
  232.   },
  233.   {
  234.    "cell_type": "code",
  235.    "execution_count": null,
  236.    "metadata": {},
  237.    "outputs": [],
  238.    "source": []
  239.   },
  240.   {
  241.    "cell_type": "code",
  242.    "execution_count": null,
  243.    "metadata": {},
  244.    "outputs": [],
  245.    "source": []
  246.   }
  247.  ],
  248.  "metadata": {
  249.   "kernelspec": {
  250.    "display_name": "Python 3",
  251.    "language": "python",
  252.    "name": "python3"
  253.   },
  254.   "language_info": {
  255.    "codemirror_mode": {
  256.     "name": "ipython",
  257.     "version": 3
  258.    },
  259.    "file_extension": ".py",
  260.    "mimetype": "text/x-python",
  261.    "name": "python",
  262.    "nbconvert_exporter": "python",
  263.    "pygments_lexer": "ipython3",
  264.    "version": "3.6.4"
  265.   }
  266.  },
  267.  "nbformat": 4,
  268.  "nbformat_minor": 2
  269. }
RAW Paste Data
Top