Porting Python's AI to Rust's AI


Say I have created an AI with Python. If Rust’s AI library has matured, then would it be possible to some how possible to convert Python’s AI code (that I have written) into Rust’s AI code?


is it Rust’s built in AI library going to have similar function/command names as compared to Python’s built in AI code?


The word “AI” is doing too much work here.

There are multiple ways to code an AI. A popular approach these days is via machine learning, especially deep learning. I think that is what you’re talking about, and, it is what I’m going to talk about next.

Neither Python-the-language nor Rust-the-language have any language-specific functionality for machine learning. Rather, both of them have libraries - not necessarily developed by the same teams who develop the languages - which provide machine learning functionality.

Now, suppose you’ve created an AI in Python that uses machine learning. What you’ve really got, then, is a model - a set of weights (numbers) and some related functions, that, when you put in some data at the top (the “example”), will run a computation on that example and return a result (the “label”). This process of taking an example and returning a label is called inference.

Note that your example and label depend on what your AI does. E.g., if your ML-based AI does voice to text, then your example is an audio stream of someone talking, and your label is a string of text characters. If your AI plays a game, then your example is a game position (e.g., where all the pieces are on a chess board), and your label is a list of which moves are good, usually represented a list of probabilities.

Models can usually be saved as a file and then opened later to do more work. E.g., two popular formats are TensorFlow and ONNX

So if you create a model in Python, there are multiple ways that model can interoperate with Rust code. One way is that you create the model in Python, and train it there, then save that model to a file and open it in Rust when you want use it (do “inference”). This is already possible if your model is in TensorFlow or ONNX format.

There is a website, http://www.arewelearningyet.com/, which tracks the state of ML in Rust, and gives a broader overview. My belief is that doing inference in Rust is further along than doing training in Rust. (inference is Rust already works well, and honestly it’s the more important half, because training is a well-defined problem that is solved pretty well by existing Python libraries).


Everyone is telling me use Python for AI Rust’s machine learning is immature at the moment though?


Even though I’m a huge Rust enthusiast, my honest recommendation is that if you’re interested in getting started with machine learning, use Python.

Rust has a place for machine learning, but right now Python is the best place to get started.


If I learn machine learning using Python, would it be easier use Rust to build machine learning? As I don’t want to re learn everything about machine learning if I were to use another language.

Would the commands for using Python to use machine learning be similar to Rust’s machine learning?


Yes, if you learn machine learning using Python, it would be easier when you use Rust for machine learning. Machine learning skill is largely language independent.

No, Python API for machine learning and Rust API for machine learning likely wouldn’t be identical and you’d need to adapt.


I see.

Why am I being recommended to learn Python over Rust for AI?


You are being recommended Python by everybody because currently Python is the best language to learn machine learning with.


Learning ML is its own thing. It is a discipline that deserves your full attention when you start to learn it. Anything that distracts from the core subject is to be avoided. Yet choosing to learn ML with Rust is going to add distractions:

  1. The Python libraries for ML are far more advanced and better documented. In addition, many of them are oriented towards non-professional programmers (data scientists, other engineers, and curious learners and hobbyists). In contrast, the libraries you find for Rust are likely to be oriented towards the needs of professionals trying to put their models into production.

  2. Python is the lingua franca of the ML world. If you want to read a blog post or article about some ML topic, the chances are that the example code is written in Python.

For what it’s worth, I myself chose the path of learning ML with Python, despite the fact that I know Rust a lot better than I know Python.

1 Like

Do you have any ideas when it will become more mature and better documented in approximation?

The reason why I want to know is that if I wanted to learn machine learning and build a system of my own choice, if Rust’s machine learning libraries become more advanced, I don’t want to have to rewrite my code if I want to stick with Rust.


In this imaginary future, Python’s machine learning ecosystem will be at least as good as it is now, so whatever you built will continue to work fine, and you don’t need to rewrite it. The most important thing is that you actually start building something, instead of spending all your time trying to make the perfect choices upfront. You will learn a lot more by actually building something.

My recommendation is to start working through the documentation of scikit-learn. It introduces basic machine learning concepts along the way, and is one of the fastest ways of getting up to speed with machine learning.

1 Like

I would also strongly recommend using python to start with machine learning as most tutorials will be in python and the ecosystem for ML is very developed.
But (shameless plug) I also wanted to point some PyTorch bindings in Rust that I’ve been working, tch-rs. They could be used both for training and inference and on cpu or gpu. It certainly helps if you’re already familiar with PyTorch python api as these bindings are very similar, e.g. you can compare the python version of alexnet with the rust equivalent.


Is PyTorch fully ready for production use?


Yes, PyTorch is fully ready for production use.

On the other hand, this is Rust forum and production readiness of PyTorch is definitely off-topic, so please refrain from continuing!

closed #16