How to create a rotation matrix in Tensorflow

I want to create a rotation matrix in a tensor flow, where all its parts are tensors.

What I have:

def rotate(tf, points, theta): rotation_matrix = [[tf.cos(theta), -tf.sin(theta)], [tf.sin(theta), tf.cos(theta)]] return tf.matmul(points, rotation_matrix) 

But this suggests that rotation_matrix is a list of tensors instead of the tensor itself. theta also a tensor object that is passed at runtime.

+5
source share
3 answers

with two operations:

 def rotate(tf, points, theta): rotation_matrix = tf.pack([tf.cos(theta), -tf.sin(theta), tf.sin(theta), tf.cos(theta)]) rotation_matrix = tf.reshape(rotation_matrix, (2,2)) return tf.matmul(points, rotation_matrix) 
+5
source

To answer the question "How to build a rotation matrix", the following is cleaner than requiring multiple calls to pack ( stack ):

 tf.stack([(tf.cos(angle), -tf.sin(angle)), (tf.sin(angle), tf.cos(angle))], axis=0) 
+1
source

Option I found that the job is to use the package, but if there is a better way, write the answer:

 def rotate(tf, points, theta): top = tf.pack([tf.cos(theta), -tf.sin(theta)]) bottom = tf.pack([tf.sin(theta), tf.cos(theta)]) rotation_matrix = tf.pack([top, bottom]) return tf.matmul(points, rotation_matrix) 
0
source

All Articles