How to run Tensorflow on CPU
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
If you want TensorFlow to run on CPU 1, you are really asking about CPU affinity at the operating-system level. TensorFlow can be told to avoid GPUs, and it can be tuned for thread counts, but choosing a specific CPU core is normally done by the OS scheduler or by process-affinity tools such as taskset on Linux. The clean solution is to pin the process first and then let TensorFlow run normally inside that CPU set.
Disable GPU Use First if Necessary
On machines that also have GPUs, hide them before TensorFlow starts so the workload stays on the CPU.
Or inside Python before creating TensorFlow devices:
That handles CPU versus GPU selection. It does not choose which CPU core will run the process.
Pin the Process to CPU 1 on Linux
On Linux, the usual way to target a specific core is taskset.
This tells the kernel to run the Python process only on CPU core 1. TensorFlow then uses whatever CPU threads are available inside that affinity mask. If the mask contains only core 1, TensorFlow effectively runs there.
You can verify the affinity from the shell:
Or inside Python:
If the output is [1], the process is pinned to CPU 1.
Keep TensorFlow Thread Counts Consistent
If the process is pinned to a single core but TensorFlow is still configured with many threads, the runtime may spend effort scheduling threads that all compete for the same CPU. For a strict single-core run, reduce the thread counts.
This does not pin the process by itself, but it matches TensorFlow’s thread model to the CPU-affinity constraint.
A Small CPU-1 Example
This example does three things clearly:
- hides GPUs
- limits TensorFlow thread parallelism
- confirms the process affinity includes only CPU
1
What TensorFlow Itself Can and Cannot Do
TensorFlow can:
- run on CPU instead of GPU
- control thread parallelism
- log device placement
TensorFlow does not normally expose a high-level API that says “bind all compute to OS core 1.” That decision belongs to the operating system scheduler and affinity controls.
So if the real requirement is core-specific execution for benchmarking or isolation, use OS tooling first.
Common Pitfalls
- Hiding GPUs and thinking that also pins the process to a particular CPU core.
- Trying to choose CPU
1only through TensorFlow thread settings without setting process affinity. - Pinning the process to one core but leaving TensorFlow configured with many competing threads.
- Forgetting that CPU numbering starts at
0on most systems, so CPU1means the second logical core. - Expecting portable behavior from
taskseton non-Linux operating systems.
Summary
- Running TensorFlow on CPU
1is mainly a CPU-affinity problem, not a TensorFlow device-selection problem. - Hide GPUs if necessary so TensorFlow stays on the CPU.
- Use
taskset -c 1on Linux to pin the process to CPU1. - Match TensorFlow thread settings to the restricted affinity when you want predictable single-core behavior.
- Let the OS choose the core unless you have a concrete reason to pin the process manually.

