I'm not familiar with Tensorflow but after some tests, this is what I've found:
tf.one_hot()
takes an indices
and a depth
. The indices
are the values to actually convert to a one-hot encoding. depth
refers to the maximum value to utilize.
For example, take the following code:
y = [1, 2, 3, 2, 1]
tf.keras.utils.to_categorical(y)
sess = tf.Session();
with sess.as_default():
print(tf.one_hot(y, 2).eval())
print(tf.one_hot(y, 4).eval())
print(tf.one_hot(y, 6).eval())
tf.keras.utils.to_categorical(y)
Returns the following:
array([[0., 1., 0., 0.],
[0., 0., 1., 0.],
[0., 0., 0., 1.],
[0., 0., 1., 0.],
[0., 1., 0., 0.]], dtype=float32)
In contrast, the tf.one_hot()
options (2, 4, and 6) do the following:
[[0. 1.]
[0. 0.]
[0. 0.]
[0. 0.]
[0. 1.]]
[[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]
[0. 0. 1. 0.]
[0. 1. 0. 0.]]
[[0. 1. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 1. 0. 0. 0. 0.]]
As can be seen here, to mimic tf.keras.utils.to_categorical()
using tf.one_hot()
, the depth
parameter should be equivalent to the maximum value present in the array, +1 for 0. In this case, the maximum value is 3, so there are four possible values in the encoding - 0, 1, 2, and 3. As such, a depth of 4 is required to represent all of these values in the one-hot encoding.
As for conversion to numpy, as shown above, using a Tensorflow session, running eval()
on a tensor converts it to a numpy array. For methods on doing this, refer to How can I convert a tensor into a numpy array in TensorFlow?.
I'm not familiar with Tensorflow but I hope this helps.
Note: for the purposes of MNIST, a depth of 10 should be sufficient.