Run TensorFlow Machine Learning Code In C# With Almost No Changes

Mark Farragher
6 min readJun 27, 2019

Machine Learning is a super-exciting field with breakthroughs happening all the time. Researchers are constantly pushing computers to their limits, teaching them how to see, hear, read, write, and listen. Skills that used to be the exclusive domain of us humans.

The language of choice for machine learning is Python, and the most popular library is Google’s TensorFlow. Almost all code examples are written in Python and depend on TensorFlow and NumPy libraries.

This can be a problem for us C# and NET developers. We are confronted with a stark choice — either learn Python, or use NET machine learning libraries and painstakingly write our own C# code from scratch.

And in fact, this is the strategy I used in my Machine Learning Course. I studied lots of Python code that used Keras and TensorFlow, and then I meticulously wrote my own C# code from scratch that uses native CNTK and ML.NET libraries instead.

This approach works, but it has some disadvantages:

  • My students are learning to write code in a very specific style that only applies to NET. These coding skills do not easily transfer to Python.
  • CNTK and ML.NET don’t support certain TensorFlow features, so I have to use Python code to demonstrate them to C# developers.

There have been efforts to write machine learning libraries in NET that mimic the API and coding style of Python, like NativeKeras and KerasSharp, but these projects are no longer under active development.

I’ve been looking for a comprehensive NET library that exactly mimics how to write machine learning code in Python, and there’s a new kid in town that just might deliver: SciSharp.

SciSharp is building a NET open source ecosystem for data science, machine learning and AI. Their philosophy is that C# machine learning code should look, feel, and behave as much as possible like the corresponding Python code.

Check this out — this is a simple linear regression demo in TensorFlow. The Python code is on the left, and the corresponding C# code is on the right: