tensorflow中的tf.one_hot函数

tf.one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)

indices:被one_hot的值.
depth:用depth值为每个one_hot的向量长度.如果depth不够,后面都用off_value表示.
on_value:通常0-1中1对应的值.
off_value:通常0-1中0对应的值.
axis:如果indices是一个向量的话:

features * depth if axis == -1
depth * features if axis == 0
当indices的shape是[batch, feature]的形式时,output的shape为:
batch * features * depth if axis == -1
batch * depth * features if axis == 1
depth * batch * features if axis == 0
arr = np.array([[1, 1, 2, 3, 1],
                [2, 1, 2, 2, 3]])

arr_onehot = tf.one_hot(arr, 3)
print(arr_onehot)

with tf.Session() as sess:
    print(sess.run(arr_onehot))

结果:
Tensor("one_hot_4:0", shape=(2, 5, 3), dtype=float32)
[[[ 0.  1.  0.]
  [ 0.  1.  0.]
  [ 0.  0.  1.]
  [ 0.  0.  0.]
  [ 0.  1.  0.]]

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

numpy中三维数组的shape

a = np.array([[[1, 2, 3], [2, 3, 3]],
[[1, 2, 3],[1, 3, 4]], 
[[1, 2, 3],[1, 3, 4]], 
[[1, 2, 3],[1, 3, 4]]])
In [44]:a.shape
Out[44]: (4, 2, 3)

4个2*3的数组,4对应batch,2对应features,3对应depth.

你可能感兴趣的:(tensorflow)