So I’ve been reading about machine learning these past few days. It’s one of those things that as a curious developer, I really want to learn how to do myself. Imagine the idea of simplifying the most complicated of ideas that would take a lot of time and effort to apply in a typical programming setup. Machine learning can achieve that with a few lines of code.
Every time I read an article about it, or watch a Youtube video of people utilizing it, I can’t help but feel the urge to learn it myself and make some cool stuff with it. It has been pretty far from a typical person’s reach until recently when Google introduced Tensorflow, a free and open-source library for machine learning and data analysis.
Tensorflow is one of the best things that happened to the world of computing. It made machine learning really simple for anyone who has the patience to learn how to code. Google basically handed down their brilliant tool to the masses.
There’s only one problem in my opinion. To be able to use it, you have to learn how to code in Python. Don’t get me wrong. Whatever you can do with any other language, you can do with Python. But for certain people like me who have been programming in a different language for years, learning Python just for a single purpose seems to be an inefficient use of time and resources. This might be an OK Boomer comment from a software engineer that has been in the industry for more than a decade. But if there’s one thing that I’ve learned about software development, having a specialization is always a plus, and learning another programming language is more of lateral growth: it’s good, but does not bring that much benefit career-wise.
So I’m a .NET developer that wants to do machine learning. But Tensorflow is only for Python and some other languages that are not under the .NET umbrella. Well, that was the case before Tensorflow.NET was introduced.
Tensorflow.NET is a Tensorflow binding library implemented in .NET standard. The project aims to deliver the entire Tensorflow API to the .NET ecosystem. Being a .NET standard library, this can be used on different platforms through .NET Core. What makes this project even more awesome is that they found a way to keep the .NET version as close as possible to the actual Python code. This makes using the .NET library just as easy as using the actual API in Python.
Perhaps you might ask. We already have a machine learning library in .NET which is ML.NET so what’s the point in learning Tensorflow? That is true, we already have ML.NET. The only concern there is it’s still a relatively new machine learning framework. They have made a lot of progress since the release. But they still have a lot of work to do. In fact the latest version of ML.NET (1.4 at the time of writing) uses Tensorflow to achieve Deep Neural Network (DNN) transfer learning with GPU support through Tensorflow.NET bindings. So in my opinion, it’s still a worthy endeavor to learn Tensorflow through Tensorflow.NET whether you want to stick to ML.NET or not.
So what should we do to be able to use Tensorflow.NET? First, we have to establish what Tensorflow.NET is not.
- Tensorflow.NET is not a wrapper for the Python version. Therefore, you don’t need to install the Python version or any other version to make it work.
- Tensorflow.NET is not a .NET version of the original Tensorflow library. It’s just a binding library, meaning, it requires another library to work.
- Since Tensorflow.NET is dependent on the Tensorflow API, a certain Tensorflow version shall have a corresponding Tensorflow.NET version. So using the latest version of Tensorflow.NET (0.20 at the time of writing) might work for Tensorflow 1.15 and 2.2 but it won’t work for any version below 1.15. The readme document shall be able to direct you to the right binding library version depending on the Tensorflow version you’re intending to use.
Now that we know what Tensorflow.NET is not, let’s discuss what Tensorflow.NET is.
- Tensorflow.NET is a binding library to a SciSharp redistributable based on Microsoft.ML.TensorFlow.Redist. Simply put, this is a Tensorflow library implementation that makes .NET bindings possible.
- To be able to use Tensorflow.NET, you have to add a reference to it in NuGet. In addition, you have to choose the right distributable for the version of Tensorflow.NET that you have. Depending on your needs, you may choose the CPU or GPU version.
- In case you want to use Tensorflow.NET with a GPU, you need an Nvidia graphics card with CUDA support. The version of CUDA that you can use would depend on the graphics card that you have. Please refer to the Nvidia CUDA documentation to learn more.
Now that we know all these things, let’s get started.
With the Visual Studio open, create a Console project.
Once we have created the project, we need to open the NuGet Package Manager to search for the libraries that we need.
Once you are in the NuGet Package Manager, click on the Browse tab and look for Tensorflow.NET and SciSharp.Tensorflow.Redist. We have to install them both to the project.
Once they are installed, we have to add using static Tensorflow.Binding;
to declare that we’re using Tensorflow.NET.
After this, we’re all good to go. To try it, I’ve copied a sample code in DataCamp. They have a good Tensorflow tutorial for beginners there that I highly recommend.
static void Main(string[] args) { // Initialize two constants var x1 = tf.constant(new int[] { 1, 2, 3, 4 }); var x2 = tf.constant(new int[] { 5, 6, 7, 8 }); // Multiply var result = tf.multiply(x1, x2); // Print the result print(result); }
Run the project to see the result.
To check if the result is the same, I ran the code in the online Python shell that is embedded in the tutorial.
In case you want to use the GPU version, it’s not that complicated to do so as long as you meet all the basic requirements mentioned above. All you have to do then is to choose the GPU version of the redistributable. Currently, Windows and Linux are the only platforms supported.
Make sure that you have the Nvidia CUDA development library installed in your system. Depending on the Tensorflow GPU version, you will need a certain version of CUDA library. For example, for Tensorflow GPU version 1.14.1, you will need CUDA 10.0. You cannot use an older or newer version. Don’t forget to install the corresponding cuDNN library for your version of CUDA.
In conclusion, I would like to share a few thoughts. Gone are the days when our chosen languages restrict us from doing certain jobs. Gone are the days when we have to learn a new language just for a single purpose. We are in the age of computing where we can do whatever we want with the tools that we have, thanks to the hardworking people developing libraries that make this possible.
What a time to be a developer… what a time to be a developer indeed.
Bien is a software engineer for more than 10 years, focusing on Microsoft .NET technology. He developed solutions ranging from embedded systems to accounting systems. He spends his free time trying to understand the world and its people.