如何在 TensorFlow 中编码标签? [英] How can I encode labels in TensorFlow?

查看:28
本文介绍了如何在 TensorFlow 中编码标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将我的字符串标签转换为像 [0, 0, ... , 1, ... 0] 这样的向量.
据我所知,这是一种称为一个热向量的东西.
我有 10 个类,所以有 10 个不同的字符串标签.

I need to convert my string labels into vectors like [0, 0, ... , 1, ... 0].
As far as I could understand this is something that called one hot vector.
I have 10 classes, so 10 different string labels.

有人可以帮忙做正反变换吗?
我是 tensorflow 的新手,所以请多多关照.

Could anyone please help with direct and inverse transformation?
I'm newbie in tensorflow so please be kind.

推荐答案

前进方向很容易,因为有 tf.one_hot op:

The forward direction is easy, since there's the 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 来查找非零索引:

The inverse of this isn't too bad either, with tf.where to find the non-zero indices:

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]

这篇关于如何在 TensorFlow 中编码标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