def as_one_hot(input_, n_indices):
 shape = input_.get_shape().as_list()
 n_elem = numpy.prod(shape)
 indices = tf.range(n_elem)
 indices = tf.cast(indices, tf.int64)
 indices_input = tf.concat(0, [indices, tf.reshape(input_, [(-1)])])
 indices_input = tf.reshape(indices_input, [2, (-1)])
 indices_input = tf.transpose(indices_input)
 res = tf.sparse_to_dense(indices_input, [n_elem, n_indices], 1.0, 0.0, name='flat_one_hot')
 res = tf.reshape(res, ([elem for elem in shape] + [n_indices]))
 return res

def as_one_hot ( input_ , n_indices ) : shape = input_ . get_shape ( ) . as_list ( ) n_elem = numpy . prod ( shape ) indices = tf . range ( n_elem ) indices = tf . cast ( indices , tf . int64 ) indices_input = tf . concat ( 0 , [ indices , tf . reshape ( input_ , [ ( - 1 ) ] ) ] ) indices_input = tf . reshape ( indices_input , [ 2 , ( - 1 ) ] ) indices_input = tf . transpose ( indices_input ) res = tf . sparse_to_dense ( indices_input , [ n_elem , n_indices ] , 1.0 , 0.0 , name = 'flat_one_hot' ) res = tf . reshape ( res , ( [ elem for elem in shape ] + [ n_indices ] ) ) return res
convert indices to one-hot .