Skip to content

Not clear how to construct "None" #264

Open

Description

@cowwoc

I'm trying to invoke the equivalent of this python API: https://www.tensorflow.org/api_docs/python/tf/transpose

where perm=None by default.

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:

  1. Should the perm parameter be optional?
  2. How does one construct None?

Activity

  1. rnett commented on Mar 31, 2021

    @rnett
    Contributor

    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.

  2. cowwoc commented on Mar 31, 2021

    @cowwoc
    Author

    @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 perm is None:

        rank = a.shape.rank
        if rank is None:
          perm = gen_math_ops._range(gen_array_ops.rank(a) - 1, -1, -1)
        else:
          perm = np.arange(rank - 1, -1, -1, dtype=np.int32)
    

    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.

  3. rnett commented on Mar 31, 2021

    @rnett
    Contributor

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

  4. cowwoc commented on Apr 1, 2021

    @cowwoc
    Author

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

  5. rnett commented on Apr 1, 2021

    @rnett
    Contributor

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

    if rank is None:
        perm = gen_math_ops._range(gen_array_ops.rank(a) - 1, -1, -1)
    else:
        perm = np.arange(rank - 1, -1, -1, dtype=np.int32)

    translates to

    Operand<TNumber> perm;
    if(x.shape().isUnknown()){
        perm = tf.range(tf.math.sub(tf.rank(x), tf.constant(1)), tf.constant(-1), tf.constant(-1))
    } else {
        perm = tf.range(tf.constant(x.shape().numDimensions() - 1), tf.constant(-1), tf.constant(-1))
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions