tensorflow学习笔记--tf.one_hot

tf.one_hot输入参数为:

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

1.如果indices为一个数值,则tf.one_hot返回值为一个长度为depth的向量.

2.如果indices为一个长度为features的向量,那么tf.one_hot的返回值一个矩阵,矩阵大小为:

axis=-1,shapa=[features,depth]

axis=0,shapa=[depth,features]

3.如果indices的唯一个矩阵,shape为[batch, features], 则返回值一个三维矩阵,矩阵大小为:

axis=-1,shape = bathc, features, depth]

axis=1,shape = [bathc, depth, features]

axis=0,shape = [depth, bathc, features]

首先根据上述条件,初始化一个值为off_value的向量或者矩阵, 并在其对应索引indices处的职位on_value,其他处的值为off_value,如果on_value,off_value为None,则对应的值为0.

例如对于下面代码:

logits = tf.convert_to_tensor([[0.2, 0.3, 0.4, 0.5], [0.5, 0.4, 0.3, 0.2]])
labels = slim.one_hot_encoding([1, 2], 4)
bbox = tf.ones_like(logits)
with tf.Session() as sess:
    print sess.run(labels)
sess.close()

得到输出矩阵为:

这里写图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值