6 ms·
As someone who has read a lot of implementing neural networks from articles, the massive problem with all of them is that they import numpy. You may think that
by fartcannon 7y ago
As someone who has read a lot of implementing neural networks from articles, the massive problem with all of them is that they import numpy. You may think that it is silly to reimplement the matrix math but with out that part of the code, you can't easily port it to other languages/microcontrollers/microwaves/badgers.
It's a legitimately valid part of machine learning, and its not easy to do for novices.
And I need help putting it on my badger damn it!
- simias 7y agoIf you don't care much about performance (and if you are reimplementing a neural network from scratch you're probably doing it more as a learning project than anything else) implementing matrix operations isn't very difficult. If you're not used to work with matrices simply reading the Wikipedia article might tell you enough to implement them yourself.
- peterhj 7y agoIf you have an assembler or C compiler you can implement matrix multiplication (GEMM) which usually does most of the heavy lifting in your neural net. Now you correctly alluded that it may not be simple to efficiently implement GEMM but if you have a simple architecture without a complex memory hierarchy then using whatever SIMD facilities are available and some standard tricks will get you in ballpark of peak FLOP/s. Or, just download a fast BLAS from your hardware vendor...
- whatshisface 7y agoThe complicated parts of Numpy are themselves a wrapper for the seminal LAPACK: http://www.netlib.org/lapack/ http://www.netlib.org/lapack/. It has C language APIs, so that might help you with what you need to do.
- fartcannon 7y agoThat's very interesting, thank you.
- Anon84 7y agoAs someone who does teach tutorials as a side gig, I would argue that implementing matrix operations in a tutorial on neural networks is overkill. No matter what the level of the tutorial you always need to draw a line and assume a certain amount of background knowledge and knowing how to use standard tools isn't too much to ask. (yes, I know numpy isn't part of python's standard library, but it comes with pretty much any Python distribution as many other libraries depend on it.) If we're talking about a longer format, such as a book, then we might consider digging deeper and implementing as much as possible using the barest of Python requirements. Indeed, Joel Grus does implement everything from scratch in his great (although a bit dated) book https://www.amazon.com/Data-Science-Scratch-Principles-Python/dp/149190142X https://www.amazon.com/Data-Science-Scratch-Principles-Pytho.... EDIT: This is still a work in progress (and relies on numpy and matplotlib), but here is my version: https://github.com/DataForScience/DeepLearning https://github.com/DataForScience/DeepLearning These notebooks are meant as support for a webinar so they might not be the clearest as standalone, but you also have the slides there.
- HuShifang 7y agoA new edition of Grus comes out next week actually... https://www.amazon.com/Data-Science-Scratch-Principles-Python/dp/1492041130 https://www.amazon.com/Data-Science-Scratch-Principles-Pytho...
- Anon84 7y agoNice! He mentioned he was working on it when I met him at Strata last year, but I didn't know it was coming out already.
- quantumOctopus 7y agoUgh I just bought the old one a week ago.
- bigred100 7y agoI’d agree... Outside of very rare circumstances (specialist in numerical linear algebra implementations), my opinion is that implementing matrix operations is something you do once (twice) in your numerical courses to get an intuition for the algorithm, and then never again. But maybe it’s educational to do once if you never have before.
- peterhj 7y agoI'm curious if you have a particular language/microcontroller/microwave/badger you have in mind? Depending on which, YMMV.
- fartcannon 7y agoNo, not off hand I don't. It's just something I've noticed in all these make a neural network posts. Feels a little like they're just drawing the rest of the owl, if you know what I mean. But thank you.
- gnulinux 7y agoMatrix math is easy peasy. Freshman level programming. Just lookup algorithms on Wikipedia and you're all set. The problem is it's extremely hard to make it efficient. Dozens of men-years are spent trying to optimize linear algebra libraries. There are handful linalg libraries that have competitive performance. It was my college project to make a fast linalg library, and boy it is fast. There are some things like matrix multiplication that if you implement in C with the trivial algorithm, takes >2 mins but with some tricks you can make it as fast as <second (vectorization, OpenMP, handwritten assembly, automatically optimized code, various optimizations, better algorithm.....). So, if you want to implement linalg in some language and compile it, go ahead, more power to you. But it's basically impossible to do it efficiently. My opinion is: this is fine and we should do this. There should be linalg libraries written in pure python (and are 1000x slower than lapack) but just understand that it's impossible to satisfy all use cases of numpy this way (at least currently).
- asdfman123 7y agoCan anyone simply explain the gist of how matrix multiplication is optimized? I know a lot of is farmed out to the GPU (if you've got a good GPU), but what's the essence of it? Caching? Some kind of clever mathematical tricks? All of the above?
- johndough 7y agoThe answer is: No one really knows because cuBLAS is closed source. But to get within the same order of magnitude, tiling the workload for better cache utilization is usually the most important step. This article [1] explains it quite well and also lists a few other tricks. In addition, there's also the fast Fourier transform for large filter kernels and Winograd convolutions [2] for small filter kernels. [1] https://cnugteren.github.io/tutorial/pages/page1.html https://cnugteren.github.io/tutorial/pages/page1.html [2] https://arxiv.org/pdf/1509.09308.pdf https://arxiv.org/pdf/1509.09308.pdf
- tntn 7y agoNot entirely true - Scott Gray knows: https://github.com/NervanaSystems/maxas/wiki/SGEMM https://github.com/NervanaSystems/maxas/wiki/SGEMM IIRC his kernels shipped in cuBLAS at some point.
- asdfman123 7y agoIt is easy to do unless you don't code at all, or are completely confused by the math. I'm a C# developer and I'm sure it would take me all of about 30 seconds to install a matrix multiplication package through nuget. I'm sure it would be immediately obvious how to add items to matrices or do a dot product.
- animal531 7y agoI'm a C# developer who wrote my own implementation (with help from random tutorials etc). It was dead easy to get code examples as needed.
- pilooch 7y agoExactly the reason why my colleagues and myself do all deep learning in C++, performance and portability, from cloud to RPie. We've even modified caffe2 so we could build the training graph from pure C++. We know this is not the current doxa :) It's also all open sourced just in case others might need it...
- fartcannon 7y agoLink? I love C++ and would love to see it.
- felipellrocha 7y agoYep. I would like to see an article that implements everything without using matrices first, then creates the matrices library with you, and refactors everything over. So much learning that we're missing by not going through this step.
- cr0sh 7y agoA good course that comes close to this would be the Coursera Machine Learning course (what used to be known as "ML Class" by Andrew Ng). It uses Octave - but you first do everything (in the section on NN) "by hand" - building and looping for the matrix operations. Only after you've gone that far, does he (Ng) introduce the fact that Octave has vector/matrix primitives... I took the original ML Class in the Fall of 2011; it was a great class, and opened my eyes a great deal on the topic of machine learning and neural networks, which I had struggled with understanding in the past (mainly on what and how backprop worked).
- fartcannon 7y agoTo me it feels a bit like that joke about drawing instructions. "1. Draw some circles. 2. Now draw the rest of the owl."
- cscheid 7y agoHm. I just finished teaching an ML course where all of the assignments were pure Python (on purpose, so students would actually have the chance to see all of the code). One of the assignments included implementing reverse-mode autodiff and a NN classifier on top. It can be done in ~600 lines of clear python, serious!
- fartcannon 7y agoWhich course?
- cscheid 7y agohttps://cscheid.net/courses/spr19/csc665/ https://cscheid.net/courses/spr19/csc665/ The assignments are not directly available, but my email is easy to find.
- danlugo92 7y agohttp://neuralnetworksanddeeplearning.com/ http://neuralnetworksanddeeplearning.com/