2
我需要將我的字符串標籤轉換爲像[0,0,...,1,... 0]這樣的向量。
據我所知,這是一種叫做熱矢量的東西。
我有10個類,所以有10個不同的字符串標籤。
如何在TensorFlow中對標籤進行編碼?
任何人都可以請直接和反向轉換的幫助嗎?
我是tensorflow新手,所以請親切。
我需要將我的字符串標籤轉換爲像[0,0,...,1,... 0]這樣的向量。
據我所知,這是一種叫做熱矢量的東西。
我有10個類,所以有10個不同的字符串標籤。
如何在TensorFlow中對標籤進行編碼?
任何人都可以請直接和反向轉換的幫助嗎?
我是tensorflow新手,所以請親切。
的前進方向是很容易的,因爲還有的tf.one_hot
OP:
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]