TensorFlow tf.reshape Fortran order like numpy
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
tf.reshape uses row-major, C-style element order, just like most TensorFlow tensor operations. It does not have a direct order='F' option like NumPy reshape. If you need Fortran-like behavior, the usual workaround is to transpose, reshape, and transpose back in a way that reproduces column-major traversal.
What C Order Versus Fortran Order Means
When reshaping, the question is not just the new shape. It is also the order in which elements are read from the original layout.
- C order reads rows first
- Fortran order reads columns first
NumPy exposes this directly:
TensorFlow reshape behaves like the C-order version.
What tf.reshape Does Normally
Output:
That is row-major order.
Emulating Fortran-Style Flattening
For a 2D tensor, a common trick is to transpose first.
Output:
That matches NumPy's column-major flattening for this case.
General Idea for Reshaping
If you want a reshape that mimics NumPy Fortran order, think in terms of rearranging axes before and after reshape. For simple 2D cases, transpose is often enough. For higher-dimensional tensors, the transformation can be more complex because you are emulating a different element traversal rule, not just swapping shape numbers.
A helpful validation habit is to compare against NumPy on the same input.
Compare TensorFlow and NumPy Explicitly
The exact transpose pattern depends on the source and target shapes, which is why there is no universal one-line tf.reshape(..., order="F") equivalent built into TensorFlow.
When You Can Simplify the Problem
Sometimes you do not actually need true Fortran-order reshaping. You only need the downstream operation to see data in a certain axis arrangement. In that case, a direct tf.transpose may be the real answer, not a Fortran-style reshape emulation.
That is an important distinction:
- reshape changes how the same linear data is viewed
- transpose changes axis order
People often ask for Fortran-style reshape when what they actually need is axis permutation.
A Practical Rule
If you are porting NumPy code that uses order="F":
- test a tiny example in NumPy
- write a TensorFlow equivalent with transpose plus reshape
- compare outputs directly
- only then generalize to your full pipeline
Trying to reason about Fortran-order reshaping only in your head is error-prone.
Common Pitfalls
- Expecting
tf.reshapeto have a direct Fortran-order flag. - Confusing transpose with reshape.
- Porting NumPy code blindly without checking whether
order="F"was actually important. - Validating only shape and forgetting to validate element order.
- Assuming one transpose pattern works for every dimensionality and target shape.
Summary
- '
tf.reshapeuses standard row-major element order.' - TensorFlow does not provide a direct
order="F"reshape option. - For simple cases, transpose plus reshape can emulate Fortran-like behavior.
- Compare against NumPy on small examples when porting code.
- Many apparent Fortran-order problems are really axis-order problems that
tf.transposecan solve more directly.
Related reading
- TensorFlow tf.summary.text and linebreaks
- Tensorflow The channel dimension of the inputs should be defined
- TensorFlow Training
- TensorFlow training on my own image
- Tensorflow understanding tf.train.shuffle_batch
- TensorFlow Understanding the collections argument in tf.summary.scalar
- Tensorflow What are the output_node_names for freeze_graph.py in the model_with_buckets model?
- Tensorflow while loop dealing with lists

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.