官网默认定义如下:
one_hot(indices, depth, on_value=None, off_value=None, axis=None, dtype=None, name=None)
该函数的功能主要是转换成one_hot类型的张量输出。

参数功能如下:
  1)indices中的元素指示on_value的位置,不指示的地方都为off_value。indices可以是向量、矩阵。
  2)depth表示输出张量的尺寸,indices中元素默认不超过(depth-1),如果超过,输出为[0,0,···,0]
  3)on_value默认为1
  4)off_value默认为0
  5)dtype默认为tf.float32 下面用几个例子说明一下:
1. indices是向量
 import tensorflow as tf

 indices = [0,2,3,5]
depth1 = 6 # indices没有元素超过(depth-1)
depth2 = 4 # indices有元素超过(depth-1)
a = tf.one_hot(indices,depth1)
b = tf.one_hot(indices,depth2) with tf.Session() as sess:
print('a = \n',sess.run(a))
print('b = \n',sess.run(b))

运行结果:

# 输入是一维的,则输出是一个二维的
a =
[[1. 0. 0. 0. 0. 0.]
[0. 0. 1. 0. 0. 0.]
[0. 0. 0. 1. 0. 0.]
[0. 0. 0. 0. 0. 1.]]      # shape=(4,6)
b =
[[1. 0. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]
[0. 0. 0. 0.]]          # shape=(4,4)

2. indices是矩阵

 import tensorflow as tf

 indices = [[2,3],[1,4]]
depth1 = 9 # indices没有元素超过(depth-1)
depth2 = 4 # indices有元素超过(depth-1)
a = tf.one_hot(indices,depth1)
b = tf.one_hot(indices,depth2) with tf.Session() as sess:
print('a = \n',sess.run(a))
print('b = \n',sess.run(b))

运行结果:

# 输入是二维的,则输出是三维的
a =
[[[0. 0. 1. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 1. 0. 0. 0. 0. 0.]] [[0. 1. 0. 0. 0. 0. 0. 0. 0.]
[0. 0. 0. 0. 1. 0. 0. 0. 0.]]]    # shape=(2,2,9)
b =
[[[0. 0. 1. 0.]
[0. 0. 0. 1.]] [[0. 1. 0. 0.]
[0. 0. 0. 0.]]]             # shape=(2,2,4)
 

Tensorflow中one_hot() 函数用法的更多相关文章

  1. 查询tensorflow中的函数用法

    一下均在ubuntu环境下: (1)方法一,使用help()函数: 比如对于tf.placeholder(),在命令行中输入import tensorflow as tf , help(tf.plac ...

  2. Oracle 中 decode 函数用法

    Oracle 中 decode 函数用法 含义解释:decode(条件,值1,返回值1,值2,返回值2,...值n,返回值n,缺省值) 该函数的含义如下:IF 条件=值1 THEN RETURN(翻译 ...

  3. php中opendir函数用法实例

    这篇文章主要介绍了php中opendir函数用法,以实例形式详细讲述了opendir函数打开目录的用法及相关的注意事项,具有一定的参考借鉴价值,需要的朋友可以参考下 本文实例分析了php中opendi ...

  4. php中setcookie函数用法详解(转)

    php中setcookie函数用法详解:        php手册中对setcookie函数讲解的不是很清楚,下面是我做的一些整理,欢迎提出意见.        语法:        bool set ...

  5. 【313】python 中 print 函数用法总结

    参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...

  6. [转载]Tensorflow中reduction_indices 的用法

    Tensorflow中reduction_indices 的用法 默认时None 压缩成一维

  7. PHP中is_*() 函数用法

    PHP中is_*() 函数用法 is_a - 如果对象属于该类或该类是此对象的父类则返回 TRUE is_array - 检测变量是否是数组 is_bool - 检测变量是否是布尔型 is_calla ...

  8. PHP中 spl_autoload_register() 函数用法

    这篇文章主要介绍了PHP中spl_autoload_register()函数用法,结合实例形式分析了__autoload函数及spl_autoload_register函数的相关使用技巧,需要的朋友可 ...

  9. PHP中spl_autoload_register()函数用法实例详解

    本文实例分析了PHP中spl_autoload_register()函数用法.分享给大家供大家参考,具体如下: 在了解这个函数之前先来看另一个函数:__autoload. 一.__autoload 这 ...

随机推荐

  1. jQuery - lable 取值、赋值

    取值 var val = $("#breakfastMonday").html(); 赋值 $("#breakfastMonday").html("v ...

  2. MySQL 8 服务器选项配置

    查看服务器使用的默认命令选项和系统变量: mysqld --help --verbose 查看服务器当前运行时使用的系统变量和状态变量: mysql> SHOW VARIABLES; mysql ...

  3. linux查看硬件、系统信息

    查看机器型号等 dmidecode 是一个读取电脑 DMI(桌面管理接口(Desktop Management Interface))表内容并且以人类可读的格式显示系统硬件信息的工具.这个表包含系统硬 ...

  4. openlayers编辑区域

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  5. matlab bitset的理解

    在阅读别人的matlab程序中,发现了这个bitset函数.于是查阅资料搞明白了大概意思,意思如下: B= bitset(A,pos,V) 将A以二进制来表示,并将第pos个位置, 设置为 V 的值, ...

  6. 如何实现 List 集合的线程安全

    在实际项目开发中,List 集合较为常用,相比于数组,其提供了更多的方法,便于遍历.搜索.添加与移除.常见的有 ArrayList.Vector等.   关于ArrayList 中所周知,ArrayL ...

  7. Android_SQLite简单的增删改查

    SQLite数据库,和其他的SQL数据库不同, 我们并不需要在手机上另外安装一个数据库软件,Android系统已经集成了这个数据库,我们无需像 使用其他数据库软件(Oracle,MSSQL,MySql ...

  8. tsocks代理git wget

    使用clash时, 命令行的wget和git操作可能没有被代理 安装tsocks: apt-get install tsocks 修改配置文件: vi /etc/tsocks.conf 找到: ser ...

  9. 实现Windows数据更新

    一.枚举 枚举是一组描述性的名称 定义一组有限的值,不能包含方法 对可能的值进行约束 .定义枚举类 public enum Gender { Male,Female } .使用枚举表示整数值 publ ...

  10. matplotlib数组转图片的一些坑

    最近用matplotlib遇到了一些坑,记录一下. 图片转数组 import matplotlib.pyplot as plt im_file='test_image.jpg' img=plt.imr ...