2017-02-07 3 views
2

Мне нужно преобразовать мои строковые метки в векторы типа [0, 0, ..., 1, ... 0].
Насколько я понял, это то, что называется одним горячим вектором.
У меня есть 10 классов, поэтому 10 различных строковых меток.
Как я могу кодировать метки в TensorFlow?

Может ли кто-нибудь помочь в прямом и обратном преобразовании?
Я новичок в тензорном потоке, поэтому, пожалуйста, будьте добрыми.

ответ

4

Прямое направление легко, так как это tf.one_hot оп:

import tensorflow as tf 

original_indices = tf.constant([1, 5, 3]) 
depth = tf.constant(10) 
one_hot_encoded = tf.one_hot(indices=original_indices, depth=depth) 

with tf.Session(): 
    print(one_hot_encoded.eval()) 

Выходы:

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

Инверсия это не так уж плох, с tf.where найти не- нулевые показатели:

def decode_one_hot(batch_of_vectors): 
    """Computes indices for the non-zero entries in batched one-hot vectors. 

    Args: 
    batch_of_vectors: A Tensor with length-N vectors, having shape [..., N]. 
    Returns: 
    An integer Tensor with shape [...] indicating the index of the non-zero 
    value in each vector. 
    """ 
    nonzero_indices = tf.where(tf.not_equal(
     batch_of_vectors, tf.zeros_like(batch_of_vectors))) 
    reshaped_nonzero_indices = tf.reshape(
     nonzero_indices[:, -1], tf.shape(batch_of_vectors)[:-1]) 
    return reshaped_nonzero_indices 

with tf.Session(): 
    print(decode_one_hot(one_hot_encoded).eval()) 

Печать:

[1 5 3] 
Смежные вопросы