tf.one_hot函数可以将输入数据转成onehot编码。至于什么是onehot编码?为什么要将输入转成onehot编码?请查看我的另一篇博文:https://blog.csdn.net/duanlianvip/article/details/93492855
一、参数
def one_hot(indices,
depth,
on_value=None,
off_value=None,
axis=None,
dtype=None,
name=None)
返回值:返回一个onehot编码的张量。
- indices:输入的张量[batch, features](待转onehot编码张量);
- depth:onehot编码的一个维度;
- on_value=None:默认值为1
- off_value=None:默认值为0
- axis=None:默认为-1
- dtype=None:输出张量的数据类型。若dtype缺省,则其根据on_value或off_value的数据类型自动保持一致;若dtype、on_value、off_value都缺省,则默认为tf.float32
- name=None:本次one_hot操作的名称
注意:
- on_value、off_value、dtype三者数据类型应相同;
- 如果indices为标量(scalar),则输出一个长度为“depth”的向量;
- 如果indices为长度为“features”的向量(vector),则输出的矩阵维度为:
axis取值 | 输出的矩阵维度 |
---|---|
axis == -1 | features * depth |
axis == 0 | depth * features |
4. 如果indices为一个维度为“[batch, features]”的矩阵,则输出的矩阵维度为:
axis取值 | 输出的矩阵维度 |
---|---|
axis == -1 | batch * features * depth |
axis == 1 | batch * depth * features |
axis == 0 | depth * 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),由于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.]]]
在人工智能的学习过程中,可繁可简,弄清每一个细节需要耗费大量时间精力,文中有不对的地方烦请指出,愿我们一起提高进步。
参考: