ThoughtWorksInc/Compute.scala

Add Tensor#transpose

Closed this issue · 6 comments

Atry commented

transpose should create a view (i.e. TransformedTensor) of the original Tensor.

This can be implemented from Tensor#transform.

Check the implementation of Tensor#permute as an example for creating a view.

In a TransformedTensor, what does matrix represent?

In the comments we have:
The matrix size is __number of dimensions of original tensor × number of dimensions of new tensor__.

So in the following case:

val x:Tensor = Tensor(Array(Seq(1.0f, 2.0f), Seq(4.0f, 5.0f)))
x: com.thoughtworks.compute.cpu.Tensor = [[1.0,2.0],[4.0,5.0]]

Then if we permute the columns of (the matrix of) x:

x.permute(Array(1,0))
res2: com.thoughtworks.compute.cpu.TransformedTensor = [[1.0,4.0],[2.0,5.0]]

(WAIT - this is the transpose, not the permutation, right??)

then the matrix is:

x.permute(Array(1,0)).matrix
res3: com.thoughtworks.compute.NDimensionalAffineTransform.MatrixData = Array(0.0, 1.0, 0.0, 1.0, 0.0, 0.0)

The above comment would make me think this matrix should be of length 2*2 = 4, but it has length 6. Also what is that matrix? You can achieve the transformation by multiplying by a permutation matrix, but that is again 2x2, not 2x3.

Atry commented

The last row of the matrix is not stored in the array because it is always 0 0 ... 0 0 1.
See https://docs.oracle.com/javase/8/docs/api/java/awt/geom/AffineTransform.html for instance of the same trick.

Atry commented

The Scaladoc is wrong. I will fix it.

#157

Good catch!

Atry commented

Also https://en.wikipedia.org/wiki/Transformation_matrix is good point to start to understand TransformedTensor.

Atry commented

(WAIT - this is the transpose, not the permutation, right??)

IIRC, transpose is just a special case of permutation

Ah by permute I thought we were talking about permuting the columns of a matrix (equivalent to changing the basis). Instead it's permuting the dimensions of a (possibly) higher-dimensional tensor. In that case, yes, transpose is a subcase of permute.