Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In TensorFlow, what is the argument 'axis' in the function 'tf.one_hot'

Could anyone help with an an explanation of what axis is in TensorFlow's one_hot function?

According to the documentation:

axis: The axis to fill (default: -1, a new inner-most axis)

Closest I came to an answer on SO was an explanation relevant to Pandas:

Not sure if the context is just as applicable.

like image 249
user919426 Avatar asked Jan 03 '18 18:01

user919426


People also ask

What is axis=- 1 in TensorFlow?

-1 means the last axis; Since you have a rank 2 tensor, the last axis is the second axis, that is, along the rows; tf. reduce_sum with axis=-1 will thus reduce (sum) the second dimension.

What is an axis in Tensorflow?

axis=1. You add a second dimension and the first dimension is kept. This would result in a (6,4) array. So for the resulting array, you use the first dimension (0) to know which example you see and the second dimension (1, the new one) to know if that class is active.

What does the axis parameter of TF Expand_dims do?

expand_dims() is used to insert an addition dimension in input Tensor. Parameters: input: It is the input Tensor. axis: It defines the index at which dimension should be inserted.

What does TF one_hot do?

The tf. one_hot operation takes a list of category indices and a depth (for our purposes, essentially a number of unique categories), and outputs a One Hot Encoded Tensor.


1 Answers

Here's an example:

x = tf.constant([0, 1, 2]) 

... is the input tensor and N=4 (each index is transformed into 4D vector).

axis=-1

Computing one_hot_1 = tf.one_hot(x, 4).eval() yields a (3, 4) tensor:

[[ 1.  0.  0.  0.]  [ 0.  1.  0.  0.]  [ 0.  0.  1.  0.]] 

... where the last dimension is one-hot encoded (clearly visible). This corresponds to the default axis=-1, i.e. the last one.

axis=0

Now, computing one_hot_2 = tf.one_hot(x, 4, axis=0).eval() yields a (4, 3) tensor, which is not immediately recognizable as one-hot encoded:

[[ 1.  0.  0.]  [ 0.  1.  0.]  [ 0.  0.  1.]  [ 0.  0.  0.]] 

This is because the one-hot encoding is done along the 0-axis and one has to transpose the matrix to see the previous encoding. The situation becomes more complicated, when the input is higher dimensional, but the idea is the same: the difference is in placement of the extra dimension used for one-hot encoding.

like image 144
Maxim Avatar answered Oct 20 '22 03:10

Maxim