The most important thing to realize about TensorFlow is that, for the most part, the core is not written in Python: It's written in a combination of highly-optimized C++ and CUDA (Nvidia's language for programming GPUs). Much of that happens, in turn, by using Eigen (a high-performance C++ and CUDA numerical library) and NVidia's cuDNN (a very optimized DNN library for NVidia GPUs, for functions such as convolutions).

The model for TensorFlow is that the programmer uses "some language" (most likely Python!) to express the model. This model, written in the TensorFlow constructs such as:

h1 = tf.nn.relu(tf.matmul(l1, W1) + b1)
h2 = ...

is not actually executed when the Python is run. Instead, what's actually created is a dataflow graph that says to take particular inputs, apply particular operations, supply the results as the inputs to other operations, and so on. This model is executed by fast C++ code, and for the most part, the data going between operations is never copied back to the Python code.

Then the programmer "drives" the execution of this model by pulling on nodes -- for training, usually in Python, and for serving, sometimes in Python and sometimes in raw C++:

sess.run(eval_results)

This one Python (or C++ function call) uses either an in-process call to C++ or an RPC for the distributed version to call into the C++ TensorFlow server to tell it to execute, and then copies back the results.

So, with that said, let's re-phrase the question: Why did TensorFlow choose Python as the first well-supported language for expressing and controlling the training of models?

The answer to that is simple: Python is probably the most comfortable language for a large range of data scientists and machine learning experts that's also that easy to integrate and have control a C++ backend, while also being general, widely-used both inside and outside of Google, and open source. Given that with the basic model of TensorFlow, the performance of Python isn't that important, it was a natural fit. It's also a huge plus that NumPy makes it easy to do pre-processing in Python -- also with high performance -- before feeding it in to TensorFlow for the truly CPU-heavy things.

There's also a bunch of complexity in expressing the model that isn't used when executing it -- shape inference (e.g., if you do matmul(A, B), what is the shape of the resulting data?) and automatic gradient computation. It turns out to have been nice to be able to express those in Python, though I think in the long term they'll probably move to the C++ backend to make adding other languages easier.

(The hope, of course, is to support other languages in the future for creating and expressing models. It's already quite straightforward to run inference using several other languages -- C++ works now, someone from Facebook contributed Go bindings that we're reviewing now, etc.)

Answer from dga on Stack Overflow
🌐
Stack Overflow
stackoverflow.com › questions › 72015945 › why-do-i-get-worse-results-in-java-than-in-python-when-using-the-same-tensorflow
Why do I get worse results in Java than in Python when using the same Tensorflow models? - Stack Overflow
As a representative test I will now compare the performance of the MoveNet model when using Python and Java. The MoveNet model "Thunder" detects 17 keypoints of a body in an image with 256x256 pixels. I will use exactly the same image (the same file without touching and resizing it) in both setups (I uploaded it to my webspace; this step was done when updating this text, however there are no differences in the results). Python: The MoveNet Model comes with a nice online Python demo in a Jupyter notebook:
🌐
GitHub
github.com › tensorflow › tensorflow › issues › 27999
I understand that java should be faster than Python. But not like this. · Issue #27999 · tensorflow/tensorflow
April 20, 2019 - You can collect some of this information using our environment capture script You can also obtain the TensorFlow version with python -c "import tensorflow as tf; print(tf.GIT_VERSION, tf.VERSION)" Describe the current behavior · I use python3.5 predict the same one sample speed: series start 2019-04-20 13:06:18 series end 2019-04-20 13:06:18 total time 0.24s 0.00min 2019-04-20 13:06:18 · I use java1.8 predict the same one sample speed: start time 2019-04-20 12:57:43 924 series start create data 2019-04-20 12:57:43 925 series end 0.00s 0.00min series start create tensor 2019-04-20 12:57:43 927 series end 0.09s 0.00min series start use model 2019-04-20 12:57:44 015 series end 0.69s 0.01min series start take result 2019-04-20 12:57:44 710 series end 0.03s 0.00min end time 2019-04-20 12:57:44 738 total time 0.81s 0.01min ·
Author   whaozl
Discussions

