官网默认定义如下:
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. [Contract] Solidity 判断 mapping 值的存在

    比如 mapping(address => uint) tester,只需要判断 mapping 是否为默认值 0, tester[msg.sender] == 0 "You can ...

  2. 重写了下Ajax请求Webservice,顺便复习一下Javascript的闭包概念

    var AjaxRequest = function(){ //返回处理结果的回调函数 this.agentCallBack = {}; //javascript 调用domino代理的方法. thi ...

  3. javascript单词

    abstract n. 摘要,抽象的东西 adj. 抽象的,理论的 vt. 移除,摘要,偷 vi. 做摘要 do aux. 助动词(无词意) v. 干,做 if conj. 如果,是否,即使 n. 条 ...

  4. C#面向对象详解

    //封装就是将数据或函数等集合在一个个的单元中,我们称之为类,被封装的对象通常被称为抽象数据类型, //封装的意义在于保护或防止代码被我们无意中破坏, //封装既可以封装成员变量,又可以封装成员方法, ...

  5. fatal error LNK1169: one or more multiply defined symbols found

    在 Project/Setting/Link/General中的 Project Options: 加入 /FORCE:MULTIPLE即可")可以解决报错问题,但是这些问题全部变成了war ...

  6. 怎么解决Chrome浏览器“Failed to load resource: net::ERR_INSECURE_RESPONSE”错误?

    用科学方法安装的arcgisServer,需要修改系统时间,但是修改了系统时间,可能会出各种问题, office 不能正确验证,chrome 不能正常使用,访问网页, 如果直接输入本地地址进行访问的话 ...

  7. c++并发编程之进程创建(给那些想知道细节的人)

    关于多进程创建,此处只讲解一个函数fork(). 1.进程创建 先上代码: #include"iostream" #include<unistd.h> //unix标准 ...

  8. TChart-图表编辑器的测试

    最近不知怎么的,想研究一下图表.先上效果图: 功能代码: unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Class ...

  9. 安装MySQL被提示缺少msvcr120.dll,msvcp120.dll或提示0xc00007b错误

    踩坑经历: 我的电脑昨天重装了win10系统,在安装MySQL时被提示缺少msvcr120.dll,我去下载msvcr120.dll后放入System32中,又被提示缺少msvcp120.dll,然后 ...

  10. 你是个好人,但不是个好leader

    午餐时间,我翻看朋友圈,浏览到一条朋友A的最新动态,她刚创业半年多的公司因为资金周转不灵.项目搁浅.合伙人撤资等多方面原因,最终倒闭了. 朋友A有着多年的名企工作经验,985大学毕业,在学校时是学霸, ...