Live data from Hacker News

Ask HN: How did Python become the lingua franca of ML/AI?

news.ycombinator.com

1–10 of 99 posts

Ask HN: How did Python become the lingua franca of ML/AI?

#1
I've looked around a bit and can't really find a satisfying answer to this question. There are posts on answer sites, but these often boil down to "dynamic languages are good at glue", or "Tensorflow / Jupyter".

That can't be the whole story, can it? Or if it is, why did these projects choose Python over other scripting lanuages?

I bet there's some interesting history here.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#2
Python has always been used as a nice layer over various C libraries so when ML started taking off and people started using GPUs to accelerate training and inference it was a natural choice for acting as the high level code for interfacing with the low level GPU code.

There were some other DSLs that were being developed at the time but the ones that stuck were the Python ones. [1]

1: https://terralang.org/

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#3
The same reason that Python is heavily used in scientific computing.

ML/AI/Scientists aren't systems people. They don't want to care about memory management/parallelization/etc. - they want to write perfect little mathematical poems which get executed on a perfect Turing machine.

Python is good at that. Thanks to the efforts of actual systems people, its libraries (numpy, scipy, etc.) run quick enough to be practical on a lot of workloads.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#4
My narrow perspective is that Python is the one language that both companies engaged in ML research (like Google) have been using, and also a very common language of instruction, for instance in the CS program at the school where I studied. If you were a CS student interested in ML/AI, starting school ten to fifteen years ago, but not particularly interested in software engineering, you'd be able to get by with only really knowing Python. Depending on how widespread this is, I'm guessing it's a part of the picture.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#5
I would put it mostly down to Spark.

Originally, it was only available in Scala/Java but then they added Python support courtesy of Py4J. And since Python was massively simpler than Scala it exploded in popularity very quickly becoming the default language.

So then you had Data Scientists who were already writing a lot of data transformations in Spark looking around at the rest of the Python ecosystem finding libraries like pandas, IDEs like Jupyter and basically staying there since it was so much easier than alternatives.

Their interests aren't really in computer science and so they look for whatever language can get them to an outcome as quickly and easily as possible. Even if it's not the most optimal, elegant or maintainable.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#6
Because Python has NumPy, which implements vectorized math on arrays and matrices. Machine learning algorithms are implemented naturally and efficiently with those primitives. PyTorch, TensorFlow, and I think every other machine learning framework in Python all use NumPy.

JavaScript, Ruby, and Perl either don't have this abstraction at all, or they have much weaker versions of it, and many fewer scientific libraries.

NumPy started in the early 2000's and continues to this day. It takes decades to build up this infrastructure! This recent interview with NumPy creator Travis Oliphant is great:

https://www.youtube.com/watch?v=gFEE3w7F0ww

He talks about how there were competing abstractions like "Numeric" and another library, and his goal with NumPy was to unify them. And how there are still some open design issues / regrets.

There were multiple people in the nascent Python community who were tired of MATLAB, not just because it's proprietary, but because it's a weak and inefficient language for anything other than its scientific use cases. You won't have a good time trying to write a web app wrapper in MATLAB, for example.

The much more recent Julia language is also inspired positively and negatively by MATLAB, and is very suitable for machine learning, though it doesn't have the decades of libraries that Python has.

-----

The NumPy extension was in turn enabled by operator overloading in Python (which is actually a very C++ influenced mechanism). JavaScript doesn't have operator overloading; I'm pretty sure Perl doesn't, but not sure about Ruby. Lua and Tcl do not have it. (Lua does have a machine learning framework though -- http://torch.ch/ -- but I think PyTorch is more popular now.)

So if Guido didn't design Python with operator overloading, then NumPy would not have grown out of it.

Also relevant is Guy Steele's famous talk Growing a Language (late 90's or early 2000's I think). He advocates for operator overloading in Java so end users can evolve language with their domain expertise! Well Java never got it, and Python ended up having the capabilities to grow linear algebra.

Guido has even said he doesn't really use or even "get" NumPy! So it turns out that an extensible design does have the benefits that Steele suggested (although it's a very difficult language design problem.) There have been several enhancements to Python driven by the NumPy community, like slicing syntax and semantics and the @ matrix multiplication operator. And I think many parts of the C API like buffers.

-----

Another interesting thing from Oliphant's interview is that he really liked that Python has complex numbers. (I don't think any of JavaScript, Ruby, Perl, or Lua have them in the core, which is important.) That piqued his interest and kicked off a few decades of hacking on Python.

He was an electrical engineering Ph.D. student and professor, and complex numbers are ubiquitous in that domain. Example:

    $ python3 -c 'print(3j * 2 + 1)'
    (1+6j)

This is another simple type built on Python's extensible core, and it's short.

    $ Python-3.9.4$ wc -l Objects/complexobject.c 
    1125 Objects/complexobject.c

I recommend writing a Python extension in C if you want to see how it works. See Modules/xx*.c in the Python source code for some templates / examples. IMO the Python source code is a lot more approachable than Perl, Ruby, or any JS engine I've looked at.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#8
My last job was at an ML company.

Most ML people there cannot build large robust systems and some struggled with the non-algorithmic bits of software. I am sure that some can out there in the world, but for the most part our ML people were very good at creating models and not very good at the development part, especially as the program grew (part of the motivation to hire devs like me in the first place).

Python gets rid of as much of the developmental complexity as possible. No types, no memory management, libraries for everything, No need to create a class to run "hello world." Pip makes it trivial to import things. Use PyCharm and you just need to click the run button, with no complicated JRE and JDK setup.

It is the fastest way to start writing models.

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#9

My last job was at an ML company. Most ML people there cannot build large robust systems and some struggled with the non-algorithmic bits of software. I am sure that some can out there in the world, but for the most part our ML people were very good at creating models and not very good at the development part, especially as the program grew (part of the motivation to hire devs like me in the first place). Python gets…

I don't think this is a fair assessment of "most ML people"

Some of the biggest distributed systems built today are used for statistical inference or scientific computation

Most "ML people" I know are highly versatile in software, networks and deep hardware knowledge, i.e., essentially they have a very good understanding of what a computer is and what is capable from

Its very naive to think that you can assemble machine learning systems without having a solid understanding of computers and statistics

You know who also likes python a lot? Hackers. I wonder why

Re: Ask HN: How did Python become the lingua franca of ML/AI?

#10
post #9

My last job was at an ML company. Most ML people there cannot build large robust systems and some struggled with the non-algorithmic bits of software. I am sure that some can out there in the world, but for the most part our ML people were very good at creating models and not very good at the development part, especially as the program grew (part of the motivation to hire devs like me in the first place). Python gets…

I don't think this is a fair assessment of "most ML people" Some of the biggest distributed systems built today are used for statistical inference or scientific computation Most "ML people" I know are highly versatile in software, networks and deep hardware knowledge, i.e., essentially they have a very good understanding of what a computer is and what is capable from Its very naive to think that you can assemble mach…

Python is used to make fast and dirty experiences. You haven't figured out the answer yet, so no point on building dedicated optimized code which might be useless in end

Almost always, ML production models end up being a binary files of matricial weights. This file can be loaded in wtv language or device you decide to use

Post reply on HN