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.
-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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With