Skip to content

Instantly share code, notes, and snippets.

@xiangze
Created June 20, 2015 18:20
Show Gist options
  • Save xiangze/f7f06a76e656809515ea to your computer and use it in GitHub Desktop.
Save xiangze/f7f06a76e656809515ea to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"metadata": {
"name": "",
"signature": "sha256:b461d56f6bdabcc4245fe06eff0047157855ed5818ded38fb1854ad3065f8c75"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"scan \u2013 Looping in Theano"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"http://deeplearning.net/software/theano/library/scan.html"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import theano\n",
"import theano.tensor as T\n",
"import numpy as np"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stderr",
"text": [
"Using gpu device 0: GeForce GT 620\n"
]
}
],
"prompt_number": 9
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"A^k\u3092accumulation\u3067\u6c42\u3081\u308b\u3002"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u6c42\u3081\u305f\u3044\u3082\u306e"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"result = 1\n",
"for i in xrange(k):\n",
" result = result * A"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"scan\u306enon_sequence\u306btensor\u3092\u5165\u308c\u308b\u3068loop(\u7573\u307f\u8fbc\u307f)\u304c\u305f\u306b\u306a\u308b\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"k = T.iscalar(\"k\")\n",
"A = T.vector(\"A\")\n",
"\n",
"result, updates = theano.scan(fn=lambda prior, A: prior * A,\n",
" outputs_info=T.ones_like(A),\n",
" non_sequences=A,\n",
" n_steps=k)\n",
"\n",
"#\u6700\u5f8c\u306e\u8981\u7d20\u3060\u3051\u3068\u3063\u3066\u304f\u308b\n",
"final_result = result[-1]\n",
"\n",
"# compile\n",
"power = theano.function(inputs=[A,k], outputs=final_result, updates=updates)\n",
"\n",
"print power(range(10),2)\n",
"print power(range(10),4)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0. 1. 4. 9. 16. 25. 36. 49. 64. 81.]\n",
"[ 0.00000000e+00 1.00000000e+00 1.60000000e+01 8.10000000e+01\n",
" 2.56000000e+02 6.25000000e+02 1.29600000e+03 2.40100000e+03\n",
" 4.09600000e+03 6.56100000e+03]\n"
]
}
],
"prompt_number": 11
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Tensor\u306e\u7b2c1\u6b21\u5143\u306b\u5bfe\u3057\u3066\u7e70\u308a\u8fd4\u3057(map)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"\u3084\u308a\u305f\u3044\u3053\u3068\n",
"\n",
"for x in a_list\n",
"\n",
"scan\u306e\u5f15\u6570sequence\u306b\u5024\u3092\u4ee3\u5165\u3059\u308b\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"coefficients = theano.tensor.vector(\"coefficients\")\n",
"x = T.scalar(\"x\")\n",
"\n",
"max_coefficients_supported = 10000\n",
"\n",
"def term(c,p,x):\n",
" return c*(x**p)\n",
"\n",
"components, updates = theano.scan(term,\n",
" outputs_info=None,\n",
" sequences=[coefficients, theano.tensor.arange(max_coefficients_supported)],\n",
" non_sequences=x)\n",
"polynomial = components.sum()\n",
"\n",
"# compile\n",
"calculate_polynomial = theano.function(inputs=[coefficients, x], outputs=polynomial)\n",
"\n",
"# Test\n",
"test_coefficients = np.asarray([1, 0, 2], dtype=np.float32)\n",
"test_value = 3\n",
"print calculate_polynomial(test_coefficients, test_value)\n",
"print 1.0 * (3 ** 0) + 0.0 * (3 ** 1) + 2.0 * (3 ** 2)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"19.0\n",
"19.0\n"
]
}
],
"prompt_number": 14
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Simple accumulation"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"output_info\u306b\u521d\u671f\u5024\u3092\u5165\u308c\u308b(output\u3068\u540c\u3058\u578b)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"up_to = T.iscalar(\"up_to\")\n",
"\n",
"def accum(arange_val, sum_to_date):\n",
" return sum_to_date + arange_val\n",
"seq = T.arange(up_to)\n",
"\n",
"# An unauthorized implicit downcast from the dtype of 'seq', to that of\n",
"# 'T.as_tensor_variable(0)' which is of dtype 'int8' by default would occur\n",
"# if this instruction were to be used instead of the next one:\n",
"# outputs_info = T.as_tensor_variable(0)\n",
"\n",
"outputs_info = T.as_tensor_variable(np.asarray(0, seq.dtype))\n",
"\n",
"scan_result, scan_updates = theano.scan(fn=accum,\n",
" outputs_info=outputs_info,\n",
" sequences=seq)\n",
"triangular_sequence = theano.function(inputs=[up_to], outputs=scan_result)\n",
"\n",
"# test\n",
"some_num = 15\n",
"print triangular_sequence(some_num)\n",
"print [n * (n + 1) // 2 for n in xrange(some_num)]"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 0 1 3 6 10 15 21 28 36 45 55 66 78 91 105]\n",
"[0, 1, 3, 6, 10, 15, 21, 28, 36, 45, 55, 66, 78, 91, 105]\n"
]
}
],
"prompt_number": 15
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"\u4ee3\u5165\u3059\u308b\u4f4d\u7f6e\u3092\u6307\u5b9a\u3059\u308b\u4f8b"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Tensor\u3068\u4ee3\u5165\u3059\u308b\u5024\u306e\u547d\u540d\u898f\u5247\u3092\u7d71\u4e00\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\uff1f"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"location = T.imatrix(\"location\")\n",
"values = T.vector(\"values\")\n",
"output_model = T.matrix(\"output_model\")\n",
"\n",
"#\n",
"def set_value_at_position(a_location, a_value, output_model):\n",
" zeros = T.zeros_like(output_model)\n",
" zeros_subtensor = zeros[a_location[0], a_location[1]]\n",
" return T.set_subtensor(zeros_subtensor, a_value)\n",
"\n",
"result, updates = theano.scan(fn=set_value_at_position,\n",
" outputs_info=None,\n",
" sequences=[location, values],\n",
" non_sequences=output_model)\n",
"\n",
"assign_values_at_positions = theano.function(inputs=[location, values, output_model], outputs=result)\n",
"\n",
"# test\n",
"test_locations = np.asarray([[1, 1], [2, 3]], dtype=np.int32)\n",
"test_values = np.asarray([42, 50], dtype=np.float32)\n",
"test_output_model = np.zeros((5, 5), dtype=np.float32)\n",
"print assign_values_at_positions(test_locations, test_values, test_output_model)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[[[ 0. 0. 0. 0. 0.]\n",
" [ 0. 42. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]]\n",
"\n",
" [[ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 50. 0.]\n",
" [ 0. 0. 0. 0. 0.]\n",
" [ 0. 0. 0. 0. 0.]]]\n"
]
}
],
"prompt_number": 17
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"shared variables\u3092\u4f7f\u3063\u305f\u4f8b Gibbs sampling"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"theano.shared(initial_value)"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"W_values=0.1\n",
"bvis_values=0.1\n",
"bhid_values=0.1"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 19
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"W = theano.shared(W_values)\n",
"bvis = theano.shared(bvis_values)\n",
"bhid = theano.shared(bhid_values)\n",
"\n",
"trng = T.shared_randomstreams.RandomStreams(1234)\n",
"\n",
"def OneStep(vsample) :\n",
" hmean = T.nnet.sigmoid(theano.dot(vsample, W) + bhid)\n",
" hsample = trng.binomial(size=hmean.shape, n=1, p=hmean)\n",
" vmean = T.nnet.sigmoid(theano.dot(hsample, W.T) + bvis)\n",
" return trng.binomial(size=vsample.shape, n=1, p=vmean, dtype=theano.config.floatX)\n",
"\n",
"sample = T.vector()\n",
"\n",
"values, updates = theano.scan(OneStep, outputs_info=sample, n_steps=10)\n",
"\n",
"gibbs10 = theano.function([sample], values[-1], updates=updates)\n"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 22
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"a = theano.shared(1)\n",
"values, updates = theano.scan(lambda: {a: a+1}, n_steps=10)\n",
"b = a + 1\n",
"c = updates[a] + 1\n",
"f = theano.function([], [b, c], updates=updates)\n",
"\n",
"print b\n",
"print c\n",
"print a.value"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Elemwise{add,no_inplace}.0\n",
"Elemwise{add,no_inplace}.0\n"
]
},
{
"ename": "Exception",
"evalue": "sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead.",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mException\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-23-0433b0f21791>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mb\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;32mprint\u001b[0m \u001b[0mc\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[1;32mprint\u001b[0m \u001b[0ma\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvalue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\compile\\sharedvalue.pyc\u001b[0m in \u001b[0;36m_value_get\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 145\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 146\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_value_get\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 147\u001b[1;33m raise Exception(\"sharedvar.value does not exist anymore. Use \"\n\u001b[0m\u001b[0;32m 148\u001b[0m \u001b[1;34m\"sharedvar.get_value() or sharedvar.set_value()\"\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 149\u001b[0m \" instead.\")\n",
"\u001b[1;31mException\u001b[0m: sharedvar.value does not exist anymore. Use sharedvar.get_value() or sharedvar.set_value() instead."
]
}
],
"prompt_number": 23
},
{
"cell_type": "heading",
"level": 3,
"metadata": {},
"source": [
"strict"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"strict=true non_sequences\u306b\u95a2\u6570\u3067\u4f7f\u308f\u308c\u308b\u3059\u3079\u3066\u306eshared variable\u3092\u66f8\u304b\u306a\u3051\u308c\u3070\u3044\u3051\u306a\u304f\u306a\u308b\u3002"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"values, updates = theano.scan(fn=OneStep,\n",
" outputs_info=sample,\n",
" non_sequences=[W, bvis, bhid],\n",
" n_steps=10,\n",
" strict=True)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "scan() got an unexpected keyword argument 'strict'",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-24-d566df2629b6>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 3\u001b[0m \u001b[0mnon_sequences\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mW\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbvis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mbhid\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 4\u001b[0m \u001b[0mn_steps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m10\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 5\u001b[1;33m strict=True)\n\u001b[0m",
"\u001b[1;31mTypeError\u001b[0m: scan() got an unexpected keyword argument 'strict'"
]
}
],
"prompt_number": 24
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"!!\n",
"\n",
"version\u306e\u554f\u984c\uff1f"
]
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Recurrent Neural Network with Scan"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tap "
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"W=theano.shared([[1,1],[1,1]])\n",
"W_in_1=theano.shared([[1,1],[1,1]])\n",
"W_in_2=theano.shared([[1,1],[1,1]])\n",
"W_feedback=theano.shared([[1,1],[1,1]])\n",
"W_out=theano.shared([[1,1],[1,1]])\n",
"\n",
"def oneStep(u_tm4, u_t, x_tm3, x_tm1, y_tm1, W, W_in_1, W_in_2, W_feedback, W_out):\n",
"\n",
" x_t = T.tanh(theano.dot(x_tm1, W) + \\\n",
" theano.dot(u_t, W_in_1) + \\\n",
" theano.dot(u_tm4, W_in_2) + \\\n",
" theano.dot(y_tm1, W_feedback))\n",
" y_t = theano.dot(x_tm3, W_out)\n",
"\n",
" return [x_t, y_t]\n",
"\n",
"u = T.matrix() \n",
"x0 = T.matrix() \n",
"y0 = T.vector() \n",
"\n",
"([x_vals, y_vals], updates) = theano.scan(fn=oneStep,\n",
" sequences=dict(input=u, taps=[-4,-0]),\n",
" outputs_info=[dict(initial=x0, taps=[-3,-1]), y0],\n",
" non_sequences=[W, W_in_1, W_in_2, W_feedback, W_out])\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"ename": "NotImplementedError",
"evalue": "('Dot failed for the following reasons:', (AsTensorError('Variable type field must be a TensorType.', <Generic>, <theano.gof.type.Generic object at 0x04DFB810>), None))",
"output_type": "pyerr",
"traceback": [
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mNotImplementedError\u001b[0m Traceback (most recent call last)",
"\u001b[1;32m<ipython-input-32-58d38f924041>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 19\u001b[0m \u001b[0msequences\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mu\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtaps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m4\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 20\u001b[0m \u001b[0moutputs_info\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mdict\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minitial\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mx0\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mtaps\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;33m[\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m-\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my0\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 21\u001b[1;33m non_sequences=[W, W_in_1, W_in_2, W_feedback, W_out])\n\u001b[0m",
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\scan_module\\scan.pyc\u001b[0m in \u001b[0;36mscan\u001b[1;34m(fn, sequences, outputs_info, non_sequences, n_steps, truncate_gradient, go_backwards, mode, name, profile)\u001b[0m\n\u001b[0;32m 730\u001b[0m \u001b[1;31m# and outputs that needs to be separated\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 731\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 732\u001b[1;33m \u001b[0mcondition\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0moutputs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mupdates\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mscan_utils\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mget_updates_and_outputs\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mfn\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0margs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 733\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mcondition\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 734\u001b[0m \u001b[0mas_while\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mTrue\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32m<ipython-input-32-58d38f924041>\u001b[0m in \u001b[0;36moneStep\u001b[1;34m(u_tm4, u_t, x_tm3, x_tm1, y_tm1, W, W_in_1, W_in_2, W_feedback, W_out)\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0moneStep\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mu_tm4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mu_t\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx_tm3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mx_tm1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0my_tm1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_in_1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_in_2\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_feedback\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_out\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 8\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 9\u001b[1;33m \u001b[0mx_t\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mtanh\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mtheano\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_tm1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mtheano\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mu_t\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_in_1\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mtheano\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mu_tm4\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_in_2\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m+\u001b[0m \u001b[0mtheano\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0my_tm1\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_feedback\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 10\u001b[0m \u001b[0my_t\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mtheano\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx_tm3\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mW_out\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 11\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\__init__.pyc\u001b[0m in \u001b[0;36mdot\u001b[1;34m(l, r)\u001b[0m\n\u001b[0;32m 150\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mrval\u001b[0m \u001b[1;33m==\u001b[0m \u001b[0mNotImplemented\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 151\u001b[0m raise NotImplementedError(\"Dot failed for the following reasons:\",\n\u001b[1;32m--> 152\u001b[1;33m (e0, e1))\n\u001b[0m\u001b[0;32m 153\u001b[0m \u001b[1;32mreturn\u001b[0m \u001b[0mrval\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 154\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n",
"\u001b[1;31mNotImplementedError\u001b[0m: ('Dot failed for the following reasons:', (AsTensorError('Variable type field must be a TensorType.', <Generic>, <theano.gof.type.Generic object at 0x04DFB810>), None))"
]
}
],
"prompt_number": 32
},
{
"cell_type": "heading",
"level": 2,
"metadata": {},
"source": [
"Conditional ending of Scan"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"def power_of_2(previous_power, max_value):\n",
" return previous_power*2, theano.scan_module.until(previous_power*2 > max_value)\n",
"\n",
"max_value = T.scalar()\n",
"values, _ = theano.scan(power_of_2,\n",
" outputs_info = T.constant(1.),\n",
" non_sequences = max_value,\n",
" n_steps = 1024)\n",
"\n",
"f = theano.function([max_value], values)\n",
"\n",
"print f(45)"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"[ 2. 4. 8. 16. 32. 64.]\n"
]
}
],
"prompt_number": 28
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"Optimizing Scan\u2019s performance"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 28
},
{
"cell_type": "heading",
"level": 1,
"metadata": {},
"source": [
"reference"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"http://deeplearning.net/software/theano/library/scan.html#module-theano.scan_module"
]
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment