diffeomorphism
diffeomorphism

Reputation: 1071

Transposing dimensions after a reshape: when it is required?

tf.reshape can change the dimension of a tensor data structure, but is there any guarantee about how will be the data ordered on each dimension?

suppose I have a tensor with dimensions A[objects, x, t] that describes some value across different objects, positions and time, and another that is just across B[x,t]. I usually have to do reshapes in order to copy B over dimensions 0 like this:

B_res = tf.tile( tf.reshape( B , [1, X_SIZE, T_SIZE]), tf.pack([OBJECT_SIZE,1,1]))
some_op = tf.mul( A, B_res )

The problem I see is when X_SIZE == T_SIZE reshape does not have any way to know how to arrange B data in the reshape, for all that I know, it could be aligning data along dimension 0 of B along dimension 2 of B_res!

Is there any rules for how reshape orders data? I want to know if a few tf.transpose operations are required after a certain tf.reshape

Upvotes: 1

Views: 1108

Answers (1)

chasep255
chasep255

Reputation: 12185

In memory arrays / tensors are really just 1D objects. So say you need to store a 10x10 array. In memory this would just be a 1D array of length 100 where the first 10 elements correspond to the first row. This is known as row major ordering. Say you want to reshape it to the shape 20x5. The underlying memory is not changed by the reshape so the first ten elements now make up rows 1 and 2. Transpose on the other hand physically reorders the memory to maintain the row major ordering while changing the location of the dimensions.

Also I think you are tiling unessarily. You should read up on broadcasting. I think you could do the following.

some_op = A * tf.reshape(B, [1, X_SIZE, T_SIZE])

In this case it will automatically broadcast B along the first dimension of A.

Note. I am not actually sure if tensorflow is using row major or column major ordering but the same concepts still apply.

Upvotes: 1

Related Questions