2016-05-05 23 views
5

Ich möchte eine Rotationsmatrix im Tensorflow erstellen, in der alle Teile davon Tensoren sind.So erstellen Sie eine Rotationsmatrix in Tensorflow

Was ich habe:

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) 

Aber das sagt, dass rotation_matrix ist eine Liste von Tensoren anstelle eines Tensor selbst. theta ist auch ein Tensor-Objekt, das zur Laufzeit übergeben wird.

Antwort

5

mit zwei Operationen:

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) 
+0

Dies ist eine gute Lösung! –

+0

Ich habe darüber nachgedacht, aber dann habe ich es vergessen. Ich denke, das ist eloquenter als das, was ich gerade mache. – dtracers

+1

'tf.pack' wurde in' tf.stack' umbenannt, siehe https://github.com/tensorflow/tensorflow/issues/7550 – Hooked

0

Eine Option I, das funktioniert, ist gefunden Packung zu verwenden, aber wenn es eine bessere Art und Weise ist schreiben Sie bitte eine Antwort:

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) 
1

die Frage des 'zu beantworten Wie man eine Rotationsmatrix 'baut, ist das folgende sauberer als erfordert eine multiple pack (stack) Anrufe:

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