TensorFlow
tf.reshape
Fortran order
numpy
reshaping arrays

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.

Practice ML system design

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:

python
1import numpy as np
2
3a = np.array([[1, 2], [3, 4]])
4print(np.reshape(a, (4,), order="C"))
5print(np.reshape(a, (4,), order="F"))

TensorFlow reshape behaves like the C-order version.

What tf.reshape Does Normally

python
1import tensorflow as tf
2
3a = tf.constant([[1, 2], [3, 4]])
4print(tf.reshape(a, [4]).numpy())

Output:

python
[1 2 3 4]

That is row-major order.

Emulating Fortran-Style Flattening

For a 2D tensor, a common trick is to transpose first.

python
1import tensorflow as tf
2
3a = tf.constant([[1, 2], [3, 4]])
4fortran_like = tf.reshape(tf.transpose(a), [4])
5print(fortran_like.numpy())

Output:

python
[1 3 2 4]

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

python
1import numpy as np
2import tensorflow as tf
3
4arr = np.arange(1, 7).reshape((2, 3))
5np_fortran = np.reshape(arr, (3, 2), order="F")
6
7x = tf.constant(arr)
8# One possible emulation for this specific case:
9tf_fortran_like = tf.transpose(tf.reshape(tf.transpose(x), (2, 3)))
10
11print(np_fortran)
12print(tf_fortran_like.numpy())

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":

  1. test a tiny example in NumPy
  2. write a TensorFlow equivalent with transpose plus reshape
  3. compare outputs directly
  4. 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.reshape to 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.reshape uses 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.transpose can solve more directly.

Related reading
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.