tensorow one-hot 独热编码介绍:
tf.one_hot 函数详解,看了很多介绍写的比较模糊,结合理解看懂了记录下来,分享
给更多需要的朋友!
实例:
import tensorflow as tf
var1 = tf.one_hot(indices=[0,1,2,3],depth=6,axis=0)
var2 = tf.one_hot(indices=[0,1,2,3],depth=6,axis=1)
var3 = tf.one_hot(indices=[0,1,2,3],depth=4,axis=1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
a,b,c = sess.run([var1,var2,var3])
print(a)
print(b)
print(c)
这里的 axis 代表从那个维度开始,depth 代表分类的个数,这里以维度 0 为示例开始介
绍:当 axis=0;代表从第一个维度开始扩展。默认输出维度为(M, depth)这里,
M=Len(indices)=3+1=4,depth=6
var1:如果从第一个维度开始扩展,那输出维度则变为(6, 4)则 a 的输出为:
[[1,0,0,0,],
[0,1,0,0],
[0,0,1,0],
[0,0,0,1],
[0,0,0,0],
[0,0,0,0]]
Var2:如果从第二个维度开始扩展,那输出维度则变为(4, 6)则 b 的输出为:
[[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
最新资源