Created
June 14, 2015 16:24
-
-
Save xiangze/9385de1d8194160295f9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:3acbd332f21cc0cbb0c9d37e650e384a2e8b4fe4b327e83dd98d0c7255712dd5" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"import theano\n", | |
"from theano import function, sandbox, shared\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": 1 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Switch to vector,tensor" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def sel(a,b):\n", | |
" return T.switch(a<b,a,b)\n", | |
"#a=T.tensor3(\"A\")\n", | |
"a=T.vector(\"a\")\n", | |
"b=T.vector(\"b\")\n", | |
"\n", | |
"f=function([a,b],sel(a,b))\n", | |
"\n", | |
"f([1,2,3],[4,1,13])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 66, | |
"text": [ | |
"array([ 1., 1., 3.], dtype=float32)" | |
] | |
} | |
], | |
"prompt_number": 66 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a=T.matrix(\"a\")\n", | |
"b=T.matrix(\"b\")\n", | |
"#https://groups.google.com/forum/#!topic/theano-users/YfrhjjOiyAQ\n", | |
"def reshape1(a):\n", | |
" return T.reshape(a,(1,T.shape(a)[0]*T.shape(a)[1]))\n", | |
"\n", | |
"def reshape1T(a):\n", | |
" return T.reshape(a,(T.shape(a)[0]*T.shape(a)[1],1))\n", | |
"#(T.shape(x)[0]*2,T.int_div(T.shape(x)[0],2)) \n", | |
"\n", | |
"def switchMin(a,b):\n", | |
" aa=reshape1(a)\n", | |
" bb=reshape1(b)\n", | |
" cc=T.switch(aa<bb,aa,bb)\n", | |
" return T.reshape(cc,(T.shape(a)[0],T.shape(a)[1]))\n", | |
"\n", | |
"def switchMax(a,b):\n", | |
" aa=reshape1(a)\n", | |
" bb=reshape1(b)\n", | |
" cc=T.switch(aa>bb,aa,bb)\n", | |
" return T.reshape(cc,(T.shape(a)[0],T.shape(a)[1]))\n", | |
"\n", | |
"fmin=function([a,b],switchMin(a,b),allow_input_downcast=True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 75 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a=np.array(range(16),dtype=float)\n", | |
"a=a.reshape(4,4)\n", | |
"b=a+np.random.uniform(-1,1,size=16).reshape(4,4)\n", | |
"print a\n", | |
"print b" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 0. 1. 2. 3.]\n", | |
" [ 4. 5. 6. 7.]\n", | |
" [ 8. 9. 10. 11.]\n", | |
" [ 12. 13. 14. 15.]]\n", | |
"[[ -0.81269797 1.3718386 1.53156345 2.31660372]\n", | |
" [ 3.86150118 4.15623634 6.75110587 6.54735707]\n", | |
" [ 7.92685634 8.614342 10.64430822 10.17661102]\n", | |
" [ 12.15359756 13.85438625 14.28163421 14.25680402]]\n" | |
] | |
} | |
], | |
"prompt_number": 73 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fmin(a,b)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"metadata": {}, | |
"output_type": "pyout", | |
"prompt_number": 74, | |
"text": [ | |
"array([[ -0.81269795, 1. , 1.5315634 , 2.31660366],\n", | |
" [ 3.86150122, 4.15623617, 6. , 6.54735708],\n", | |
" [ 7.92685652, 8.61434174, 10. , 10.17661095],\n", | |
" [ 12. , 13. , 14. , 14.25680447]], dtype=float32)" | |
] | |
} | |
], | |
"prompt_number": 74 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Dimshuffle" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"http://deeplearning.net/software/theano/library/tensor/basic.html#theano.tensor._tensor_py_operators.dimshuffle" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a=T.tensor3('a')\n", | |
"f=function([a],a.dimshuffle(1,0,2),allow_input_downcast=True)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 81 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"k=np.array(range(27),dtype=float)\n", | |
"\n", | |
"k=k.reshape(3,3,3)\n", | |
"print k\n", | |
"print f(k)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[[ 0. 1. 2.]\n", | |
" [ 3. 4. 5.]\n", | |
" [ 6. 7. 8.]]\n", | |
"\n", | |
" [[ 9. 10. 11.]\n", | |
" [ 12. 13. 14.]\n", | |
" [ 15. 16. 17.]]\n", | |
"\n", | |
" [[ 18. 19. 20.]\n", | |
" [ 21. 22. 23.]\n", | |
" [ 24. 25. 26.]]]\n", | |
"[[[ 0. 1. 2.]\n", | |
" [ 9. 10. 11.]\n", | |
" [ 18. 19. 20.]]\n", | |
"\n", | |
" [[ 3. 4. 5.]\n", | |
" [ 12. 13. 14.]\n", | |
" [ 21. 22. 23.]]\n", | |
"\n", | |
" [[ 6. 7. 8.]\n", | |
" [ 15. 16. 17.]\n", | |
" [ 24. 25. 26.]]]\n" | |
] | |
} | |
], | |
"prompt_number": 82 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Hamilton\u7a4d\u5206\u306eTensor\u4e26\u5217\u5316" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"def dynamics(x0, v0, stepsize, n_steps, logP):\n", | |
"\n", | |
" def leapfrog(p, v, step):\n", | |
" dE = T.grad(logP(p).sum(), p)\n", | |
" vn=v-step*dE\n", | |
" xn=p+vn*step\n", | |
" return [xn,vn], {}\n", | |
"\n", | |
" E0 = logP(x0)\n", | |
" dE = T.grad(E0.sum(), x0)\n", | |
"\n", | |
" (xs, vs), scan_updates = theano.scan(leapfrog,\n", | |
" outputs_info=[dict(initial=x0+stepsize*v_half), dict(initial=v0-stepsize/2*dE),],\n", | |
" non_sequences=[stepsize],\n", | |
" n_steps=n_steps - 1)\n", | |
"\n", | |
" final_x,final_v = xs[-1],vs[-1]\n", | |
"\n", | |
" assert not scan_updates\n", | |
" E=logP(final_x)\n", | |
" final_v = final_v - stepsize/2 * T.grad(E.sum(), final_x)\n", | |
" return final_x, final_v" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 94 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from scipy import linalg" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 86 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"dim=3\n", | |
"seed=120\n", | |
"\n", | |
"rng = np.random.RandomState(seed)\n", | |
"mu = np.array(rng.rand(dim) * 10, dtype=theano.config.floatX)\n", | |
"cov = np.array(rng.rand(dim, dim), dtype=theano.config.floatX)\n", | |
"cov = (cov + cov.T) / 2.\n", | |
"cov[np.arange(dim), np.arange(dim)] = 1.0\n", | |
"cov_inv = linalg.inv(cov)\n", | |
"\n", | |
"gaussian_energy=lambda x:(T.dot((x - mu), cov_inv) * (x - mu)).sum(axis=1)/2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 98 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"din=lambda x,v:dynamics(x,v,1,100,gaussian_energy)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 95 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"v=T.vector('v')\n", | |
"T.grad(gaussian_energy(v),v)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"ename": "ValueError", | |
"evalue": "Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis 1", | |
"output_type": "pyerr", | |
"traceback": [ | |
"\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mValueError\u001b[0m Traceback (most recent call last)", | |
"\u001b[1;32m<ipython-input-97-461572e5f2ed>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m()\u001b[0m\n\u001b[0;32m 1\u001b[0m \u001b[0mv\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mvector\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'v'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m \u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mgrad\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mgaussian_energy\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0mv\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32m<ipython-input-87-af0492ba03e6>\u001b[0m in \u001b[0;36m<lambda>\u001b[1;34m(x)\u001b[0m\n\u001b[0;32m 9\u001b[0m \u001b[0mcov_inv\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mlinalg\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0minv\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mcov\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 11\u001b[1;33m \u001b[0mgaussian_energy\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;32mlambda\u001b[0m \u001b[0mx\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mT\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mdot\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mmu\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mcov_inv\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m*\u001b[0m \u001b[1;33m(\u001b[0m\u001b[0mx\u001b[0m \u001b[1;33m-\u001b[0m \u001b[0mmu\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[1;36m1\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m/\u001b[0m\u001b[1;36m2\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m", | |
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\tensor\\var.pyc\u001b[0m in \u001b[0;36msum\u001b[1;34m(self, axis, dtype, keepdims, acc_dtype)\u001b[0m\n\u001b[0;32m 450\u001b[0m return theano.tensor.basic.sum(self, axis=axis,\n\u001b[0;32m 451\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mkeepdims\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 452\u001b[1;33m acc_dtype=acc_dtype)\n\u001b[0m\u001b[0;32m 453\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 454\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mprod\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mFalse\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0macc_dtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mNone\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\tensor\\basic.pyc\u001b[0m in \u001b[0;36msum\u001b[1;34m(input, axis, dtype, keepdims, acc_dtype)\u001b[0m\n\u001b[0;32m 2668\u001b[0m \"\"\"\n\u001b[0;32m 2669\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 2670\u001b[1;33m \u001b[0mout\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0melemwise\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSum\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mdtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdtype\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0macc_dtype\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0macc_dtype\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 2671\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 2672\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mkeepdims\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\gof\\op.pyc\u001b[0m in \u001b[0;36m__call__\u001b[1;34m(self, *inputs, **kwargs)\u001b[0m\n\u001b[0;32m 397\u001b[0m \"\"\"\n\u001b[0;32m 398\u001b[0m \u001b[0mreturn_list\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mkwargs\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'return_list'\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mFalse\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 399\u001b[1;33m \u001b[0mnode\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmake_node\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0minputs\u001b[0m\u001b[1;33m,\u001b[0m \u001b[1;33m**\u001b[0m\u001b[0mkwargs\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 400\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_stack_trace_on_call\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 401\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0madd_tag_trace\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mnode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\tensor\\elemwise.pyc\u001b[0m in \u001b[0;36mmake_node\u001b[1;34m(self, input)\u001b[0m\n\u001b[0;32m 1795\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1796\u001b[0m \u001b[1;32massert\u001b[0m \u001b[0mop\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0macc_dtype\u001b[0m \u001b[1;32mis\u001b[0m \u001b[1;32mnot\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1797\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mCAReduce\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mmake_node\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mop\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0minput\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 1798\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1799\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;32mC:\\Python27\\lib\\site-packages\\theano\\tensor\\elemwise.pyc\u001b[0m in \u001b[0;36mmake_node\u001b[1;34m(self, input)\u001b[0m\n\u001b[0;32m 1273\u001b[0m raise ValueError((\n\u001b[0;32m 1274\u001b[0m \u001b[1;34m'Not enough dimensions on %s to reduce on axis %s'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m-> 1275\u001b[1;33m % (input, axis)))\n\u001b[0m\u001b[0;32m 1276\u001b[0m \u001b[0minput\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mas_tensor_variable\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0minput\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 1277\u001b[0m \u001b[0maxis\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0maxis\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", | |
"\u001b[1;31mValueError\u001b[0m: Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis 1" | |
] | |
} | |
], | |
"prompt_number": 97 | |
}, | |
{ | |
"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