You know how most programmers find functional coding to be ever so slightly mind bending and how it's somewhat difficult to wrap one's head around working with variables whose state you cannot change and lazy evaluations and all manner of odd things?
The thing I've had most trouble with and still do actually, is coding in a functionally clean manner, using more recursion, cleaner abstractions and so on. Just as I thought I was almost starting to get kind of good at this, a bunch of people proved me wrong when I crowdsourced some elegance.
Yeah, some people are really good at this functional stuff.
And then one day ml-class introduced me to mathematical programming with Octave. Sure, I've done some Octave before at school, but that was just enough to get my feet wet - basic syntax and stuff. Or maybe I just paying enough attention to really grasp the awesome things I was being shown.
Either way, I feel as if over the past two weeks, doing machine learning homework in Octave has opened a whole new world of striving for elegance and purity in my code. If I thought functional was mindbending, this stuff is ripping my face off.
Apparently when you take a naive loop and make it into something beautiful it's called vectorization in this field. The interesting bit here is that all you really need to perform optimization of epic proportions is some math fu, no translating the problem into something else, no looking at it from five different perspectives ... just maths and then some.
Although I think this might have been exactly the perfect example for the code below ... it's difficult to search through videos for this stuff.
The naive approach could be something like this (didn't actually run the code):
J = 0;for i = 1:mJ += =y(i)*log(sigmoid(theta*X(i,:))-(1-y(i)*log(1-sigmoid(theta*X(i,:));endJ = J/m;for j = 1:size(theta)grad(j) = 0;for i = 1:mgrad(j) += (sigmoid(theta*X(i,:))-y(i))*X(i,j);endgrad(j) = grad(j)/m;end
But all those lops aren't really necessary, all they are basically doing is matrix multiplication, which gives us a nice way to vectorize the whole thing:
J = (1/m)*sum(-y'*log(sigmoid(X*theta))-(1-y')*log(1-sigmoid(X*theta)));grad = (1/m)*(sigmoid(X*theta)-y)'*X;
The difference in elegance absolutely blows my mind and I can't wait to see what other wonders I discover through this Octave thing in the course of this semester.
Pretty much all my octave can be found in the ml-class-homework repository. But I'm sure I'll end up modeling more algorithms in this thing.
- 12 steps to running gradient descent in Octave (flowingmotion.jojordan.org)
- Machine Learning Ex4 - Logistic Regression (r-bloggers.com)
- Machine Learning Ex 5.2 - Regularized Logistic Regression (r-bloggers.com)
Here's how it works 👇
Then get thoughtful letters 💌 on mindsets, tactics, and technical skills for your career.
"Man, love your simple writing! Yours is the only email I open from marketers and only blog that I give a fuck to read & scroll till the end. And wow always take away lessons with me. Inspiring! And very relatable. 👌"
Ready to Stop copy pasting D3 examples and create data visualizations of your own? Learn how to build scalable dataviz components your whole team can understand with React for Data Visualization
Curious about Serverless and the modern backend? Check out Serverless Handbook, modern backend for the frontend engineer.
Ready to learn how it all fits together and build a modern webapp from scratch? Learn how to launch a webapp and make your first 💰 on the side with ServerlessReact.Dev
By the way, just in case no one has told you it yet today: I love and appreciate you for who you are ❤️