Last active
April 25, 2016 19:43
-
-
Save xiangze/0f800305ee67b229577a to your computer and use it in GitHub Desktop.
This file contains 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:9c91ec5b7622299c143db260ad3a67d14aa971fe254744898714186ad2bb510d" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Theano Loop & Iteration(map)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- http://deeplearning.net/software/theano/tutorial/loop.html\n", | |
"- http://deeplearning.net/software/theano/library/scan.html\n", | |
"- http://sinhrks.hatenablog.com/entry/2015/04/25/233025" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"from theano import function, config, shared, sandbox\n", | |
"import theano\n", | |
"import theano.tensor as T\n", | |
"import numpy as np\n", | |
"import time\n" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stderr", | |
"text": [ | |
"Using gpu device 0: GeForce GT 620\n" | |
] | |
} | |
], | |
"prompt_number": 1 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fx2=lambda prior,n: prior * 2" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [], | |
"prompt_number": 2 | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"- Loop\n", | |
"\n", | |
"-- \u95a2\u6570\u3092\u7573\u307f\u8fbc\u3093\u3067\u3044\u3063\u305f\u7d50\u679c\u3092\u51fa\u529b \n", | |
"\n", | |
"-- \u5f15\u6570non_sequenses\u3092\u6307\u5b9a\n", | |
"\n", | |
"- iteraion(map)\n", | |
"\n", | |
"-- \u95a2\u6570\u3092tensor\u306e\u5404\u8981\u7d20\u306b\u9069\u7528\u3057\u305f\u3082\u306e\u3092\u51fa\u529b \n", | |
"\n", | |
"-- \u5f15\u6570sequences\u3092\u6307\u5b9a\n", | |
"\n", | |
"--theano.map\u3067\u540c\u7b49\u306e\u3053\u3068\u304c\u3067\u304d\u308b\u3002\n", | |
"\n", | |
"\u8a73\u3057\u304f\u306f\n", | |
"http://sinhrks.hatenablog.com/entry/2015/04/25/233025" | |
] | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Loop" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"[fn(x), fn(fn(x))...]" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"n = T.iscalar('n')\n", | |
"v = T.ivector('v')\n", | |
"result, updates = theano.scan(fx2,\n", | |
" sequences=None,\n", | |
" outputs_info=v,\n", | |
" non_sequences=v,\n", | |
" n_steps=n)\n", | |
" \n", | |
"floop = theano.function(inputs=[v, n], outputs=result[-1], updates=updates)\n", | |
"print floop([1, 2, 3], 3)" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 8 16 24]\n" | |
] | |
} | |
], | |
"prompt_number": 4 | |
}, | |
{ | |
"cell_type": "heading", | |
"level": 1, | |
"metadata": {}, | |
"source": [ | |
"Iteration(map)" | |
] | |
}, | |
{ | |
"cell_type": "markdown", | |
"metadata": {}, | |
"source": [ | |
"[fn(x) for x in ...]\n", | |
"\n", | |
"map\u3092\u3064\u304b\u3046\u3068\u3044\u3044\u304b\u3082" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"a = T.ivector('a')\n", | |
"outputs = T.as_tensor_variable(np.asarray(0))\n", | |
"result, updates = theano.scan(fx2,\n", | |
" sequences=[a],\n", | |
" outputs_info=outputs,\n", | |
" non_sequences=None)\n", | |
" \n", | |
"fite = theano.function(inputs=[a], outputs=result)#, updates=updates)\n", | |
"print fite([5,6,7])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[10 12 14]\n" | |
] | |
} | |
], | |
"prompt_number": 5 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"result, updates = theano.scan(fn=lambda seq, prior: seq + prior,\n", | |
" sequences=[a],\n", | |
" outputs_info=outputs,\n", | |
" non_sequences=None)\n", | |
"\n", | |
"fite2 = theano.function(inputs=[a], outputs=result)#, updates=updates)\n", | |
"print fite2([5,6,7])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 5 11 18]\n" | |
] | |
} | |
], | |
"prompt_number": 8 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"m = T.imatrix('m')\n", | |
"result, updates = theano.scan(lambda n:n+2,\n", | |
" sequences=[m],\n", | |
" non_sequences=None)\n", | |
"\n", | |
"fite_m=theano.function([m],result)\n", | |
"print fite_m([[1,2,3],[4,5,6]])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[3 4 5]\n", | |
" [6 7 8]]\n" | |
] | |
} | |
], | |
"prompt_number": 7 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"X=T.scalar('X')\n", | |
"A=T.vector('A')\n", | |
"fx=lambda x,y:x*y\n", | |
"\n", | |
"fm,_=theano.map(fx,\n", | |
" sequences=[A], \n", | |
" non_sequences=X\n", | |
" )\n", | |
"fmapx = theano.function(inputs=[X,A], outputs=fm) \n", | |
"print fmapx(4,[1,2,3]) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[ 4. 8. 12.]\n" | |
] | |
} | |
], | |
"prompt_number": 15 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"fm,_=theano.map(fx,\n", | |
" sequences=[m], \n", | |
" non_sequences=X\n", | |
" )\n", | |
"fmapxm = theano.function(inputs=[X,m], outputs=fm) \n", | |
"print fmapxm(4,[[1,2,3],[4,5,6]]) " | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 4. 8. 12.]\n", | |
" [ 16. 20. 24.]]\n" | |
] | |
} | |
], | |
"prompt_number": 18 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"V=T.vector('V')\n", | |
"fm,_=theano.map(lambda x,v:x+v,\n", | |
" sequences=[m], \n", | |
" non_sequences=V\n", | |
" )\n", | |
"fmapxmv = theano.function(inputs=[V,m], outputs=fm) \n", | |
"print fmapxmv([2,2,2],[[1,2,3],[4,5,6]])" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"[[ 3. 4. 5.]\n", | |
" [ 6. 7. 8.]]\n" | |
] | |
} | |
], | |
"prompt_number": 26 | |
}, | |
{ | |
"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