Last active
November 18, 2016 12:13
-
-
Save justheuristic/d827f4978581b1e316e8ebb86bea1783 to your computer and use it in GitHub Desktop.
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 tensorflow as tf" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "config = tf.ConfigProto()\n", | |
| "config.log_device_placement = False\n", | |
| "config.gpu_options.allow_growth = True\n", | |
| "s = tf.InteractiveSession(config=config)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "input_size = 256\n", | |
| "vocab_size = 10**5\n", | |
| "batch_size = 100" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "x = tf.get_variable(\"logits\",initializer=tf.random_normal([batch_size,input_size]))\n", | |
| "y = tf.get_variable(\"answers\",initializer=tf.range(batch_size)[:,None])\n", | |
| "w = tf.get_variable(\"weights\",initializer=tf.ones((vocab_size,input_size)))\n", | |
| "b = tf.get_variable(\"bias\",initializer=tf.ones((vocab_size,)))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "s.run(tf.initialize_all_variables())" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### sampled logsoftmax" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "sampled_loss = tf.nn.sampled_softmax_loss(w,b,x,y,num_sampled=100,num_classes=vocab_size)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1000 loops, best of 3: 1.15 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit -n1000 s.run(sampled_loss)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "markdown", | |
| "metadata": {}, | |
| "source": [ | |
| "### regular logsoftmax" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [], | |
| "source": [ | |
| "logits = tf.matmul(x,tf.transpose(w)) +b\n", | |
| "regular_loss = tf.nn.softmax_cross_entropy_with_logits(logits,\n", | |
| " tf.one_hot(y,vocab_size))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 9, | |
| "metadata": { | |
| "collapsed": false | |
| }, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "1000 loops, best of 3: 18.4 ms per loop\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "%timeit -n1000 s.run(regular_loss)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": { | |
| "collapsed": true | |
| }, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python [Root]", | |
| "language": "python", | |
| "name": "Python [Root]" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 2 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython2", | |
| "version": "2.7.12" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 0 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment