You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Digging into the API I found Ops.linalg.transpose() but the perm parameter is mandatory, and I can't figure out how to construct the equivalent of None. So I have two questions:
Yeah, it's kind of annoying, what you pasted is as close as you can get to "equivalent behavior", and it's not particularly clear or easy to convert. The docs are a bit more helpful though: If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor., which is something you can do in Java. Probably easiest to do it manually if you know the rank.
That said, you can't transpose something with only one dimension. Transposing reverses the dimension order, and with only one dimension that changes nothing. If you're going for a row -> column or similar, you'll need to broadcast it first (see this).
I don't have the code in front of me but something like tf.linalg.transpose(tf.shape.reshape(x, tf.array(1, -1)), tf.array(1, 0)). Although since you're reshaping anyways I think you can skip the transpose and just do tf.reshape(x, tf.array(-1, 1)).
For reference, @Craigacp mentioned on Gitter that np.matmul(w_real,x_data.T) in python is equivalent to Ops.linalg.matMul(w_real, x_data, MatMul.transposeB(true)). The options provide a convenient way to transpose the first or second argument so if you happen to be transposing in the context of a matMul() then you're in luck; otherwise, I still don't understand:
If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor., which is something you can do in Java. Probably easiest to do it manually if you know the rank.
I mean, I don't understand how to translate the above sentence into the second parameter of the transpose() method. Maybe a concrete example would help :)
This would be the equivalent of the above for a 2D array: tf.linalg.transpose(x, tf.array(1, 0)). Or tf.linalg.transpose(x, tf.range(tf.math.sub(tf.rank(x), tf.constant(1)), tf.constant(-1), tf.constant(-1))) for any rank. But docs are going to reference things like rank and (n-1...0) (which is the TF docs syntax for a range) a lot if you look at some of the other methods, so I suggest reading up on them (rank is just the number of dimensions, fyi).
Activity
rnett commentedon Mar 31, 2021
There isn't one, the actual ops don't support None, it's all Python-side. If you look at the actual Python op method (https://github.com/tensorflow/tensorflow/blob/85c8b2a817f95a3e979ecd1ed95bff1dc1335cff/tensorflow/python/ops/array_ops.py#L2140, it's called from the one the docs link to) you can see how it's handled.
cowwoc commentedon Mar 31, 2021
@rnett I'm not sure I understand. Big picture, I just want to calculate the transpose of a 1D vector. How would I do this using the Java API?
Digging into the python code I see this code is executed if
permisNone:I don't know how to implement the equivalent behavior using the Java API and I'm not sure whether you were trying to tell me that I needed to do so.
rnett commentedon Mar 31, 2021
Yeah, it's kind of annoying, what you pasted is as close as you can get to "equivalent behavior", and it's not particularly clear or easy to convert. The docs are a bit more helpful though:
If perm is not given, it is set to (n-1...0), where n is the rank of the input tensor., which is something you can do in Java. Probably easiest to do it manually if you know the rank.That said, you can't transpose something with only one dimension. Transposing reverses the dimension order, and with only one dimension that changes nothing. If you're going for a row -> column or similar, you'll need to broadcast it first (see this).
I don't have the code in front of me but something like
tf.linalg.transpose(tf.shape.reshape(x, tf.array(1, -1)), tf.array(1, 0)). Although since you're reshaping anyways I think you can skip the transpose and just dotf.reshape(x, tf.array(-1, 1)).cowwoc commentedon Apr 1, 2021
For reference, @Craigacp mentioned on Gitter that
np.matmul(w_real,x_data.T)in python is equivalent toOps.linalg.matMul(w_real, x_data, MatMul.transposeB(true)). The options provide a convenient way to transpose the first or second argument so if you happen to be transposing in the context of amatMul()then you're in luck; otherwise, I still don't understand:I mean, I don't understand how to translate the above sentence into the second parameter of the
transpose()method. Maybe a concrete example would help :)rnett commentedon Apr 1, 2021
This would be the equivalent of the above for a 2D array:
tf.linalg.transpose(x, tf.array(1, 0)). Ortf.linalg.transpose(x, tf.range(tf.math.sub(tf.rank(x), tf.constant(1)), tf.constant(-1), tf.constant(-1)))for any rank. But docs are going to reference things likerankand(n-1...0)(which is the TF docs syntax for a range) a lot if you look at some of the other methods, so I suggest reading up on them (rank is just the number of dimensions, fyi).translates to