TensorFlow, tf.one_hot, why is the output shape determined by the axis value?

I read the docs tf.one_hot and found that

.... A new axis is created on the dimension axis (default: new axis is added at the end).

What is it The new axis

?

If the indices are vectors of length functions, the output form would be:

shows depth x if axis == -1

     

depth functions x if axis == 0

If the indices are a matrix (package) with the form [batch, functions], the output form would be:

batch x has x depth if axis == -1

     

functions xxxx if axis == 1

     

depth x batch functions x if axis == 0

Why is the output shape determined by the axis?

+3


source to share


1 answer


tf.one_hot()

converts a list of indices (for example, [0, 2, 1]

) and converts it to a list of single-jet length vectors depth

.

For example, if depth = 3

,

  • index 0 at the input will be replaced with [1, 0, 0]
  • index 1 at the input will be replaced with [0, 1, 0]
  • index 2 at the input will be replaced by [0, 0, 1]

So [0, 2, 1]

would be encoded as[[1, 0, 0], [0, 0, 1], [0, 1, 0]]



As you can see, the output has one more dimension than the input (since each index is replaced by a vector).

By default ( and what you usually want ) a new dimension is created as the last one, so if your input has a shape (d1, d2, .., dn)

, your result will have a shape (d1, d2, .., dn, depth)

, But if you change the axis of the inputs, you can choose a new dimension elsewhere, for example if axis=0

your the output will be in the form (depth, d1, d2, .., dn)

.

Changing the order of sizes is basically an n-dimensional version of the transpose: you have the same data, but switch the order of the indices to access it (which is equivalent to switching columns and rows in a 2D matrix).

+9


source







All Articles