dnc-jupyter/free-recall/free-recall-rnn.ipynb

709 lines
107 KiB
Plaintext
Raw Permalink Normal View History

2017-03-01 22:29:54 +08:00
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Repeat Copy Task\n",
"### Differentiable Neural Computer (DNC) using a RNN Controller\n",
"\n",
"<a href=\"https://goo.gl/6eiJFc\"><img src=\"../static/dnc_schema.png\" alt=\"DNC schema\" style=\"width: 700px;\"/></a>\n",
"\n",
"**Sam Greydanus $\\cdot$ February 2017 $\\cdot$ MIT License.**\n",
"\n",
"Represents the state of the art in differentiable memory. Inspired by this [Nature paper](https://goo.gl/6eiJFc). Some ideas taken from [this Gihub repo](https://github.com/Mostafa-Samir/DNC-tensorflow)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Brain analogy"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
" However, there are interesting parallels between the memory mechanisms of a DNC and the functional capabilities of the mammalian hippocampus. DNC memory modification is fast and can be one-shot, resembling the associative long-term potentiation of hippocampal CA3 and CA1 synapses. The hippocampal dentate gyrus, a region known to support neurogenesis, has been proposed to increase representational sparsity, thereby enhancing memory capacity: usage- based memory allocation and sparse weightings may provide similar facilities in our model. Human 'free recall' experiments demonstrate the increased probability of item recall in the same order as first presented—a hippocampus-dependent phenomenon accounted for by the temporal context model, bearing some similarity to the formation of temporal links."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"import tensorflow as tf\n",
"import numpy as np\n",
"import sys\n",
"sys.path.insert(0, '../dnc')\n",
"\n",
"from dnc import DNC\n",
"from rnn_controller import RNNController\n",
"\n",
"import matplotlib.pyplot as plt\n",
"%matplotlib inline"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Hyperparameters"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"xydim = 6\n",
"tf.app.flags.DEFINE_integer(\"xlen\", xydim, \"Input dimension\")\n",
"tf.app.flags.DEFINE_integer(\"ylen\", xydim, \"output dimension\")\n",
"tf.app.flags.DEFINE_integer(\"length\", 5, \"Sequence length\")\n",
"tf.app.flags.DEFINE_integer(\"batch_size\", 3, \"Size of batch in minibatch gradient descent\")\n",
"\n",
"tf.app.flags.DEFINE_integer(\"R\", 1, \"Number of DNC read heads\")\n",
"tf.app.flags.DEFINE_integer(\"W\", 10, \"Word length for DNC memory\")\n",
"tf.app.flags.DEFINE_integer(\"N\", 7, \"Number of words the DNC memory can store\")\n",
"\n",
"tf.app.flags.DEFINE_integer(\"print_every\", 100, \"Print training info after this number of train steps\")\n",
"tf.app.flags.DEFINE_integer(\"iterations\", 30000, \"Number of training iterations\")\n",
"tf.app.flags.DEFINE_float(\"lr\", 1e-4, \"Learning rate (alpha) for the model\")\n",
"tf.app.flags.DEFINE_float(\"momentum\", .9, \"RMSProp momentum\")\n",
"tf.app.flags.DEFINE_integer(\"save_every\", 1000, \"Save model after this number of train steps\")\n",
"tf.app.flags.DEFINE_string(\"save_path\", \"rnn_models/model.ckpt\", \"Where to save checkpoints\")\n",
"FLAGS = tf.app.flags.FLAGS"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Data functions"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
2017-03-02 07:11:23 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAfMAAAD+CAYAAAAu5uwhAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAEZ9JREFUeJzt3X+snXVhx/H359buojghEUe76FSCOntr0BZ1zgBzhbGQ\niVuWIHcu8xcaRDfTLVEYGDeVyWagDpVkcfJDkRLc4oRsii24KTokUsH0glvYUFBaFBg1gq2l97s/\nzmm999Lenufcc/s83/b9Ss4f9+lznvPJt+c8n/N9znnOk1IKkiSpXmNtB5AkSQtjmUuSVDnLXJKk\nylnmkiRVzjKXJKlylrkkSZ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iCSJGk4/ja7JEmVs8wlSaqcZS5JUuUsc0mSKmeZS5JUOctckqTKWeaSJFXO\nMpckqXKWuSRJlbPMJUmqnGUuSVLlLHNJkipnmUuSVLkFXzVtIEnv1gHT0+e2HUGD6shzZo/Srav9\njo19uO0Is0xPn9d2hFnOn97ZdgQNqGsv9a5oMi7OzCVJqpxlLklS5SxzSZIqZ5lLklQ5y1ySpMpZ\n5pIkVc4ylySpcpa5JEmVs8wlSaqcZS5JUuUsc0mSKteozJO8P8n0nNtdixVOkiTt3zAXWtkMrAF2\n/wT8E6OLI0mSmhqmzJ8opfx45EkkSdJQhvnM/AVJfpjkf5JcneQ5I08lSZIG1rTMbwXeBJwKnA08\nH/hqksNHnEuSJA2o0WH2UsqNM/7cnOQ24PvAGcAVowwmSZIGM8xn5nuUUrYl+W/g2PnW27B2I+NH\njM9aNjG5gpWTEwt5eEmSDgqb108xtX72yWE7tu0Y+P4LKvMkT6dX5J+eb71T1p3M8lXLFvJQkiQd\ntFZOTjxpgrtl01Y+tXq
2017-03-01 22:29:54 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x10ad67690>"
2017-03-01 22:29:54 +08:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def get_sequence(length, dim):\n",
" X = np.concatenate((np.random.randint(2, size=(length,dim)), np.zeros((length + 3,dim))))\n",
" X = np.vstack(X) ; X[:,dim-1] = 0\n",
" \n",
" X = np.concatenate((X[-1:,:],X[:-1,:]))\n",
" y = np.concatenate((X[-(length + 2):,:],X[:-(length + 2),:]))\n",
" markers = range(length+1, X.shape[0], 2*length+3)\n",
" X[markers,dim-1] = 1\n",
" return X, y\n",
" \n",
"def next_batch(batch_size, length, dim):\n",
" X_batch = []\n",
" y_batch = []\n",
" for _ in range(batch_size):\n",
" X, y = get_sequence(length, dim)\n",
" X_batch.append(X) ; y_batch.append(y)\n",
" return [X_batch, y_batch]\n",
"\n",
"batch = next_batch(1, FLAGS.length, FLAGS.xlen)\n",
"plt.imshow(batch[0][0].T - batch[1][0].T, interpolation='none')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Helper functions"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def binary_cross_entropy(y, y_hat):\n",
" return tf.reduce_mean(-y*tf.log(y_hat) - (1-y)*tf.log(1-y_hat))\n",
"\n",
"def llprint(message):\n",
" sys.stdout.write(message)\n",
" sys.stdout.flush()"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def free_recall_loss(y, y_hat, tsteps): \n",
" # sorry this dimension stuff is uuuuugly but we have to because it's batched\n",
" y = tf.expand_dims(y, [1])\n",
" y_hat = tf.expand_dims(y_hat, [1])\n",
" \n",
" y_hat = tf.tile(y_hat,[1,tsteps,1,1])\n",
" y_hat = tf.transpose(y_hat, [0,2,1,3])\n",
" \n",
" y_minus = -y*tf.log(y_hat) - (1-y)*tf.log(1-y_hat) # binary cross entropy loss\n",
" y_minus = tf.reduce_sum(y_minus, axis=-1)\n",
" y_minus = tf.reduce_min(y_minus, axis=1)\n",
" \n",
" return tf.reduce_sum(y_minus) / (FLAGS.batch_size*FLAGS.length)"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 8,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
2017-03-02 07:11:23 +08:00
"permute by: [0 4 3 1 2]\n",
"guessed permutation: [0 4 3 1 2]\n"
2017-03-01 22:29:54 +08:00
]
2017-03-02 01:49:41 +08:00
},
{
"data": {
2017-03-02 07:11:23 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGMAAAB1CAYAAABatF8TAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAB65JREFUeJztnWuoXcUVx3//xOC7LXh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CA1w65XqV0Zkq7AbfNwL3Aqbn3HWLFfbC98t6MLiUw9VJCaKOtVqpyB95iVrwcebPss\n4CPg9gHzfwss8ixzpMh3nme+rcC1A5x3EPBfYAFub5FlIfem1pohaRbuqXu+lVb4564BzqmwaK/I\n1JJmFEsfBg1SE2W9St0LLEdwXuuTHemTwAlVFOgTmTokSE3M9SpJrnaNjEdkar8gNaHrVXpSc38x\nC/cyalFH+iiwKnafASzHvaM/OlDe54CVJd9fAuwCvil+145Cvlaaku0ziqdnHOcEB+xuRi4A1sUs\nqy0y9fkWHpm6X5Ca1nqVU3A1ajbwb9zi09lFfzgwe6OZWgaMShoHNgBLgANwtaMrvtvRhUSmDglS\nY7HXq9TZTLVV75tx7zi24zrM0/ucP5/p6t9+/LGkKes8dxfwy5Iy/oCLo7Ud+BR4FlgQ8NvWEji0\nze8zEiLbphIiKyMhsjISIisjIbIyEiIrIyGyMhIiKyMhsjISIisjIbIyEuI7aIsTsxBe7NYAAAAA\nSUVORK5CYII=\n",
2017-03-02 01:49:41 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x119267310>"
2017-03-02 01:49:41 +08:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
2017-03-02 07:11:23 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAGMAAAB1CAYAAABatF8TAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAB5BJREFUeJztnWusXFUVx3//YgMCPhKuIAmKGsSEYAqCD4S2oZCU+KHE\nxABiYtBQUkA/NDE+EgOxIYTE0ECsTWyNXg1R4gebYNReLOUhqU21YvVG0CgioOVKS7wmpcS2LD7s\nM73TuTNzZ+/Z53Q3Z/2Sk8nsOfvsNbNmv9Zee22ZGU4ZLDreAjhzuDIKwpVREK6MgnBlFIQroyBc\nGQXhyiiIN9X5cElnACuB54DX6iyrYE4B3gNMmdn+oXeaWW0XcCNgfmHAjQv9Xkk1Q9L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OOBoM3IVsCNnWV2Rqa+09MjUCwWp6exXuYhQo5YAvyNsPl1S9Ycj\nczyaqfXApKTdwC5gLXAqoXb0JfY4upTI1ClBaiz3fpUmm6mu6n0bYY3jIKHDvHSB+5czV/27r+8N\nacp67z0CfHZIGd8lxNE6CLwEPAysSPhu20kc2vp6RkG4baogXBkF4cooCFdGQbgyCsKVURCujIJw\nZRSEK6MgXBkF4cooiDcAgcEJ8jLx8x4AAAAASUVORK5CYII=\n",
2017-03-02 01:49:41 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x119367110>"
2017-03-02 01:49:41 +08:00
]
},
"metadata": {},
"output_type": "display_data"
2017-03-01 22:29:54 +08:00
}
],
"source": [
2017-03-02 01:49:41 +08:00
"def remove_duplicates(k, l):\n",
" i = 0 ; removed_duplicate = False\n",
" while i < len(k) and not removed_duplicate: \n",
" if k[-i] == k[-i-1]:\n",
" ri = np.random.randint(2)\n",
" k = np.delete(k,-i-ri) ; l = np.delete(l,-i-ri) ; removed_duplicate = True\n",
" k,l = remove_duplicates(k,l)\n",
" i+=1\n",
" return k,l\n",
"\n",
"def guess_recall_order(real_y, pred_y, tsteps):\n",
" # sorry this is uuuuugly but we have to because it's batched\n",
" real_y = np.tile(real_y, [1,1,1,1]) ; real_y = np.transpose(real_y, (1,0,2,3))\n",
" pred_y = np.tile(pred_y,[1,1,1,1]) ; pred_y = np.transpose(pred_y, (1,0,2,3))\n",
" \n",
" pred_y = np.tile(pred_y,[1,tsteps,1,1])\n",
" pred_y = np.transpose(pred_y, (0,2,1,3))\n",
" \n",
" real_y = real_y[0,:,:,:] ; pred_y = pred_y[0,:,:,:]\n",
" y_minus = .5*(real_y - pred_y)**2\n",
" y_minus = np.sum(y_minus, axis=-1)\n",
" y_mins = np.amin(y_minus, axis=1)\n",
" \n",
" k, l = np.where(y_minus == np.tile(y_mins,[tsteps,1]).T)\n",
" k, l = remove_duplicates(k, l)\n",
" return l\n",
"\n",
"# test\n",
2017-03-01 22:29:54 +08:00
"X, real_y = next_batch(FLAGS.batch_size, FLAGS.length, FLAGS.xlen)\n",
"real_y = np.stack(real_y)[:,-FLAGS.length:,:]\n",
"\n",
2017-03-02 07:11:23 +08:00
"p = np.random.permutation(real_y[0].shape[0]) ; print 'permute by: ', p\n",
2017-03-02 01:49:41 +08:00
"pred_y = [y_i[p,:] for y_i in real_y]\n",
2017-03-01 22:29:54 +08:00
"pred_y = np.stack(pred_y)\n",
"\n",
2017-03-02 01:49:41 +08:00
"plt.figure(0, figsize=[1,1]) ; plt.imshow(real_y[0,:,:].T, interpolation='none')\n",
"plt.figure(1, figsize=[1,1]) ; plt.imshow(pred_y[0,:,:].T, interpolation='none')\n",
2017-03-01 22:29:54 +08:00
"\n",
2017-03-02 01:49:41 +08:00
"print \"guessed permutation: \", guess_recall_order(real_y, pred_y, FLAGS.length)"
2017-03-01 22:29:54 +08:00
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Build graph, initialize everything"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 9,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"building graph...\n",
"defining loss...\n",
"computing gradients...\n",
"init variables... \n",
"ready to train..."
]
}
],
"source": [
"sess = tf.InteractiveSession()\n",
"\n",
"llprint(\"building graph...\\n\")\n",
"optimizer = tf.train.RMSPropOptimizer(FLAGS.lr, momentum=FLAGS.momentum)\n",
"dnc = DNC(RNNController, FLAGS)\n",
"\n",
"llprint(\"defining loss...\\n\")\n",
"y_hat, outputs = dnc.get_outputs()\n",
"y_hat = tf.clip_by_value(tf.sigmoid(y_hat), 1e-6, 1. - 1e-6) # avoid infinity\n",
"rlen = (dnc.tsteps-3)/2\n",
"loss = free_recall_loss(dnc.y[:,-rlen:,:], y_hat[:,-rlen:,:], tsteps=rlen)\n",
"\n",
"llprint(\"computing gradients...\\n\")\n",
"gradients = optimizer.compute_gradients(loss)\n",
"for i, (grad, var) in enumerate(gradients):\n",
" if grad is not None:\n",
" gradients[i] = (tf.clip_by_value(grad, -10, 10), var)\n",
" \n",
"grad_op = optimizer.apply_gradients(gradients)\n",
"\n",
"llprint(\"init variables... \\n\")\n",
"sess.run(tf.global_variables_initializer())\n",
"llprint(\"ready to train...\")"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 10,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"model overview...\n",
"\tvariable \"dnc_scope/basic_lstm_cell/weights:0\" has 73728 parameters\n",
"\tvariable \"dnc_scope/basic_lstm_cell/biases:0\" has 512 parameters\n",
"\tvariable \"W_z:0\" has 6144 parameters\n",
"\tvariable \"W_v:0\" has 768 parameters\n",
"\tvariable \"W_r:0\" has 60 parameters\n",
"total of 81212 parameters\n"
]
}
],
"source": [
"# tf parameter overview\n",
"total_parameters = 0 ; print \"model overview...\"\n",
"for variable in tf.trainable_variables():\n",
" shape = variable.get_shape()\n",
" variable_parameters = 1\n",
" for dim in shape:\n",
" variable_parameters *= dim.value\n",
" print '\\tvariable \"{}\" has {} parameters' \\\n",
" .format(variable.name, variable_parameters)\n",
" total_parameters += variable_parameters\n",
"print \"total of {} parameters\".format(total_parameters)"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 11,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"loaded model: rnn_models/model.ckpt-30000\n"
]
}
],
"source": [
"global_step = 0\n",
"saver = tf.train.Saver(tf.global_variables())\n",
"load_was_success = True # yes, I'm being optimistic\n",
"try:\n",
" save_dir = '/'.join(FLAGS.save_path.split('/')[:-1])\n",
" ckpt = tf.train.get_checkpoint_state(save_dir)\n",
" load_path = ckpt.model_checkpoint_path\n",
" saver.restore(sess, load_path)\n",
"except:\n",
" print \"no saved model to load.\"\n",
" load_was_success = False\n",
"else:\n",
" print \"loaded model: {}\".format(load_path)\n",
" saver = tf.train.Saver(tf.global_variables())\n",
" global_step = int(load_path.split('-')[-1]) + 1"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Train loop"
]
},
{
"cell_type": "code",
2017-03-02 01:49:41 +08:00
"execution_count": 10,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"loss_history = []\n",
"for i in xrange(global_step, FLAGS.iterations + 1):\n",
" llprint(\"\\rIteration {}/{}\".format(i, FLAGS.iterations))\n",
"\n",
" rlen = np.random.randint(1, FLAGS.length + 1)\n",
" X, y = next_batch(FLAGS.batch_size, rlen, FLAGS.xlen)\n",
" tsteps = 2*rlen+3\n",
"\n",
" fetch = [loss, grad_op]\n",
" feed = {dnc.X: X, dnc.y: y, dnc.tsteps: tsteps}\n",
"\n",
" step_loss, _ = sess.run(fetch, feed_dict=feed)\n",
" loss_history.append(step_loss)\n",
" global_step = i\n",
"\n",
" if i % 100 == 0:\n",
" llprint(\"\\n\\tloss: {:03.4f}\\n\".format(np.mean(loss_history)))\n",
" loss_history = []\n",
" if i % FLAGS.save_every == 0 and i is not 0:\n",
" llprint(\"\\n\\tSAVING MODEL\\n\")\n",
" saver.save(sess, FLAGS.save_path, global_step=global_step)"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 12,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
2017-03-02 07:11:23 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAA1AAAAHpCAYAAACMQd2lAAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAAPYQAAD2EBqD+naQAAIABJREFUeJzs3Xm4JFV5+PHvOzMwbMKgyOKKBFxgEBkQRQGNoBB3NFFG\nk7gTXPMjC4IoKIlCNAoiEncHFyAYNzQRBJcIoiKMomxRBNxYhmVmkIGBYeb9/VF1oadv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8wYn03RYV2UUT8B8Xf+ZvL+HZrOO5IivHbP4mIT5blPZjiitqzgIlO1NC+\nb4ornRtERJSV6huBizNzzcQBPX63kqT2dXzLUR9NqtYFg9R976CY9OEHEfEJijsyD6O4e/P0zLyD\nHusa4J0U9cwPy3jWUoxc2RA4ok2annWo06zPNHvVPQ1glY3W05ivBR7c6bimYx9HsQbCCorG+EnA\nhk3p9wcuo7hNfyXFVZxJ05iXx76D4oHMNc1ltvkM/4/ieZ6J6VBfTDFOeA3F+OLnNBz7VIox12vL\neL/c4rOeAFxX8fxtTtEhu7mM4b8phiFcC3y6xbmqcl73AS4uz9WvgTe0O1dtYmp5/iiuXvUda8P7\nz6R4sHUivtdTTLe6qum4rSjW/bieYujfHymu6r12mN9303v/QvGM15EUdz6P6fe7dXNzc5vNW9U6\nvkKdUbUuqFT3tak3H0HxvNBNFMPvfg18GJjXcEy7unFSfuX+3YD/KeubPwHnAXu1OUdd6/YO57ll\nnWZ95jZbt8ic2ZOiRLG69zHAQzPz9hrj2Jzix+WIzPxst+O75LUhxY/8+zLzlCGENytExFeBnTOz\n1ZjxYZc16fuOiA2ADwE
2017-03-01 22:29:54 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x11a05afd0>"
2017-03-01 22:29:54 +08:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"X, y = next_batch(FLAGS.batch_size, FLAGS.length, FLAGS.xlen)\n",
"tsteps = 2*FLAGS.length+3\n",
"\n",
"feed = {dnc.X: X, dnc.y: y, dnc.tsteps: tsteps}\n",
"fetch = [outputs['y_hat'], outputs['w_w'], outputs['w_r'], outputs['f'], outputs['g_a']]\n",
"[_y_hat, _w_w, _w_r, _f, _g_a] = sess.run(fetch, feed)\n",
"_y_hat = np.clip(_y_hat, 1e-6, 1-1e-6)\n",
"_y = y[0] ; _X = X[0]\n",
"\n",
"fig, ((ax1,ax2),(ax3,ax5),(ax4,ax6),) = plt.subplots(nrows=3, ncols=2)\n",
"plt.rcParams['savefig.facecolor'] = \"0.8\"\n",
"fs = 12 # font size\n",
"fig.set_figwidth(10)\n",
"fig.set_figheight(5)\n",
"\n",
"ax1.imshow(_X.T - _y.T, interpolation='none') ; ax1.set_title('input ($X$) and target ($y$)')\n",
"ax2.imshow(_y_hat[0,-FLAGS.length:,:].T, interpolation='none') ; ax2.set_title('prediction ($\\hat y$)')\n",
"\n",
"ax3.imshow(_w_w[0,:,:].T, interpolation='none') ; ax3.set_title('write weighting ($w_w$)')\n",
"ax4.imshow(_w_r[0,:,:,0].T, interpolation='none') ; ax4.set_title('read weighting ($w_r$)')\n",
"\n",
"ax5.imshow(_f[0,:,:].T, interpolation='none') ; ax5.set_title('free gate ($f$)') ; ax5.set_aspect(3)\n",
"ax6.imshow(_g_a[0,:,:].T, interpolation='none') ; ax6.set_title('allocation gate ($g_a$)') ; ax6.set_aspect(3)\n",
"\n",
"plt.tight_layout()"
]
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 26,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"FLAGS.length = 12"
]
},
{
"cell_type": "code",
"execution_count": 27,
2017-03-01 22:29:54 +08:00
"metadata": {
2017-03-02 01:49:41 +08:00
"collapsed": false
2017-03-01 22:29:54 +08:00
},
2017-03-02 01:49:41 +08:00
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"starting free recall trials: \n",
"\ttrial #0\n",
"\ttrial #100\n",
"\ttrial #200\n",
"\ttrial #300\n",
"\ttrial #400\n",
"\ttrial #500\n",
"\ttrial #600\n",
"\ttrial #700\n",
"\ttrial #800\n",
2017-03-02 07:11:23 +08:00
"\ttrial #900\n",
"\ttrial #1000\n",
"\ttrial #1100\n",
"\ttrial #1200\n",
"\ttrial #1300\n",
"\ttrial #1400\n",
"\ttrial #1500\n",
"\ttrial #1600\n",
"\ttrial #1700\n",
"\ttrial #1800\n",
"\ttrial #1900\n",
"\ttrial #2000\n",
"\ttrial #2100\n",
"\ttrial #2200\n",
"\ttrial #2300\n",
"\ttrial #2400\n",
"\ttrial #2500\n",
"\ttrial #2600\n",
"\ttrial #2700\n",
"\ttrial #2800\n",
"\ttrial #2900\n",
"\ttrial #3000\n",
"\ttrial #3100\n",
"\ttrial #3200\n",
"\ttrial #3300\n",
"\ttrial #3400\n",
"\ttrial #3500\n",
"\ttrial #3600\n",
"\ttrial #3700\n",
"\ttrial #3800\n",
"\ttrial #3900\n",
"\ttrial #4000\n",
"\ttrial #4100\n",
"\ttrial #4200\n",
"\ttrial #4300\n",
"\ttrial #4400\n",
"\ttrial #4500\n",
"\ttrial #4600\n",
"\ttrial #4700\n",
"\ttrial #4800\n",
"\ttrial #4900\n"
2017-03-02 01:49:41 +08:00
]
}
],
"source": [
"recall_orders = []\n",
2017-03-02 07:11:23 +08:00
"trials = 5000 ; print \"starting free recall trials: \"\n",
2017-03-02 01:49:41 +08:00
"for i in range(trials):\n",
" X, y = next_batch(FLAGS.batch_size, FLAGS.length, FLAGS.xlen)\n",
" tsteps = 2*FLAGS.length+3\n",
"\n",
" feed = {dnc.X: X, dnc.tsteps: tsteps}\n",
" _y_hat = outputs['y_hat'].eval(feed)\n",
" _y_hat = np.clip(_y_hat, 1e-6, 1-1e-6)\n",
" _y = y[0] ; _X = X[0]\n",
" \n",
" real_y = np.tile(_y[-FLAGS.length:,:], [1,1,1])\n",
" pred_y = _y_hat[0:1,-FLAGS.length:,:]\n",
" order = guess_recall_order(real_y, pred_y, FLAGS.length)\n",
" \n",
" recall_orders.append(order)\n",
" if (i%100 == 0): print(\"\\ttrial #{}\".format(i))\n",
"recall_orders = np.stack(recall_orders)"
]
2017-03-01 22:29:54 +08:00
},
{
"cell_type": "code",
2017-03-02 07:11:23 +08:00
"execution_count": 29,
2017-03-01 22:29:54 +08:00
"metadata": {
2017-03-02 01:49:41 +08:00
"collapsed": false
2017-03-01 22:29:54 +08:00
},
2017-03-02 01:49:41 +08:00
"outputs": [
{
"data": {
2017-03-02 07:11:23 +08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wj++OjvyZ2Od9KM8Z3Kt2h+XgA/pxnacTfgsxMNkmwD\n3IWml7vfvsD5VXXBCDVKmoJjdiUBkOTvaC7eOpNmnOOkquo7NEMZXkHf1epVdSJNWP4/Sa53hXmS\nbZO8bVx1t/s8jyZw/FOSZQP22TsW8hjgXkkeOqDd0iSLRyzjczShauAdv3qmDFtLExZfkmSy4QgT\nvY79v6NXMd6vun9IE8om84IBz6/munD6JZpOk/52q2iOxZfh2iEZ/U6mCf2Tjf2GZkaNiR7WSVXV\nRpqx2Y/JprdN3pNmdoXvVtWwPeswwpjdamZlOAF4Wja9Y9pBwE4051+/FTQ/C0mzwJ5daeEJ8Igk\n+9H8DtiTplfqITQh7NE1vQn7X0vTSzfIQTRft/9Xki/SXPx1Gc142SfTXCX/LzP5EAM8n+Yio/9N\n8kGa0L4nzTRdf8N1Uz69jWZ6tC8mOZpm2qedgDsDj6MZj3rhsDuvqjOT/BvwxiT7Al+guUjvFsDf\n01xUd3hVVZLnAscCJ7Xzyf6JZjqz21fVw6vqkiTfoRkqsi3wB5rp4PZhiim4hvTfNKHsVgN6o68C\nHtYeox/RXIz2cOANPT2Qx9GcA29oP/PE1GOPopnJYaIH+DVJ7k9z05JzaH4uzwV+SzNbxmTW0Hze\ndyc5nmZmhkE9o9D8ofZg4PtJjqT5g+HZwLZA/7zOkx3DTdaPOGYX4FU032p8J8kHaMYLvxQ4vqq+\ntskOkz1ozr3+i/wkjYlhV1p4iutuv3o1TbD7X5r5YI8ecLHR4Dep+naSb9PMXlB9r52f5N40NwV4\nEs3X1dvSfJX8RZq5Zad
2017-03-02 01:49:41 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x11bef3d50>"
2017-03-02 01:49:41 +08:00
]
},
"metadata": {},
"output_type": "display_data"
},
{
"data": {
2017-03-02 07:11:23 +08:00
"image/png": "iVBORw0KGgoAAAANSUhEUgAAArsAAAHUCAYAAAA6KeCQAAAABHNCSVQICAgIfAhkiAAAA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p7cqfjnTRjfDflmzR/XkNrh3X8J81xeFJVfXeKtotpeqi9OE2aIcfsSgtQkofRXLx1Bs24\nwUlV1bdphjK8nL6r1avqZJqw/I9JDhjwPtskmWw84kiq6nyawPHPSZYOeM/eMY7HAvdNsu+Adkva\nQDGKz9KEqoF3/OqZMmwNTVg8JMlkwxEmeh37/z5eyXi/wv4BcK8ptr9gwPMruS6cfommg6S/3Uqa\nY/FluHZIRr9TaUL/ZGO/oZlRY6KHdVJVtYFmbPYB2fi2ybvSzK7wnaoatmcdNs+Y3fcATwIOrnbG\nkCncmeb3bXP9J1LqLHt2pW4L8Kgke9D8vu9K0yu1D00I27+mN2H/a2l66QY5kObr9v9OcjzNxV+X\n0YyXfSrNVfL/OpMPMcDzaS4y+nmSD9KE9l1ppun6G5q7uUFz4c/+wPFJjgFW04yTvDvweJrxqBcO\n++ZVdUaSfwPemOQ2wOdpLtK7LfBYmovqDq+qSnIwcBxwSjuf7J9opjO7c1U9sqouTfJtmqEi29Dc\nhWvftrZxXuD1BeAZSW4/oDf6CuAR7TH6Ic3FaI8E3lBVF7RtvkhzDryh/cwTU489hmYmh4ke4H9P\n8iCaMaln0/y5HAz8jma2jMmspvm8705yAs3MDP81Sdt/owme30tyJM1/GJ4DbAP0z+s82THcaP2o\nY3bT3NJ3//bp7WkumnxV+/zUqjq+bXcIzXH4PrA+ydP7XuqzVdU7ZGZfmt+j/osjJQ1rrqeDcHFx\nmZ2F66apmljW0QSpr9C
2017-03-02 01:49:41 +08:00
"text/plain": [
2017-03-02 07:11:23 +08:00
"<matplotlib.figure.Figure at 0x11df76e50>"
2017-03-02 01:49:41 +08:00
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"def plot_recall(probs, pos):\n",
" # print \"Is normalized: \", np.abs(1-np.sum(position_probs)) < 1e-6\n",
2017-03-02 07:11:23 +08:00
" plt.figure(pos, figsize=[8,5])\n",
" plt.axis((0,4,0.0,1.0))\n",
2017-03-02 01:49:41 +08:00
" plt.title(\"DNC free recall (position={})\".format(pos))\n",
" plt.xlabel(\"Serial position\") ; plt.ylabel(\"Recall probability\")\n",
" plt.plot(range(len(probs)), probs) ; plt.show()\n",
"\n",
2017-03-02 07:11:23 +08:00
"for pos in range(1):\n",
2017-03-02 01:49:41 +08:00
" position_probs = [np.sum(i == recall_orders[:,pos])/float(trials) for i in range(FLAGS.length)]\n",
" plot_recall(position_probs, pos=pos)\n",
" \n",
"p0_probs = np.asarray([np.sum(i == recall_orders[:,0])/float(trials) for i in range(FLAGS.length)])\n",
"p1_probs = np.asarray([np.sum(i == recall_orders[:,1])/float(trials) for i in range(FLAGS.length)])\n",
"plot_recall(.5*(p0_probs + p1_probs), pos=12)"
]
2017-03-01 22:29:54 +08:00
},
{
"cell_type": "code",
2017-03-02 01:49:41 +08:00
"execution_count": null,
2017-03-01 22:29:54 +08:00
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
2017-03-02 01:49:41 +08:00
"# # X, y = next_batch(FLAGS.batch_size, FLAGS.length, FLAGS.xlen)\n",
"# # y = np.stack(y)\n",
"# # y = y[:,FLAGS.length + 3:,:]\n",
"# # # p = np.random.permutation(FLAGS.length)\n",
"# # y_shuffle = y #y[:,p,:] ; print 'permute by: ', p\n",
"# # # y_shuffle[:,-1,-1] = 4\n",
"\n",
"# # plt.figure(0, figsize=[1,1])\n",
"# # plt.imshow(y[0,:,:].T, interpolation='none')\n",
"# # plt.figure(1, figsize=[1,1])\n",
"# # plt.imshow(y_shuffle[0,:,:].T, interpolation='none')\n",
"# # plt.show()\n",
"\n",
"# def guess_recall_order(real_y, pred_y, FLAGS):\n",
2017-03-01 22:29:54 +08:00
"# # sorry this is uuuuugly but we have to because it's batched\n",
"# real_y = np.tile(real_y, [1,1,1,1]) ; real_y = np.transpose(real_y, (1,0,2,3))\n",
"# pred_y = np.tile(pred_y,[1,1,1,1]) ; pred_y = np.transpose(pred_y, (1,0,2,3))\n",
" \n",
2017-03-02 01:49:41 +08:00
"# pred_y = np.tile(pred_y,[1,FLAGS.length,1,1])\n",
2017-03-01 22:29:54 +08:00
"# pred_y = np.transpose(pred_y, (0,2,1,3))\n",
" \n",
2017-03-02 01:49:41 +08:00
"# # real_y = real_y[0,:,:,:] ; pred_y = pred_y[0,:,:,:]\n",
2017-03-01 22:29:54 +08:00
"# y_minus = .5*(real_y - pred_y)**2\n",
"# y_minus = np.sum(y_minus, axis=-1)\n",
2017-03-02 01:49:41 +08:00
"# y_mins = np.amin(y_minus, axis=1)\n",
2017-03-01 22:29:54 +08:00
" \n",
2017-03-02 01:49:41 +08:00
"# k, l = np.where(y_minus == y_mins)\n",
"# i = 0\n",
"# while i < len(k): \n",
"# if k[-i] == k[-i-1]:\n",
"# k = np.delete(k,-i) ; l = np.delete(l,-i)\n",
"# i+=1\n",
"# return l\n",
2017-03-01 22:29:54 +08:00
"\n",
"# X, real_y = next_batch(FLAGS.batch_size, FLAGS.length, FLAGS.xlen)\n",
"# real_y = np.stack(real_y)[:,-FLAGS.length:,:]\n",
"\n",
2017-03-02 01:49:41 +08:00
"# p = np.random.permutation(y_i.shape[0]) ; print 'permute by: ', p\n",
"# pred_y = [y_i[p,:] for y_i in real_y]\n",
2017-03-01 22:29:54 +08:00
"# pred_y = np.stack(pred_y)\n",
"# # pred_y[:,-2:,-1] = 4\n",
"\n",
2017-03-02 01:49:41 +08:00
"# plt.figure(0, figsize=[1,1])\n",
"# plt.imshow(real_y[0,:,:].T, interpolation='none')\n",
"# plt.figure(1, figsize=[1,1])\n",
"# plt.imshow(pred_y[0,:,:].T, interpolation='none')\n",
"# plt.show()\n",
"\n",
2017-03-01 22:29:54 +08:00
"# np_loss(real_y, pred_y, FLAGS)"
]
2017-03-02 01:49:41 +08:00
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def scatter(idx, vals, target):\n",
" \"\"\"target[idx] += vals, but allowing for repeats in idx\"\"\"\n",
" np.add.at(target, idx.ravel(), vals.ravel())"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
2017-03-01 22:29:54 +08:00
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"language": "python",
"name": "python2"
2017-03-02 07:11:23 +08:00
},
"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.10"
2017-03-01 22:29:54 +08:00
}
},
"nbformat": 4,
"nbformat_minor": 1
}