Is the Java tensorflow code stable and useful?
Typically what you'd do is train models offline with python, then load and run inference using the java runtime. More on reddit.com
🌐 r/tensorflow
4
2
May 15, 2024
Why does TensorFlow run significantly faster in Python than in Java on the GPU? - Stack Overflow
I have trained a TensorFlow model and I have tested it on the GPU in Python and Scala (using the Java API). Running from Java consistently performs worse, clocking in at about half the speed, and I'm More on stackoverflow.com
🌐 stackoverflow.com
June 27, 2017
Tensorflow inference performance with python vs. c++ . vs java - Stack Overflow
I'm trying to run inference using tensorflow. Trying to decide among the API's to use: python, c++, or java. What's the factors that impact performance? Are there any benchmarks? Any knowledge or t... More on stackoverflow.com
🌐 stackoverflow.com
November 13, 2017
Getting wrong predictions on a model loaded in java vs python using the tensorflow “saved model” api
I'm trying to load a model in Java that was trained in python and saved using the saved model api (from tensorflow.python.saved_model). More on github.com
🌐 github.com
2
May 17, 2018
Top answer
1 of 4
297

The most important thing to realize about TensorFlow is that, for the most part, the core is not written in Python: It's written in a combination of highly-optimized C++ and CUDA (Nvidia's language for programming GPUs). Much of that happens, in turn, by using Eigen (a high-performance C++ and CUDA numerical library) and NVidia's cuDNN (a very optimized DNN library for NVidia GPUs, for functions such as convolutions).

The model for TensorFlow is that the programmer uses "some language" (most likely Python!) to express the model. This model, written in the TensorFlow constructs such as:

h1 = tf.nn.relu(tf.matmul(l1, W1) + b1)
h2 = ...

is not actually executed when the Python is run. Instead, what's actually created is a dataflow graph that says to take particular inputs, apply particular operations, supply the results as the inputs to other operations, and so on. This model is executed by fast C++ code, and for the most part, the data going between operations is never copied back to the Python code.

Then the programmer "drives" the execution of this model by pulling on nodes -- for training, usually in Python, and for serving, sometimes in Python and sometimes in raw C++:

sess.run(eval_results)

This one Python (or C++ function call) uses either an in-process call to C++ or an RPC for the distributed version to call into the C++ TensorFlow server to tell it to execute, and then copies back the results.

So, with that said, let's re-phrase the question: Why did TensorFlow choose Python as the first well-supported language for expressing and controlling the training of models?

The answer to that is simple: Python is probably the most comfortable language for a large range of data scientists and machine learning experts that's also that easy to integrate and have control a C++ backend, while also being general, widely-used both inside and outside of Google, and open source. Given that with the basic model of TensorFlow, the performance of Python isn't that important, it was a natural fit. It's also a huge plus that NumPy makes it easy to do pre-processing in Python -- also with high performance -- before feeding it in to TensorFlow for the truly CPU-heavy things.

There's also a bunch of complexity in expressing the model that isn't used when executing it -- shape inference (e.g., if you do matmul(A, B), what is the shape of the resulting data?) and automatic gradient computation. It turns out to have been nice to be able to express those in Python, though I think in the long term they'll probably move to the C++ backend to make adding other languages easier.

(The hope, of course, is to support other languages in the future for creating and expressing models. It's already quite straightforward to run inference using several other languages -- C++ works now, someone from Facebook contributed Go bindings that we're reviewing now, etc.)

2 of 4
47

TF is not written in python. It is written in C++ (and uses high-performant numerical libraries and CUDA code) and you can check this by looking at their github. So the core is written not in python but TF provide an interface to many other languages (python, C++, Java, Go)

If you come from a data analysis world, you can think about it like numpy (not written in python, but provides an interface to Python) or if you are a web-developer - think about it as a database (PostgreSQL, MySQL, which can be invoked from Java, Python, PHP)


Python frontend (the language in which people write models in TF) is the most popular due to many reasons. In my opinion the main reason is historical: majority of ML users already use it (another popular choice is R) so if you will not provide an interface to python, your library is most probably doomed to obscurity.


