I have used python for some specific machine learning problems, and I have found that usually python is a lot slower than C/C++. While using theano, how much speed will it catch up when competing with C/C++?

And if my goal is to do scientific experiments or competitions, in what cases should I use python(theano) and in which case I should use C/C++?

asked May 07 '13 at 08:14

chentingpc's gravatar image

chentingpc
46113

edited May 07 '13 at 08:14


3 Answers:

I would second most of what Sander has said, but would add some details, namely:

  • Theano provides very good speed, especially considering the abstraction you get. When working in a lower level language you can get into a lot of beard twirling over which data structure to use, or how to manage references to your working data, when that's really not the problem you want to solve.

  • Theano has a non-trivial learning curve. I suppose it's going to vary from person to person, but in my opinion Sander is giving it a bit of a soft touch. If you're in a situation where a learning curve is too risky, don't go with theano. In particular, if you don't have access to a unix/linux environment, do not use theano, especially if you're planning to target the GPU. In theory it works, and there are some decent instructions on getting things going on Windows. I have gotten it to (mostly) work, but it was practically quite challenging to get right. [Theano people: if this situation has changed in recent months, by all means, correct me]

  • Regarding using Enthought's Canopy distribution: It's really great as far as getting a cohesive library setup, and as Sander says they do lots of hand tuning of the set up, and they link to the MKL so you get that benefit if you're on Intel hardware. However, if you're planning to use Theano with EPD, run the test suit for numpy, and then for Theano! It's tempting to skip, as it can take a long time, but it's a complicated toolchain and lots of things can go wrong. Taking the time to make sure all your ducks are in a row for the hardware you're on will save you time in the end (speaking from experience here). Again, EPD+MKL on windows can be a little problematic, although less so than Theano in my experience.

  • As an alternative to Canopy/EPD, take a look at Continuum Analytics' Anaconda Distribution. It's very similar to EPD, but in my opinion it stands to out-pace EPD very soon. Continuum is championing the development of some great tools such as Blaze, Wakari, and Numba. I won't get into those in detail here, but for example, Blaze is aiming to be the successor to Numpy. Note: It's not there yet, and I am in no way affiliated with Continuum Analytics.

  • When writing python code that needs to be fast, profile it to figure out where the bottle neck is, and then drop that piece into C using one of the many tools such as Cython, Weave, etc. More recently I've started to like FFI a little more than Cython, although it's less mature. Similarly, I've seen some great things done with CA's Numba (e.g. C-level performance on pure python code simply by adding a decorator). It's most important to set out what is your critical constraint ahead of time, though. Do you need to be able to get something working and experiment with it (most common in ML); Do you absolutely, positively need the fastest training time you can get, and development time is no object (pun not intended)?; Do you need future portability for when it goes into production? Think about questions like that for a bit and the dizzying array of (great) solutions/tools will quickly shrink down to one or two.

  • My advice is, if you are doing ML research and/or competitions, just jump into the python ecosystem with one of the distributions and get going. The tools such as theano, cython, ffi, weave, numba will get you so close to "the fastest" that it won't matter. If you're using a large templated C++ library in a non-trivial way, you might spend more time at each change waiting for it to compile and link with all the dependencies than a theano implementation would take to finish training your model.

  • If you do decide to go with C++, CUV is a nice library that lets you do some of the same things (i.e. GPGPU computation) as theano, and it's been used for many of the same things as theano is used for, such as neural networks, RBMs, etc.

answered May 07 '13 at 22:35

Kyzyl%20Herzog's gravatar image

Kyzyl Herzog
371144

edited May 08 '13 at 06:38

Sander%20Dieleman's gravatar image

Sander Dieleman
155672734

I didn't really consider that setting up all this stuff on a Windows platform can be a little more involved, good catch. Linux is probably the better platform for this kind of work at the moment, especially when using Theano.

Thanks for mentioning Anaconda, I'd heard the name before but I didn't know what it is. Looks like a good alternative to EPD, I'm going to give it a try since I can't get Canopy to work on my machine.

I fixed the links in your post as well :)

EDIT: it would actually be very interesting to see a more in-depth comparison between EPD and Anaconda at this point, maybe also with some performance info. All I've found so far is this: http://stackoverflow.com/questions/15762943/anaconda-vs-epd-enthought-vs-manual-installation-of-python

(May 08 '13 at 06:44) Sander Dieleman

I never really code C/C++ myself so I can't compare, but I've been very satisfied with Theano's performance so far. I'm sure writing optimised C code yourself can't be beaten in terms of performance, but it does take a lot more time and effort :) Writing Theano code is only a little bit harder than using numpy (you just have to wrap your head around the symbolic variables and expressions, after that it's a breeze).

Especially if you have an nvidia GPU, using Theano can result in huge speed gains. I can't give you any numbers, sorry. In practice, Theano might even be faster than writing the code yourself sometimes, since it may discover optimisations that you missed.

In general, when using Python for ML, I strongly recommend using the Enthought Python Distribution, which is free for academic use. It contains an optimised version of numpy compiled with the Intel MKL blas library. EPD's Numpy is much, much faster compared to a vanilla install (I've seen speed gains between 10x and 100x). It seems like they're overhauling it at the moment though, apparently it's now called 'Canopy'. I haven't been able to get the new version installed correctly, so using an older version might be preferable.

And finally, if you're writing code that numpy / Theano can't speed up, consider having a look at Cython, which lets you compile your Python code to C, and defines some extra syntax for things like static typing etc, to further speed up the code. I've found this much less daunting than switching to C/C++ entirely, but I guess that could be different for you if you are more familiar with those languages.

answered May 07 '13 at 08:34

Sander%20Dieleman's gravatar image

Sander Dieleman
155672734

If using a CPU you should be able to attain the same kind of speeds from Theano as compared to hand crafted mathematical c code. If you are using GPU's life is reasonably easy with Theano, with an excellent associated increase in runtime speed.

Theano lets you code in python, and makes life easy for use with GPUs.

Some links:

http://deeplearning.net/software/theano/introduction.html

http://gpuscience.com/articles/theano-a-cpu-and-gpu-math-compiler-in-python/

answered May 07 '13 at 13:59

amair's gravatar image

amair
2452312

Your answer
toggle preview

powered by OSQA

User submitted content is under Creative Commons: Attribution - Share Alike; Other things copyright (C) 2010, MetaOptimize LLC.