如何在Tensorflow中创建旋转矩阵

dtr*_*ers 9 python tensorflow

我想在tensorflow中创建一个旋转矩阵,其中所有部分都是张量.

是)我有的:

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)
Run Code Online (Sandbox Code Playgroud)

但是这表示这rotation_matrix是一个张量列表而不是张量本身. theta也是在运行时传入的张量对象.

fab*_*ioM 9

有两个操作:

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)
Run Code Online (Sandbox Code Playgroud)

  • `tf.pack` 已重命名为 `tf.stack`,参见 https://github.com/tensorflow/tensorflow/issues/7550 (2认同)