TensorFlow CPU
TensorFlow-cpu is the CPU-optimized variant of TensorFlow, an open-source machine learning framework developed by Google. It is designed for high-performance numerical computation, suitable for training and deploying machine learning models without GPU acceleration. The current version is 2.21.0, and it follows a frequent release cadence, typically aligning with the main TensorFlow project's minor and patch updates every few months.
Warnings
- breaking Support for Python 3.9 has been removed starting with TensorFlow 2.21.
- breaking Keras 3 became the default Keras version starting with TensorFlow 2.16. This may introduce breaking changes if your code relies on Keras 2 APIs.
- breaking The `tf.estimator` API has been removed.
- gotcha If a GPU is present on your system, TensorFlow might prioritize it even if you've installed `tensorflow-cpu`. This can lead to unexpected GPU utilization.
- gotcha Optimizing TensorFlow performance on CPU often requires tuning specific runtime options and environment variables.
Install
-
pip install tensorflow-cpu
Imports
- tensorflow
import tensorflow as tf
Quickstart
import tensorflow as tf
# Verify TensorFlow version
print(f"TensorFlow Version: {tf.__version__}")
# Verify CPU device is recognized
cpu_devices = tf.config.list_physical_devices('CPU')
print(f"CPU Devices: {cpu_devices}")
if cpu_devices:
print(f"TensorFlow is configured to use CPU: {cpu_devices[0].name}")
# Perform a simple operation to confirm functionality
a = tf.constant([[1.0, 2.0], [3.0, 4.0]])
b = tf.constant([[1.0, 1.0], [1.0, 1.0]])
print(f"Result of a + b:\n{a + b}")
else:
print("No CPU devices found by TensorFlow. Check installation.")