您好,欢迎来到爱够旅游网。
搜索
您的当前位置:首页TensorFlow tf.one_hot()函数

TensorFlow tf.one_hot()函数

来源:爱够旅游网

tf.one_hot函数可以将输入数据转成onehot编码。至于什么是onehot编码?为什么要将输入转成onehot编码?请查看我的另一篇博文:

一、参数

def one_hot(indices,
            depth,
            on_value=None,
            off_value=None,
            axis=None,
            dtype=None,
            name=None)

返回值:返回一个onehot编码的张量。

注意:

  1. on_value、off_value、dtype三者数据类型应相同;
  2. 如果indices为标量(scalar),则输出一个长度为“depth”的向量;
  3. 如果indices为长度为“features”的向量(vector),则输出的矩阵维度为:
axis取值输出的矩阵维度
axis == -1features * depth
axis == 0depth * features

     4. 如果indices为一个维度为“[batch, features]”的矩阵,则输出的矩阵维度为:

axis取值输出的矩阵维度
axis == -1batch * features * depth
axis == 1batch * depth * features
axis == 0depth * batch * features

     5. tf.one_hot()函数规定输入的元素indices从0开始,最大的元素值不能超过(depth - 1),因此能够表示depth个单位的输入。若输入的元素值超出范围,输出的编码均为 [0, 0 … 0, 0]。

     6. indices = 0 对应的输出是[1, 0 … 0, 0], indices = 1 对应的输出是[0, 1 … 0, 0], 依次类推,最大可能值的输出是[0, 0 … 0, 1]。

二、实验验证

验证程序:

import tensorflow as tf
import numpy as np

a = tf.Variable(2)
b = tf.Variable([1, 2, 3, 4, 5])
c = tf.Variable([[1, 2], [5, 6], [7, 8]])

a_one_hot1 = tf.one_hot(a, 1)
a_one_hot2 = tf.one_hot(a, 4)
b_one_hot1 = tf.one_hot(b, 6, axis=-1)
b_one_hot2 = tf.one_hot(b, 6, axis=0)
b_one_hot3 = tf.one_hot(b, 6, on_value=5, off_value=2)
b_one_hot4 = tf.one_hot(b, 6, on_value='x', off_value='y')
b_one_hot5 = tf.one_hot(b, 6, on_value='x', off_value='y', dtype=tf.string)
c_one_hot1 = tf.one_hot(c, 8)
c_one_hot2 = tf.one_hot(c, 9,axis=-1)
c_one_hot3 = tf.one_hot(c, 9,axis=1)
c_one_hot4 = tf.one_hot(c, 9,axis=0)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())

    print(sess.run(a_one_hot1))
    print('------------------')
    print(sess.run(a_one_hot2))
    print('------------------')
    print(sess.run(b_one_hot1))
    print('------------------')
    print(sess.run(b_one_hot2))
    print('------------------')
    print(sess.run(b_one_hot3))
    print('------------------')
    print(sess.run(b_one_hot4))
    print('------------------')
    print(sess.run(b_one_hot5))
    print('------------------')
    print(sess.run(c_one_hot1))
    print('------------------')
    print(sess.run(c_one_hot2))
    print('------------------')
    print(sess.run(c_one_hot3))
    print('------------------')
    print(sess.run(c_one_hot4))

输出结果:

[0.] # 最大的元素值超过"depth-1"(1-1),所以显示为0
------------------
[0. 0. 1. 0.] # one_hot(2)
------------------
[[0. 1. 0. 0. 0. 0.] # shape:[5, 6]
 [0. 0. 1. 0. 0. 0.] 
 [0. 0. 0. 1. 0. 0.]
 [0. 0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 0. 1.]]
------------------
[[0. 0. 0. 0. 0.] # shape:[6, 5]
 [1. 0. 0. 0. 0.]
 [0. 1. 0. 0. 0.]
 [0. 0. 1. 0. 0.]
 [0. 0. 0. 1. 0.]
 [0. 0. 0. 0. 1.]]
------------------
[[2 5 2 2 2 2]
 [2 2 5 2 2 2]
 [2 2 2 5 2 2]
 [2 2 2 2 5 2]
 [2 2 2 2 2 5]]
------------------
[[b'y' b'x' b'y' b'y' b'y' b'y']
 [b'y' b'y' b'x' b'y' b'y' b'y']
 [b'y' b'y' b'y' b'x' b'y' b'y']
 [b'y' b'y' b'y' b'y' b'x' b'y']
 [b'y' b'y' b'y' b'y' b'y' b'x']]
------------------
[[b'y' b'x' b'y' b'y' b'y' b'y']
 [b'y' b'y' b'x' b'y' b'y' b'y']
 [b'y' b'y' b'y' b'x' b'y' b'y']
 [b'y' b'y' b'y' b'y' b'x' b'y']
 [b'y' b'y' b'y' b'y' b'y' b'x']]
------------------
[[[0. 1. 0. 0. 0. 0. 0. 0.] # shape:[3, 2, 8]
  [0. 0. 1. 0. 0. 0. 0. 0.]]

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

 [[0. 0. 0. 0. 0. 0. 0. 1.]
  [0. 0. 0. 0. 0. 0. 0. 0.]]] # one_hot(8),由于于"depth-1",所以显示为0
------------------
[[[0. 1. 0. 0. 0. 0. 0. 0. 0.] # shape:[3, 2, 9]
  [0. 0. 1. 0. 0. 0. 0. 0. 0.]]

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

 [[0. 0. 0. 0. 0. 0. 0. 1. 0.]
  [0. 0. 0. 0. 0. 0. 0. 0. 1.]]]
------------------
[[[0. 0.] # shape:[3, 9, 2]
  [1. 0.]
  [0. 1.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]]

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

 [[0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [0. 0.]
  [1. 0.]
  [0. 1.]]]
------------------
[[[0. 0.] # shape:[9, 3, 2]
  [0. 0.]
  [0. 0.]]

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

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

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

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

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

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

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

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

在人工智能的学习过程中,可繁可简,弄清每一个细节需要耗费大量时间精力,文中有不对的地方烦请指出,愿我们一起提高进步。

参考:

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- igbc.cn 版权所有 湘ICP备2023023988号-5

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务