-
-
Save brennenhuang/416e23f66c000d3828d0d4730c5f9a0a to your computer and use it in GitHub Desktop.
How to restore CUDNNLSTM of TensorFlow at CPU device? So that it could be used in GPU, CPU or Mobile
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": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import tensorflow as tf\n", | |
| "import numpy as np\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "num_layers = 1\n", | |
| "num_units = 128\n", | |
| "direction = \"bidirectional\"" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "INFO:tensorflow:Restoring parameters from cudnn_cpu_bi_trans/model_1\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "inputs = tf.placeholder(tf.float32, [None, None, 32], name=\"inputs\")\n", | |
| "with tf.variable_scope(\"cudnn_lstm\"):\n", | |
| " single_cell = lambda: tf.contrib.cudnn_rnn.CudnnCompatibleLSTMCell(num_units)\n", | |
| " cells_fw = [single_cell() for _ in range(num_layers)]\n", | |
| " cells_bw = [single_cell() for _ in range(num_layers)]\n", | |
| " # Leave the scope arg unset.\n", | |
| " (outputs, output_state_fw,\n", | |
| " output_state_bw) = tf.contrib.rnn.stack_bidirectional_dynamic_rnn(cells_fw, cells_bw, inputs,dtype=tf.float32)\n", | |
| "\n", | |
| " saver = tf.train.Saver()\n", | |
| "# Create session\n", | |
| "sess = tf.Session()\n", | |
| "# Restores\n", | |
| "saver.restore(sess, 'cudnn_cpu_bi_trans/model_1')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "out = sess.run(outputs, {inputs: np.zeros([10,100,32])})" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "(10, 100, 256)" | |
| ] | |
| }, | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "out.su" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python (tenosflow)", | |
| "language": "python", | |
| "name": "tensorflow" | |
| }, | |
| "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.6.4" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment