前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >tf.one_hot

tf.one_hot

作者头像
狼啸风云
修改2022-09-04 20:51:50
1.1K0
修改2022-09-04 20:51:50
举报
文章被收录于专栏:计算机视觉理论及其实现
代码语言:javascript
复制
tf.one_hot(
    indices,
    depth,
    on_value=None,
    off_value=None,
    axis=None,
    dtype=None,
    name=None
)

返回一个独热张量。 索引中由索引表示的位置取值on_value,而所有其他位置取值off_value。on_value和off_value必须具有匹配的数据类型。如果还提供了dtype,则它们必须与dtype指定的数据类型相同。如果没有提供on_value,它将默认为值1,类型为dtype。如果没有提供off_value,它将默认值为0,类型为dtype。如果输入索引的秩为N,那么输出的秩为N+1。新轴是在维度轴上创建的(缺省值:新轴附加在末尾)。如果索引是标量,则输出形状将是长度深度向量。如果索引是长度特征向量,则输出形状为:

代码语言:javascript
复制
  features x depth if axis == -1
  depth x features if axis == 0

如果索引是一个形状为[batch, features]的矩阵(batch),则输出形状为:

代码语言:javascript
复制
  batch x features x depth if axis == -1
  batch x depth x features if axis == 1
  depth x batch x features if axis == 0

如果没有提供dtype,它将尝试假设数据类型为on_value或off_value(如果传入了一个或两个值)。如果不提供on_value、off_value或dtype, dtype将默认为tf.float32。注意:如果需要非数值数据类型输出(tf)。字符串,特遣部队。bool等),on_value和off_value都必须提供给one_hot。

例如:

代码语言:javascript
复制
indices = [0, 1, 2]
depth = 3
tf.one_hot(indices, depth)  # output: [3 x 3]
# [[1., 0., 0.],
#  [0., 1., 0.],
#  [0., 0., 1.]]

indices = [0, 2, -1, 1]
depth = 3
tf.one_hot(indices, depth,
           on_value=5.0, off_value=0.0,
           axis=-1)  # output: [4 x 3]
# [[5.0, 0.0, 0.0],  # one_hot(0)
#  [0.0, 0.0, 5.0],  # one_hot(2)
#  [0.0, 0.0, 0.0],  # one_hot(-1)
#  [0.0, 5.0, 0.0]]  # one_hot(1)

indices = [[0, 2], [1, -1]]
depth = 3
tf.one_hot(indices, depth,
           on_value=1.0, off_value=0.0,
           axis=-1)  # output: [2 x 2 x 3]
# [[[1.0, 0.0, 0.0],   # one_hot(0)
#   [0.0, 0.0, 1.0]],  # one_hot(2)
#  [[0.0, 1.0, 0.0],   # one_hot(1)
#   [0.0, 0.0, 0.0]]]  # one_hot(-1)

参数:

  • indices: 指标的张量。
  • depth: 定义一个热维的深度的标量。
  • on_value: 一个标量,定义了当索引[j] = i时要填充输出的值。
  • off_value: 一个标量,定义当索引[j] != i.(默认值:0)时要填充输出的值。
  • axis: 要填充的轴(默认值:-1,一个新的最内层轴)。
  • dtype: 输出张量的数据类型。
  • name: 操作的名称(可选)

返回值:

  • output: 一个独热张量。

异常:

  • TypeError: If dtype of either on_value or off_value don't match dtype
  • TypeError: If dtype of on_value and off_value don't match one another

原链接: https://tensorflow.google.cn/versions/r1.9/api_docs/python/tf/one_hot?hl=en

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2019年06月18日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
批量计算
批量计算(BatchCompute,Batch)是为有大数据计算业务的企业、科研单位等提供高性价比且易用的计算服务。批量计算 Batch 可以根据用户提供的批处理规模,智能地管理作业和调动其所需的最佳资源。有了 Batch 的帮助,您可以将精力集中在如何分析和处理数据结果上。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档