But being written in python does not mean that your model is executed in python. On the contrary, if you written your model in the right way Python is never executed during the evaluation of the TF graph (except of tf.py_func(), which exists for debugging and should be avoided in real model exactly because it is executed on Python's side).

This is different from for example numpy. For example if you do np.linalg.eig(np.matmul(A, np.transpose(A)) (which is eig(AA')), the operation will compute transpose in some fast language (C++ or fortran), return it to python, take it from python together with A, and compute a multiplication in some fast language and return it to python, then compute eigenvalues and return it to python. So nonetheless expensive operations like matmul and eig are calculated efficiently, you still lose time by moving the results to python back and force. TF does not do it, once you defined the graph your tensors flow not in python but in C++/CUDA/something else.

🌐
Quora
quora.com › Do-I-really-need-to-learn-Python-for-Tensorflow-What-are-the-disadvantages-of-using-Tensorflow-with-Java-and-C
Do I really need to learn Python for Tensorflow? What are the disadvantages of using Tensorflow with Java and C++? - Quora
Answer (1 of 2): Python is the easiest way to use machine learning now without prior programming knowledge. Be it TensorFlow, Keras or Scikit Learn they make your job much, much easier. The anaconda distribution gives you almost everything you need for ML. YOU DO NOT NEED PRIOR PROGRAMMING EXPER...
🌐
Reddit
reddit.com › r/tensorflow › is the java tensorflow code stable and useful?
r/tensorflow on Reddit: Is the Java tensorflow code stable and useful?
May 15, 2024 -

I am a Java guy and been barely getting into TensorFlow. I want to integrate in real time more closely with my Java applications. I dont see much discussion on this project. Is it full Java, no C++ level or Python integration? Is it fully support and works mostly like tensorflow python code?

https://github.com/tensorflow/java

🌐
Java Code Geeks
javacodegeeks.com › home › core java
Java for Machine Learning with TensorFlow or PyTorch - Java Code Geeks
July 11, 2024 - Python Advantage: TensorFlow and PyTorch are undeniably powerful and well-established frameworks with extensive documentation, tutorials, and a vast community. Their focus on Python integrates seamlessly with popular libraries like NumPy and Scikit-learn, creating a robust ML ecosystem. Java Alternatives: Don’t despair, Java developers!
🌐
Glassflow
glassflow.dev › blog › choosing-the-right-language-python-vs-java-for-ai-and-machine-learning
Choosing the right language: Python vs. Java for AI and machine learning
October 28, 2024 - Python, being an interpreted language, ... cases. Many AI and ML tools, such as TensorFlow Serving (for model deployment) or platforms for distributed training (like Ray), have better integration with Python than Java....
Find elsewhere
🌐
DIVISIO
divis.io › en › 2017 › 11 › enterprise-tensorflow-1
Divisio | Enterprise Tensorflow - Java vs. Python
November 30, 2017 - Except for Android, where we can regard Java as a first class citizen, platform constraints that apply to Python will generally also apply to Java (we need a separate runtime environment etc.). As we do focus on server side Java, we do not regard the Android case here. Also Google is heavily invested in this area (see Tensorflow Light), so there is plenty of discussion of this scenario elsewhere.
🌐
Quora
quora.com › Should-I-learn-Python-or-C++-for-using-TensorFlow-in-finance
Should I learn Python or C++ for using TensorFlow in finance? - Quora
Answer (1 of 4): Python. Although there is a C++ interface of TensorFlow in the works, Python provides support for more than just building models. Python is helpful for building Machine Learning Applications from its inception to deployment. Here are a few helpful libraries that can enhance your...
🌐
TensorFlow
tensorflow.org › install › lang_java_legacy
Install TensorFlow for Java
July 25, 2024 - TensorFlow provides a Java API— useful for loading models created with Python and running them within a Java application.
🌐
TensorFlow
tensorflow.org › api_docs
API Documentation | TensorFlow v2.16.1
TensorFlow has APIs available in several languages both for constructing and executing a TensorFlow graph. The Python API is at present the most complete and the easiest to use, but other language APIs may be easier to integrate into projects and may offer some performance advantages in graph ...
🌐
Career Karma
careerkarma.com › blog › java › machine learning: python vs. java
Machine Learning: Python vs. Java | Career Karma
July 20, 2022 - While working on my Capstone, I utilized both Sklearn and TensorFlow to build much bigger and more powerful neural networks to generate text. I can say first hand that Python significantly reduces the pain associated with doing machine learning. Learning Python as a start to becoming a machine learning engineer is a great choice. Ultimately, machine learning comes down to writing lots of code. Java ...
🌐
TensorFlow
tensorflow.org › jvm
TensorFlow for Java | JVM
August 31, 2021 - In the early days, the Java language bindings for TensorFlow were hosted in the main TensorFlow repository and released only when a new version of the core library was ready to be distributed, which happens only a few times a year. Now, all Java-related code has been moved to this repository so that it can evolve and be released independently from official TensorFlow releases.
🌐
Stack Overflow
stackoverflow.com › questions › 44767262 › why-does-tensorflow-run-significantly-faster-in-python-than-in-java-on-the-gpu
Why does TensorFlow run significantly faster in Python than in Java on the GPU? - Stack Overflow
June 27, 2017 - I have trained a TensorFlow model and I have tested it on the GPU in Python and Scala (using the Java API). Running from Java consistently performs worse, clocking in at about half the speed, and I'm
🌐
JavaKing
javaking.com › home › latest › java vs python: which programming language should you learn first?
Java vs Python: Which Programming Language Should You Learn First?
January 3, 2026 - If you want to work with large-scale data infrastructure, Java fluency helps. Data Science: Python dominates this field. Pandas, NumPy, and Matplotlib are standard tools. If you want to analyze data professionally, Python is the default. Machine Learning: TensorFlow, PyTorch, and scikit-learn ...
🌐
TensorFlow
tensorflow.org › jvm › install tensorflow java
Install TensorFlow Java | JVM
August 31, 2021 - TensorFlow Java can run on any JVM for building, training and deploying machine learning models. It supports both CPU and GPU execution, in graph or eager mode, and presents a rich API for using TensorFlow in a JVM environment.
🌐
Stack Overflow
stackoverflow.com › questions › 47260910 › tensorflow-inference-performance-with-python-vs-c-vs-java
Tensorflow inference performance with python vs. c++ . vs java - Stack Overflow
November 13, 2017 - I'm trying to run inference using tensorflow. Trying to decide among the API's to use: python, c++, or java. What's the factors that impact performance? Are there any benchmarks? Any knowledge or t...
🌐
GitHub
github.com › tensorflow › java
GitHub - tensorflow/java: Java bindings for TensorFlow
TensorFlow can run on any JVM for building, training and running machine learning models. It comes with a series of utilities and frameworks that help achieve most of the tasks common to data scientists and developers working in this domain. Java and other JVM languages, such as Scala or Kotlin, are frequently used in small-to-large enterprises all over the world, which makes TensorFlow a strategic choice for adopting machine learning at a large scale.
Starred by 916 users
Forked by 223 users
Languages   Java 99.8% | Java 99.8%
🌐
Pythonindia
pythonindia.in › home › java vs. python: which language holds the future ahead in 2026?
How are Java vs. Python languages unique?
December 10, 2025 - But Python’s dominance in web automation, data analytics, and AI has contributed to its rise. In Python vs. Java, both are worldwide favorites, with Java outperforming Python for enterprise growth. ... The power of AL and ML relies on TensorFlow, PyTorch, and Scikit-learn.
🌐
GitHub
github.com › tensorflow › tensorflow › issues › 19359
Getting wrong predictions on a model loaded in java vs python using the tensorflow “saved model” api · Issue #19359 · tensorflow/tensorflow
May 17, 2018 - At first I thought it was because the weights/bias weren't being loaded but I'm able to "run" the weights/bias operation in the java version and see that it has the correct weights that I see in the python script after training.
Published   May 17, 2018
Author   JsFlo