{ "metadata": { "name": "", "signature": "sha256:d1c82017224d0c9a84cea8e4b6c0b3e3b3277c83922ded5c17cf0838d9e6e825" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "from sympy import *\n", "init_printing(use_latex='mathjax')\n", "\n", "x = symbols('x')" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Code Generation" ] }, { "cell_type": "code", "collapsed": false, "input": [ "expr = R_nl(3, 1, x, 6)\n", "expr" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from sympy.utilities.autowrap import ufuncify\n", "from sympy.utilities.lambdify import lambdify\n", "\n", "fn_numpy = lambdify(x, expr, 'numpy')\n", "fn_fortran = ufuncify([x], expr)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### They work the same" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from numpy import linspace\n", "xx = linspace(0, 1, 5)\n", "fn_numpy(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fn_fortran(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Plot form with matplotlib" ] }, { "cell_type": "code", "collapsed": false, "input": [ "from matplotlib.pyplot import plot, show, legend\n", "%matplotlib inline" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "xx = linspace(0, 5, 50000) # A bigger \n", "plot(xx, fn_numpy(xx))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Time fn_numpy" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Time fn_fortran" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Theano" ] }, { "cell_type": "code", "collapsed": false, "input": [ "import theano.tensor as T\n", "import theano" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "tx = T.vector('x')\n", "ty = T.vector('y')\n", "tz = tx + 2 * ty\n", "tz" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Print z, see expression" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "# Make function\n", "f = theano.function([tx, ty], tz)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "import numpy as np\n", "xx = np.array([1, 2, 3], dtype=np.float32)\n", "yy = np.array([10, 20, 30], dtype=np.float32)\n", "f(xx, yy)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Theano compiles symbolic trees down to numeric C and CUDA code\n", "\n", "Less sophisticated mathematics but more sophisticated code generation\n", "\n", "(Almost) all SymPy expressions can be transformed into Theano Expressions." ] }, { "cell_type": "code", "collapsed": false, "input": [ "a = Symbol('a')\n", "from sympy.printing.theanocode import theano_code\n", "theano_code(a)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "theano.printing.debugprint(theano_code(expr))" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "from sympy.printing.theanocode import theano_function\n", "\n", "fn_theano = theano_function([x], [expr], dims={x: 1}, dtypes={x: 'float64'})" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "theano.printing.debugprint(fn_theano)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fn_theano(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# More complex problem\n", "\n", "Lets compute both the expression and its derivative simultaneously. " ] }, { "cell_type": "code", "collapsed": false, "input": [ "outputs = expr, simplify(expr.diff(x))\n", "outputs" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "fn_numpy = lambdify([x], outputs, 'numpy')\n", "fn_theano = theano_function([x], outputs, dims={x: 1}, dtypes={x: 'float64'})\n", "\n", "fns_fortran = [ufuncify([x], output) for output in outputs]\n", "fn_fortran = lambda xx: [fn_fortran(xx) for fn_fortran in fns_fortran]" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "xx = linspace(0, 5, 50000)\n", "\n", "for y in fn_theano(xx):\n", " plot(xx, y)\n", "legend(['$R_{31}$', \"$R'_{31}$\"])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "timeit fn_numpy(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "timeit fn_fortran(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "timeit fn_theano(xx)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Matrices" ] }, { "cell_type": "code", "collapsed": false, "input": [ "n = Symbol('n', integer=True)\n", "X = MatrixSymbol('X', n, n)\n", "y = MatrixSymbol('y', n, 1)\n", "\n", "f = theano_function([X, y], [X*y])" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [ "nX = np.array([[1, 0], [0, 1]], dtype=np.float32)\n", "ny = np.array([[10], [1]], dtype=np.float32)\n", "\n", "f(nX, ny)" ], "language": "python", "metadata": {}, "outputs": [] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }