diff --git a/ipnb/01_scalar_soln.py b/ipnb/01_scalar_soln.py new file mode 100644 index 0000000..0ca85a0 --- /dev/null +++ b/ipnb/01_scalar_soln.py @@ -0,0 +1,39 @@ +import numpy as np +from theano import function +import theano.tensor as T + + +def make_scalar(): + """ + Returns a new Theano scalar. + """ + + return T.scalar() + + +def log(x): + """ + Returns the logarithm of a Theano scalar x. + """ + + return T.log(x) + + +def add(x, y): + """ + Adds two theano scalars together and returns the result. + """ + + return x + y + +a = make_scalar() +b = make_scalar() +c = log(b) +d = add(a, c) +f = function([a, b], d) +a = np.cast[a.dtype](1.) +b = np.cast[b.dtype](2.) +actual = f(a, b) +expected = 1. + np.log(2.) +assert np.allclose(actual, expected) +print "SUCCESS!" diff --git a/ipnb/02_vector_mat_soln.py b/ipnb/02_vector_mat_soln.py new file mode 100644 index 0000000..46da45b --- /dev/null +++ b/ipnb/02_vector_mat_soln.py @@ -0,0 +1,59 @@ +import numpy as np +from theano import function +import theano.tensor as T + + +def make_vector(): + """ + Returns a new Theano vector. + """ + + return T.vector() + + +def make_matrix(): + """ + Returns a new Theano matrix. + """ + + return T.matrix() + + +def elemwise_mul(a, b): + """ + a: A theano matrix + b: A theano matrix + Returns the elementwise product of a and b + """ + + return a * b + + +def matrix_vector_mul(a, b): + """ + a: A theano matrix + b: A theano vector + Returns the matrix-vector product of a and b + """ + + return T.dot(a, b) + +a = make_vector() +b = make_vector() +c = elemwise_mul(a, b) +d = make_matrix() +e = matrix_vector_mul(d, c) + +f = function([a, b, d], e) + +rng = np.random.RandomState([1, 2, 3]) +a_value = rng.randn(5).astype(a.dtype) +b_value = rng.rand(5).astype(b.dtype) +c_value = a_value * b_value +d_value = rng.randn(5, 5).astype(d.dtype) +expected = np.dot(d_value, c_value) + +actual = f(a_value, b_value, d_value) + +assert np.allclose(actual, expected) +print "SUCCESS!" diff --git a/ipnb/03_tensor_soln.py b/ipnb/03_tensor_soln.py new file mode 100644 index 0000000..e5b3b7d --- /dev/null +++ b/ipnb/03_tensor_soln.py @@ -0,0 +1,58 @@ +import numpy as np +from theano import function +import theano.tensor as T + + +def make_tensor(dim): + """ + Returns a new Theano tensor with no broadcastable dimensions. + dim: the total number of dimensions of the tensor. + """ + + return T.TensorType(broadcastable=tuple([False] * dim), dtype='float32')() + + +def broadcasted_add(a, b): + """ + a: a 3D theano tensor + b: a 4D theano tensor + Returns c, a 4D theano tensor, where + + c[i, j, k, l] = a[l, k, i] + b[i, j, k, l] + + for all i, j, k, l + """ + + return a.dimshuffle(2, 'x', 1, 0) + b + + +def partial_max(a): + """ + a: a 4D theano tensor + + Returns b, a theano matrix, where + + b[i, j] = max_{k,l} a[i, k, l, j] + + for all i, j + """ + + return a.max(axis=(1, 2)) + +a = make_tensor(3) +b = make_tensor(4) +c = broadcasted_add(a, b) +d = partial_max(c) + +f = function([a, b], d) + +rng = np.random.RandomState([1, 2, 3]) +a_value = rng.randn(2, 2, 2).astype(a.dtype) +b_value = rng.rand(2, 2, 2, 2).astype(b.dtype) +c_value = np.transpose(a_value, (2, 1, 0))[:, None, :, :] + b_value +expected = c_value.max(axis=1).max(axis=1) + +actual = f(a_value, b_value) + +assert np.allclose(actual, expected), (actual, expected) +print "SUCCESS!" diff --git a/ipnb/11_function_soln.py b/ipnb/11_function_soln.py new file mode 100644 index 0000000..d67e981 --- /dev/null +++ b/ipnb/11_function_soln.py @@ -0,0 +1,24 @@ +from theano import tensor as T +from theano import function + + +def evaluate(x, y, expr, x_value, y_value): + """ + x: A theano variable + y: A theano variable + expr: A theano expression involving x and y + x_value: A numpy value + y_value: A numpy value + + Returns the value of expr when x_value is substituted for x + and y_value is substituted for y + """ + + return function([x, y], expr)(x_value, y_value) + + +x = T.iscalar() +y = T.iscalar() +z = x + y +assert evaluate(x, y, z, 1, 2) == 3 +print "SUCCESS!" diff --git a/ipnb/12_shared_soln.py b/ipnb/12_shared_soln.py new file mode 100644 index 0000000..f2337e8 --- /dev/null +++ b/ipnb/12_shared_soln.py @@ -0,0 +1,60 @@ +import numpy as np +from theano.compat.python2x import OrderedDict +from theano import function +from theano import shared + + +def make_shared(shape): + """ + Returns a theano shared variable containing a tensor of the specified + shape. + You can use any value you want. + """ + return shared(np.zeros(shape)) + + +def exchange_shared(a, b): + """ + a: a theano shared variable + b: a theano shared variable + Uses get_value and set_value to swap the values stored in a and b + """ + temp = a.get_value() + a.set_value(b.get_value()) + b.set_value(temp) + + +def make_exchange_func(a, b): + """ + a: a theano shared variable + b: a theano shared variable + Returns f + where f is a theano function, that, when called, swaps the + values in a and b + f should not return anything + """ + + updates = OrderedDict() + updates[a] = b + updates[b] = a + f = function([], updates=updates) + return f + + +a = make_shared((5, 4, 3)) +assert a.get_value().shape == (5, 4, 3) +b = make_shared((5, 4, 3)) +assert a.get_value().shape == (5, 4, 3) +a.set_value(np.zeros((5, 4, 3), dtype=a.dtype)) +b.set_value(np.ones((5, 4, 3), dtype=b.dtype)) +exchange_shared(a, b) +assert np.all(a.get_value() == 1.) +assert np.all(b.get_value() == 0.) +f = make_exchange_func(a, b) +rval = f() +assert isinstance(rval, list) +assert len(rval) == 0 +assert np.all(a.get_value() == 0.) +assert np.all(b.get_value() == 1.) + +print "SUCCESS!" diff --git a/ipnb/13_bug_soln.py b/ipnb/13_bug_soln.py new file mode 100644 index 0000000..32b9b48 --- /dev/null +++ b/ipnb/13_bug_soln.py @@ -0,0 +1,10 @@ +# The weird thing is that the function succeeds. +# +# This is weird because the two values passed in for x and y do not +# have the same shape, yet x is added with something that has the same +# shape as y (z). +# +# This happens because optimizations realize that z is always zero and +# therefore remove the addition, which removes the error. +# +# The problem is more evident if FAST_COMPILE or DEBUG_MODE is used. diff --git a/ipnb/21_grad_soln.py b/ipnb/21_grad_soln.py new file mode 100644 index 0000000..db606d1 --- /dev/null +++ b/ipnb/21_grad_soln.py @@ -0,0 +1,23 @@ +# Fill in the TODOs in this exercise, then run +# python 01_grad.py to see if your solution works! +# +from theano import tensor as T + + +def grad_sum(x, y, z): + """ + x: A theano variable + y: A theano variable + z: A theano expression involving x and y + + Returns dz / dx + dz / dy + """ + + return sum(T.grad(z, [x, y])) + +x = T.scalar() +y = T.scalar() +z = x + y +s = grad_sum(x, y, z) +assert s.eval({x: 0, y: 0}) == 2 +print "SUCCESS!" diff --git a/ipnb/22_traverse_soln.py b/ipnb/22_traverse_soln.py new file mode 100644 index 0000000..6751147 --- /dev/null +++ b/ipnb/22_traverse_soln.py @@ -0,0 +1,59 @@ +import numpy as np +from theano.gof import Variable +from theano import tensor as T + + +def arg_to_softmax(prob): + """ + Oh no! Someone has passed you the probability output, + "prob", of a softmax function, and you want the unnormalized + log probability--the argument to the softmax. + + Verify that prob really is the output of a softmax. Raise a + TypeError if it is not. + + If it is, return the argument to the softmax. + """ + + if not isinstance(prob, Variable): + raise TypeError() + + if prob.owner is None: + raise TypeError() + + owner = prob.owner + + if not isinstance(owner.op, T.nnet.Softmax): + raise TypeError() + + rval, = owner.inputs + + return rval + +if __name__ == "__main__": + x = np.ones((5, 4)) + try: + arg_to_softmax(x) + raise Exception("You should have raised an error.") + except TypeError: + pass + + x = T.matrix() + try: + arg_to_softmax(x) + raise Exception("You should have raised an error.") + except TypeError: + pass + + y = T.nnet.sigmoid(x) + try: + arg_to_softmax(y) + raise Exception("You should have raised an error.") + except TypeError: + pass + + y = T.nnet.softmax(x) + rval = arg_to_softmax(y) + assert rval is x + + print "SUCCESS!" diff --git a/ipnb/31_debug_soln.py b/ipnb/31_debug_soln.py new file mode 100644 index 0000000..56bde73 --- /dev/null +++ b/ipnb/31_debug_soln.py @@ -0,0 +1,17 @@ +import numpy as np +from theano import function +from theano import tensor as T +from theano import config +config.compute_test_value = 'raise' +a = T.vector() +a.tag.test_value = np.ones((3,)).astype(a.dtype) +b = T.log(a) +c = T.nnet.sigmoid(b) +d = T.sqrt(c) +e = T.concatenate((d, c), axis=0) +f = b * c * d +# This is the first bad line +g = e + f +h = g / c +fn = function([a], h) +fn(np.ones((3,)).astype(a.dtype)) diff --git a/ipnb/Theano-basic.ipynb b/ipnb/Theano-basic.ipynb new file mode 100644 index 0000000..b826bb1 --- /dev/null +++ b/ipnb/Theano-basic.ipynb @@ -0,0 +1,623 @@ +{ + "metadata": { + "name": "", + "signature": "sha256:33e931e54d686a2ab2c44bfcdc99a4383aecf754e0c80fef2e0ada6858e9b48c" + }, + "nbformat": 3, + "nbformat_minor": 0, + "worksheets": [ + { + "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "All the exercices on this sheet work this way:\n", + "\n", + " 1. You have a cell with TODOs that raise errors with a description of what is needed. Do that.\n", + " 2. Then run the cell(ctrl-enter) to execute it.\n", + " 3. It should print \"Success\" at the end (there is validation code in the cell). If not, try again.\n", + " 4. If you want to see the solution, execute the cell that start with \"%load\" after the exercice." + ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 1. Builing expressions\n", + "\n", + "#### Excercice 1.1\n", + "\n", + "This exercice walks you through creating Theano variables and doing some computation with them." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import function\n", + "raise NotImplementedError(\"TODO: add any other imports you need\")\n", + "\n", + "\n", + "def make_scalar():\n", + " \"\"\"\n", + " Returns a new Theano scalar.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "def log(x):\n", + " \"\"\"\n", + " Returns the logarithm of a Theano scalar x.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "def add(x, y):\n", + " \"\"\"\n", + " Adds two theano scalars together and returns the result.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + " \n", + "# The following code uses your code and tests it.\n", + "a = make_scalar()\n", + "b = make_scalar()\n", + "c = log(b)\n", + "d = add(a, c)\n", + "f = function([a, b], d)\n", + "a = np.cast[a.dtype](1.)\n", + "b = np.cast[b.dtype](2.)\n", + "actual = f(a, b)\n", + "expected = 1. + np.log(2.)\n", + "assert np.allclose(actual, expected)\n", + "print \"SUCCESS!\"\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 01_scalar_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Exercice 1.2\n", + "\n", + "This exercice asks you to make Theano variables, elementwise multiplication and matrix/vector dot product.\n" + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import function\n", + "raise NotImplementedError(\"TODO: add any other imports you need\")\n", + "\n", + "\n", + "def make_vector():\n", + " \"\"\"\n", + " Returns a new Theano vector.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "def make_matrix():\n", + " \"\"\"\n", + " Returns a new Theano matrix.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "def elemwise_mul(a, b):\n", + " \"\"\"\n", + " a: A theano matrix\n", + " b: A theano matrix\n", + " Returns the elementwise product of a and b\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "def matrix_vector_mul(a, b):\n", + " \"\"\"\n", + " a: A theano matrix\n", + " b: A theano vector\n", + " Returns the matrix-vector product of a and b\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "# The following code uses your code and tests it.\n", + "a = make_vector()\n", + "b = make_vector()\n", + "c = elemwise_mul(a, b)\n", + "d = make_matrix()\n", + "e = matrix_vector_mul(d, c)\n", + "\n", + "f = function([a, b, d], e)\n", + "\n", + "rng = np.random.RandomState([1, 2, 3])\n", + "a_value = rng.randn(5).astype(a.dtype)\n", + "b_value = rng.rand(5).astype(b.dtype)\n", + "c_value = a_value * b_value\n", + "d_value = rng.randn(5, 5).astype(d.dtype)\n", + "expected = np.dot(d_value, c_value)\n", + "\n", + "actual = f(a_value, b_value, d_value)\n", + "assert np.allclose(actual, expected)\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 02_vector_mat_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Exercice 1.3\n", + "\n", + "This exercices asks you to create a tensor variable, do broadcastable additions and compute the max over part of a tensor." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import function\n", + "raise NotImplementedError(\"TODO: add any other imports you need\")\n", + "\n", + "\n", + "def make_tensor(dim):\n", + " \"\"\"\n", + " Returns a new Theano tensor with no broadcastable dimensions.\n", + " dim: the total number of dimensions of the tensor.\n", + " (You can use any dtype you like)\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "def broadcasted_add(a, b):\n", + " \"\"\"\n", + " a: a 3D theano tensor\n", + " b: a 4D theano tensor\n", + " Returns c, a 4D theano tensor, where\n", + "\n", + " c[i, j, k, l] = a[l, k, i] + b[i, j, k, l]\n", + "\n", + " for all i, j, k, l\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "def partial_max(a):\n", + " \"\"\"\n", + " a: a 4D theano tensor\n", + "\n", + " Returns b, a theano matrix, where\n", + "\n", + " b[i, j] = max_{k,l} a[i, k, l, j]\n", + "\n", + " for all i, j\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "# The following code uses your code and tests it.\n", + "a = make_tensor(3)\n", + "b = make_tensor(4)\n", + "c = broadcasted_add(a, b)\n", + "d = partial_max(c)\n", + "\n", + "f = function([a, b], d)\n", + "\n", + "rng = np.random.RandomState([1, 2, 3])\n", + "a_value = rng.randn(2, 2, 2).astype(a.dtype)\n", + "b_value = rng.rand(2, 2, 2, 2).astype(b.dtype)\n", + "c_value = np.transpose(a_value, (2, 1, 0))[:, None, :, :] + b_value\n", + "expected = c_value.max(axis=1).max(axis=1)\n", + "\n", + "actual = f(a_value, b_value)\n", + "\n", + "assert np.allclose(actual, expected), (actual, expected)\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 03_tensor_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 2. Compiling and Running\n", + "\n", + "#### Exercice 2.1\n", + "\n", + "This exercice asks you to compile a Theano function and call it. " + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from theano import tensor as T\n", + "raise NotImplementedError(\"TODO: add any other imports you need\")\n", + "\n", + "\n", + "def evaluate(x, y, expr, x_value, y_value):\n", + " \"\"\"\n", + " x: A theano variable\n", + " y: A theano variable\n", + " expr: A theano expression involving x and y\n", + " x_value: A numpy value\n", + " y_value: A numpy value\n", + "\n", + " Returns the value of expr when x_value is substituted for x\n", + " and y_value is substituted for y\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "# The following code use your code and test it.\n", + "x = T.iscalar()\n", + "y = T.iscalar()\n", + "z = x + y\n", + "assert evaluate(x, y, z, 1, 2) == 3\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 11_function_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Exercice 2.2\n", + "\n", + "This exercice makes you use shared variables. You must create some and update them by swapping 2 shared variables value." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "raise NotImplementedError(\"TODO: add any other imports you need\")\n", + "\n", + "\n", + "def make_shared(shape):\n", + " \"\"\"\n", + " Returns a theano shared variable containing a tensor of the specified\n", + " shape.\n", + " You can use any value you want.\n", + " \"\"\"\n", + " raise NotImplementedError(\"TODO: implement the function\")\n", + "\n", + "\n", + "def exchange_shared(a, b):\n", + " \"\"\"\n", + " a: a theano shared variable\n", + " b: a theano shared variable\n", + " Uses get_value and set_value to swap the values stored in a and b\n", + " \"\"\"\n", + " raise NotImplementedError(\"TODO: implement the function\")\n", + "\n", + "\n", + "def make_exchange_func(a, b):\n", + " \"\"\"\n", + " a: a theano shared variable\n", + " b: a theano shared variable\n", + " Returns f\n", + " where f is a theano function, that, when called, swaps the\n", + " values in a and b\n", + " f should not return anything\n", + " \"\"\"\n", + " raise NotImplementedError(\"TODO: implement the function\")\n", + "\n", + "\n", + "# The following code use your code and test it.\n", + "a = make_shared((5, 4, 3))\n", + "assert a.get_value().shape == (5, 4, 3)\n", + "b = make_shared((5, 4, 3))\n", + "assert a.get_value().shape == (5, 4, 3)\n", + "a.set_value(np.zeros((5, 4, 3), dtype=a.dtype))\n", + "b.set_value(np.ones((5, 4, 3), dtype=b.dtype))\n", + "exchange_shared(a, b)\n", + "assert np.all(a.get_value() == 1.)\n", + "assert np.all(b.get_value() == 0.)\n", + "f = make_exchange_func(a, b)\n", + "rval = f()\n", + "assert isinstance(rval, list)\n", + "assert len(rval) == 0\n", + "assert np.all(a.get_value() == 0.)\n", + "assert np.all(b.get_value() == 1.)\n", + "\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 12_shared_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Exercice 2.3\n", + "\n", + "Something weird happens when you run this code, find the problem. Explain what is happening.\n", + "\n", + "Hint: some compilation modes make the problem more obvious than others." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import function\n", + "from theano import tensor as T\n", + "x = T.vector()\n", + "y = T.vector()\n", + "z = T.zeros_like(y)\n", + "a = x + z\n", + "f = function([x, y], a)\n", + "output = f(np.zeros((1,), dtype=x.dtype), np.zeros((2,), dtype=y.dtype))" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 13_bug_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 3. Modifying Graphs\n", + "\n", + "#### Exercice 3.1\n", + "\n", + "This exercice makes you use the Theano symbolic grad." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from theano import tensor as T\n", + "\n", + "\n", + "def grad_sum(x, y, z):\n", + " \"\"\"\n", + " x: A theano variable\n", + " y: A theano variable\n", + " z: A theano expression involving x and y\n", + "\n", + " Returns dz / dx + dz / dy\n", + " \"\"\"\n", + " raise NotImplementedError(\"TODO: implement this function.\")\n", + "\n", + "\n", + "# The following code use your code and test it.\n", + "x = T.scalar()\n", + "y = T.scalar()\n", + "z = x + y\n", + "s = grad_sum(x, y, z)\n", + "assert s.eval({x: 0, y: 0}) == 2\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 21_grad_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "#### Exercice 3.2\n", + "\n", + "This exercice is here to show you how to navigate in a Theano graph. You will need to find the inputs used to produce\n", + "some computation." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import tensor as T\n", + "raise NotImplementedError(\"Add any imports you need.\")\n", + "\n", + "\n", + "def arg_to_softmax(prob):\n", + " \"\"\"\n", + " Oh no! Someone has passed you the probability output,\n", + " \"prob\", of a softmax function, and you want the unnormalized\n", + " log probability--the argument to the softmax.\n", + "\n", + " Verify that prob really is the output of a softmax. Raise a\n", + " TypeError if it is not.\n", + "\n", + " If it is, return the argument to the softmax.\n", + " \"\"\"\n", + "\n", + " raise NotImplementedError(\"Implement this function.\")\n", + "\n", + "\n", + "x = np.ones((5, 4))\n", + "try:\n", + " arg_to_softmax(x)\n", + " raise Exception(\"You should have raised an error.\")\n", + "except TypeError:\n", + " pass\n", + "\n", + "x = T.matrix()\n", + "try:\n", + " arg_to_softmax(x)\n", + " raise Exception(\"You should have raised an error.\")\n", + "except TypeError:\n", + " pass\n", + "\n", + "y = T.nnet.sigmoid(x)\n", + "try:\n", + " arg_to_softmax(y)\n", + " raise Exception(\"You should have raised an error.\")\n", + "except TypeError:\n", + " pass\n", + "\n", + "y = T.nnet.softmax(x)\n", + "rval = arg_to_softmax(y)\n", + "assert rval is x\n", + "\n", + "print \"SUCCESS!\"" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 22_traverse_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## 4. Debugging\n", + "\n", + "#### Exercice 4.1\n", + "\n", + "The code in the next cell has a bug. Run the cell to see it.\n", + "\n", + "Use Theano flags or extra parameters to function() to find the cause.\n", + "\n", + "Don't try to find the bug by inspection of prints, the point of the exercice is to get you to work with the theano debugging tools that will be required for more complex code.\n", + "\n", + "To modify the environement for a cell use the `%env` magic command like this:\n", + "\n", + " %env THEANO_FLAGS=floatX=float32\n", + "\n", + "You will have to restart the ipython kernel from the Kernel menu above to get the enviroment changes to work." + ] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import numpy as np\n", + "from theano import function\n", + "from theano import tensor as T\n", + "a = T.vector()\n", + "b = T.log(a)\n", + "c = T.nnet.sigmoid(b)\n", + "d = T.sqrt(c)\n", + "e = T.concatenate((d, c), axis=0)\n", + "f = b * c * d\n", + "g = e + f\n", + "h = g / c\n", + "fn = function([a], h)\n", + "fn(np.ones((3,)).astype(a.dtype))" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load 31_debug_soln.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + } + ], + "metadata": {} + } + ] +} \ No newline at end of file