tf.one_hot( ) axis参数详细说明

上原型:

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

indices: 代表了on_value所在的索引,其他位置值为off_value。类型为tensor,其尺寸与depth共同决定输出tensor的尺寸。

depth:编码深度。

on_value & off_value为编码开闭值,缺省分别为1和0,indices指定的索引处为on_value值;

axis:编码的轴,分情况可取-1、0或-1、0、1,默认为-1

dtype:默认为 on_value 或 off_value的类型,若未提供on_value或off_value,则默认为tf.float32类型。

返回一个 one-hot tensor。

axis说明

indices是一个长度为features的向量

当axis==0,输出尺寸为:depth * features

indices = [2,3,1,6]#长度为features=4的向量
dep = 3
asa = tf.one_hot(indices,dep,axis=0)
with tf.Session() as sess:
    print('hot: ', sess.run(asa))

结果为:

hot:  [[0. 0. 0. 0.]
 [0. 0. 1. 0.]
 [1. 0. 0. 0.]]

尺寸为3 * 4也就是:depth * features

当axis==-1,输出尺寸为:features * depth

indices = [2,3,1,6]#长度为features=4的向量
dep = 3
asa = tf.one_hot(indices,dep,axis=-1)
with tf.Session() as sess:
    print('hot: ', sess.run(asa))

结果为:

hot:  [[0. 0. 1.]
 [0. 0. 0.]
 [0. 1. 0.]
 [0. 0. 0.]]

尺寸为4 * 3也就是:features * depth

当axis = 1时,同axis = -1。

indices是一个尺寸为[batch,features]的矩阵

当axis==0,输出尺寸为:depth * batch * features,如下代码:

indices = [[2,3,4],[1,6,7]]#[2,3]的矩阵
dep = 4
asa = tf.one_hot(indices,dep,axis=0)
with tf.Session() as sess:
    print('hot: ', sess.run(asa))

输出结果:

hot:  [[[0. 0. 0.]
  [0. 0. 0.]]

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

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

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

尺寸为[4,2,3]也就是:depth * batch * features

当axis==-1,输出尺寸为:batch * features * depth

indices = [[2,3,4],[1,6,7]]#[2,3]的矩阵
dep = 4
asa = tf.one_hot(indices,dep,axis=-1)
with tf.Session() as sess:
    print('hot: ', sess.run(asa))

输出结果为:

hot: [[[0. 0. 1. 0.]
[0. 0. 0. 1.]
[0. 0. 0. 0.]]

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

尺寸为[2,3,4]也就是:batch * features * depth

当axis==1,输出尺寸为:batch * depth * features

indices = [[2,3,4],[1,6,7]]#[2,3]的矩阵
dep = 4
asa = tf.one_hot(indices,dep,axis=1)
with tf.Session() as sess:
    print('hot: ', sess.run(asa))

输出结果为:

hot:  [[[0. 0. 0.]
  [0. 0. 0.]
  [1. 0. 0.]
  [0. 1. 0.]]

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

尺寸为[2,4,3]也就是:batch * depth * features

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值