Created
August 6, 2016 04:17
-
-
Save p-baleine/9eedf5c5a6245ec0e87d662abeb67c0b to your computer and use it in GitHub Desktop.
Dynamic Recurrent Neural Network using tf.nn.dynamic_rnn
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import numpy as np\n", | |
"import tensorflow as tf" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Input size\n", | |
"nin = 100\n", | |
"# Hidden layer num\n", | |
"n_hidden = 256\n", | |
"# Sequence max length\n", | |
"seq_max_len = 30" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# #batch_size * #max_seq_len * #inputsize\n", | |
"input = tf.placeholder(tf.float32, [None, seq_max_len, nin])\n", | |
"# #batch_size\n", | |
"sequence_length = tf.placeholder(tf.int32, [None])\n", | |
"\n", | |
"batch_size = tf.shape(input)[0]\n", | |
"\n", | |
"cell = tf.nn.rnn_cell.GRUCell(n_hidden)\n", | |
"initial_state = cell.zero_state(batch_size, dtype=tf.float32)\n", | |
"\n", | |
"output, state = tf.nn.dynamic_rnn(\n", | |
" cell, input, initial_state=initial_state,\n", | |
" sequence_length=sequence_length)\n", | |
"\n", | |
"# Retrieve the last output\n", | |
"index = tf.range(0, batch_size) * seq_max_len + (sequence_length - 1)\n", | |
"output = tf.gather(tf.reshape(output, [-1, n_hidden]), index)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Sequence length\n", | |
"seqlen = np.random.permutation(np.arange(1, 11))\n", | |
"# #batch_size * variable_length * #inputsize\n", | |
"x = [[np.random.randint(100, size=nin) / 100\n", | |
" for _ in range(l)] for l in seqlen]\n", | |
"# 0 pad, #batch_size * #max_seq_len * #inputsize\n", | |
"x = [_x + [[0.] * nin] * (seq_max_len - len(_x)) for _x in x]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"[[ 0.24374032 -0.26738089 0.19482653 ..., 0.06986387 0.63538063\n", | |
" -0.15972397]\n", | |
" [ 0.02840432 -0.16684723 0.11450367 ..., -0.04713225 0.27185273\n", | |
" -0.09963451]\n", | |
" [ 0.14622581 -0.19808987 0.29102004 ..., -0.01792343 0.49112913\n", | |
" -0.0518562 ]\n", | |
" ..., \n", | |
" [ 0.16123015 -0.3246842 0.17073596 ..., 0.12696315 0.40817925\n", | |
" 0.05533985]\n", | |
" [ 0.24779946 -0.25818256 0.24045026 ..., 0.08095235 0.45237231\n", | |
" -0.03921546]\n", | |
" [ 0.13250643 -0.14197701 0.18299626 ..., 0.09092216 0.27430093\n", | |
" -0.12567034]]\n", | |
"(10, 256)\n" | |
] | |
} | |
], | |
"source": [ | |
"init = tf.initialize_all_variables()\n", | |
"\n", | |
"with tf.Session() as sess:\n", | |
" sess.run(init)\n", | |
" out = sess.run(output, {input: x, sequence_length: seqlen})\n", | |
" print(out) # #batch_size * #n_hidden" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [] | |
} | |
], | |
"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.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment