How to create a Rotation Matrix in Tensorflow
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
In TensorFlow, a rotation matrix is usually built from tf.cos and tf.sin so the result stays differentiable and can be used inside training pipelines. The main concerns are getting the matrix formula right, handling batch dimensions correctly, and applying the transform without shape errors.
The exact matrix depends on whether you are rotating in 2D or 3D. For most machine learning and computer vision tasks, a 2D rotation matrix is the simplest place to start.
A 2D Rotation Matrix
A counterclockwise rotation by angle theta uses:
- first row:
cos(theta), -sin(theta) - second row:
sin(theta), cos(theta)
In TensorFlow:
This produces a 2 x 2 tensor that can rotate points in the plane.
For a single angle, the returned shape is just 2 x 2. For a batch of angles, the same construction generalizes naturally to [batch, 2, 2], which is why building the matrix from TensorFlow ops instead of Python lists is so useful.
Applying the Matrix to a Point
tf.linalg.matvec is a clean way to apply a matrix to a vector without manual reshaping.
Batched Angles
In ML workflows, you often need one rotation per sample. Instead of looping in Python, build all matrices at once:
This keeps the computation vectorized and accelerator-friendly.
A 3D Example Around the Z Axis
For 3D, the matrix depends on the chosen axis. A rotation around the z-axis looks like:
Once you have axis-specific matrices, you can compose them with tf.matmul.
Why This Works Well in TensorFlow
Because the matrix is built from TensorFlow ops, gradients flow through it automatically. That matters when the angle is a trainable variable.
That is why it is better to build the matrix with TensorFlow primitives instead of with NumPy inside a training graph.
Common Pitfalls
- Mixing degrees and radians. TensorFlow trig functions expect radians.
- Putting the sine signs in the wrong positions and silently rotating the wrong direction.
- Ignoring batch dimensions and getting matrix-shape mismatches.
- Using NumPy matrix construction in the middle of a differentiable TensorFlow pipeline.
- Forgetting that rotation order matters when composing multiple 3D rotations.
Summary
- A TensorFlow rotation matrix is built from
tf.cosandtf.sin. - Use a
2 x 2matrix for 2D and axis-specific3 x 3matrices for 3D. - '
tf.linalg.matvecandtf.einsumare practical ways to apply rotations.' - Vectorize batched rotations instead of looping in Python.
- Build matrices with TensorFlow ops so gradients remain available.
Related reading
- How to create a sparse layer in Keras i.e. not all neurons are connected to each other?
- How to create a tensorflow serving client for the 'wide and deep' model?
- How to create a Tensorflow Tensorboard Empty Graph
- How to create an optimizer in Tensorflow
- How to create an edge preserving blur similar to a bilateral filter using a limited set of primitive operations
- How to create dataset similar to cifar-10
- how to create confusion matrix for classification in tensorflow
- How to create dataset in the same format as the FSNS dataset?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free 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.