Tensorflow in one_hot () function parameters and usage

The official website of default is defined as follows:
one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)
The main function of the function is converted into the output one_hot type tensor. 


This parameter is as follows:
  indices indicating the position of the elements of on_value, where instructions are not off_value. indices can be vectors, matrices.
  indicates output depth tensor size, indices of elements does not exceed the default (depth-1), if exceeded, the output is [0,0, ?????, 0]
  on_value default. 1
  off_value default to 0
  DTYPE default tf.float32


The following examples explain a few:
1. a vector indices
. 1  Import tensorflow TF AS
 2  
. 3 indices = [0,2,3,5 ]
 . 4 depth1. 6 =    # indices does not exceed element (depth1) 
. 5 depth2. 4 =    # indices over all elements (depth1) 
. 6 A = TF .one_hot (indices, depth1)
 . 7 B = tf.one_hot (indices, depth2)
 . 8  
. 9  with tf.Session () AS Sess:
 10      Print ( ' A = \ n- ' , sess.run (A))
 . 11      Print ( ' = B \ n- ' , sess.run (B))

Input is one-dimensional, then the output is a two-dimensional:

a = 
 [[1. 0. 0. 0. 0. 0.]
  [0. 0. 1. 0. 0. 0.]
  [0. 0. 0. 1. 0. 0.]
  [0. 0. 0. 0. 0. 1.]]      # shape=(4,6)
b = 
 [[1. 0. 0. 0.]
  [0. 0. 1. 0.]
  [0. 0. 0. 1.]
  [0. 0. 0. 0.]]          # shape=(4,4)

2. indices is a matrix

. 1  Import tensorflow TF AS
 2  
. 3 indices = [[2,3], [l, 4 ]]
 . 4 depth1. 9 =    # indices does not exceed element (depth1) 
. 5 depth2. 4 =    # indices over all elements (depth1) 
. 6 A = tf.one_hot (indices, depth1)
 . 7 B = tf.one_hot (indices, depth2)
 . 8  
. 9  with tf.Session () AS Sess:
 10      Print ( ' A = \ n- ' , sess.run (A))
 . 11      Print ( ' B = \ n- ' , sess.run (B))

Enter the two-dimensional, three-dimensional output:

a = 
 [[[0. 0. 1. 0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 1. 0. 0. 0. 0. 0.]]

  [[0. 1. 0. 0. 0. 0. 0. 0. 0.]
   [0. 0. 0. 0. 1. 0. 0. 0. 0.]]]    # shape=(2,2,9)
b = 
 [[[0. 0. 1. 0.]
   [0. 0. 0. 1.]]

  [[0. 1. 0. 0.]
   [0. 0. 0. 0.]]]             # shape=(2,2,4)

 

 

Guess you like

Origin www.cnblogs.com/muzidaitou/p/11262820.html