I’ve recently been concerned with the problem of understanding the Python language better. I frequently encounter code that I struggle to parse/interpret. Take the following snippet for example:
final_state_locations =
next_states.flatten(start_dim=1).max(dim=1)[0].eq(0).type(torch.bool)
I’m sure an experienced python programmer won’t see a problem, but I’m basically an amateur. I picked up basic python, then it’s use in a data science context, so mainly pandas, plus a few statistical functions from scikit-learn or similar package. Now I’m learning how to use Python for machine learning and, well, multi-dimensional tensors in PyTorch are a bit harder to work with than a flat dataframe in Pandas.
So, my quest for greater understanding has lead me down an alternative path. Functional Programming. FP is not about understanding code better. It’s about writing code so that it’s more understandable. Less focus on implementation details and more on general intention. There are a few main principles of this programming paradigm, and one of them is immutable state, and functions without side effects. So I’m wondering how this could possibly apply to training an ML model, which is all about changing node weights to minimize some error function.
I guess there is a FP solution to this problem. Instead of changing weighs one creates a new set of weights from the old weights, and then loads the new weights into the model! Replace the old version with a new version instead of changing the old version. I guess that’s how immutability works, which I first came across when learning about the immutability of strings in Java. I’m not really seeing how this is going to make my code easier to understand. I guess I’ve got a way to go in my understanding of functional programming, or perhaps it’s just not a good fit for machine learning. Anyway, it’s interesting I guess.
On reflection, that example I gave at the top of the post is probably an example of functional programming. A new object is created by running some existing data through a string of functions. Not sure where that leaves me.