Last active
November 12, 2015 15:57
-
-
Save lucastheis/7696ebbeeaf2ed0c2545 to your computer and use it in GitHub Desktop.
Don't mix integers with floats in Theano.
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
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 1, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"import os" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 2, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"os.environ['THEANO_FLAGS'] = 'floatX=float32,device=gpu1,nvcc.fastmath=True'" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 3, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stderr", | |
"output_type": "stream", | |
"text": [ | |
"Using gpu device 1: Tesla K40m (CNMeM is disabled)\n" | |
] | |
} | |
], | |
"source": [ | |
"import numpy as np\n", | |
"import theano as th\n", | |
"import theano.tensor as tt" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 4, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"A = tt.matrix('A')\n", | |
"B = tt.matrix('B')\n", | |
"\n", | |
"A_shape_0_ = tt.cast(A.shape[0], th.config.floatX)\n", | |
"\n", | |
"f_gpu = th.function([A, B], tt.dot(A, B) / A_shape_0_)\n", | |
"f_cpu = th.function([A, B], tt.dot(A, B) / A.shape[0])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 5, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"HostFromGpu [@A] '' 8\n", | |
" |GpuElemwise{TrueDiv}[(0, 0)] [@B] '' 7\n", | |
" |GpuDot22 [@C] '' 4\n", | |
" | |GpuFromHost [@D] '' 1\n", | |
" | | |A [@E]\n", | |
" | |GpuFromHost [@F] '' 2\n", | |
" | |B [@G]\n", | |
" |GpuFromHost [@H] '' 6\n", | |
" |Elemwise{Cast{float32}} [@I] '' 5\n", | |
" |InplaceDimShuffle{x,x} [@J] '' 3\n", | |
" |Shape_i{0} [@K] '' 0\n", | |
" |A [@E]\n" | |
] | |
} | |
], | |
"source": [ | |
"th.printing.debugprint(f_gpu)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 6, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"Elemwise{true_div,no_inplace} [@A] '' 6\n", | |
" |HostFromGpu [@B] '' 5\n", | |
" | |GpuDot22 [@C] '' 4\n", | |
" | |GpuFromHost [@D] '' 1\n", | |
" | | |A [@E]\n", | |
" | |GpuFromHost [@F] '' 2\n", | |
" | |B [@G]\n", | |
" |InplaceDimShuffle{x,x} [@H] '' 3\n", | |
" |Shape_i{0} [@I] '' 0\n", | |
" |A [@E]\n" | |
] | |
} | |
], | |
"source": [ | |
"th.printing.debugprint(f_cpu)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": 7, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [ | |
{ | |
"name": "stdout", | |
"output_type": "stream", | |
"text": [ | |
"100 loops, best of 3: 3 ms per loop\n", | |
"100 loops, best of 3: 4.21 ms per loop\n" | |
] | |
} | |
], | |
"source": [ | |
"A = np.asarray(np.random.randn(1000, 1000), dtype=th.config.floatX)\n", | |
"B = np.asarray(np.random.randn(1000, 1000), dtype=th.config.floatX)\n", | |
"\n", | |
"%timeit f_gpu(A, B)\n", | |
"%timeit f_cpu(A, B)" | |
] | |
} | |
], | |
"metadata": { | |
"kernelspec": { | |
"display_name": "Python 3", | |
"language": "python", | |
"name": "python3" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.4.0" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 0 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment