Skip to content

Instantly share code, notes, and snippets.

@justheuristic
Last active November 18, 2016 12:13
Show Gist options
  • Select an option

  • Save justheuristic/d827f4978581b1e316e8ebb86bea1783 to your computer and use it in GitHub Desktop.

Select an option

Save justheuristic/d827f4978581b1e316e8ebb86bea1783 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Display the source blob
Display the rendered blob
Raw
{
"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": [
"intermediate_projection_size = 256\n",
"max_output_sequence_length = 50\n",
"dict_size = 10**5\n",
"batch_size = 100"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### mocking your code"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"class self:\n",
" #mock\n",
" num_sampled = 100\n",
" intermediate_projection = tf.get_variable(\"logits\",\n",
" #assuming shape [batch, time, unit] or vice versa\n",
" initializer=tf.random_normal([batch_size,\n",
" max_output_sequence_length,\n",
" intermediate_projection_size]))\n",
" targets = tf.get_variable(\"answers\",\n",
" initializer=tf.reshape(tf.range(batch_size*max_output_sequence_length),\n",
" [batch_size,max_output_sequence_length])\n",
" )\n",
"#the old code\n",
"with tf.variable_scope('output_projection'):\n",
" self.output_projection_weights = tf.get_variable('weights',\n",
" shape=[intermediate_projection_size, dict_size],\n",
" initializer=tf.uniform_unit_scaling_initializer())\n",
" self.output_projection_biases = tf.get_variable('biases',\n",
" shape=[dict_size],\n",
" initializer=tf.constant_initializer(0.0))\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### main"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"\n",
"with tf.name_scope('loss') as ns:\n",
" flat_intermediate_projection = tf.reshape(self.intermediate_projection,[-1,intermediate_projection_size])\n",
" flat_targets = tf.reshape(self.targets,[-1,1])\n",
" \n",
" flat_loss = tf.nn.sampled_softmax_loss(tf.transpose(self.output_projection_weights),\n",
" self.output_projection_biases,\n",
" flat_intermediate_projection,\n",
" flat_targets,\n",
" num_sampled = self.num_sampled,\n",
" num_classes = dict_size,\n",
" )\n",
" self.loss = tf.reshape(flat_loss,self.targets.get_shape())\n",
" "
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"100 loops, best of 3: 17.6 ms per loop\n"
]
}
],
"source": [
"s.run(tf.initialize_all_variables())\n",
"%timeit s.run(self.loss).shape"
]
},
{
"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