官网默认定义如下:
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. ubuntu18.04误删apt-get命令恢复总结

    1.背景 由于使用aptitude命令替换了apt-get命令后感到后悔,想要恢复apt-get命令,特此总结以下踩过的坑 aptitude和apt-get的区别:https://www.cnblog ...

  2. 一个C语言程序是由( )组成?

    A) 一个主程序和若干子程序组成 B)一个或多个函数组成 C) 若干过程组成 D) 若干子程序组成 正确答案 B 解析 [解析] 一个C源程序是由一个main函数和若干个其他函数组成的.函数是C程序的 ...

  3. 纪中21日c组T1 1575. 二叉树

    1575. 二叉树 (File IO): input:tree.in output:tree.out 时间限制: 1000 ms  空间限制: 262144 KB  具体限制   Goto Probl ...

  4. 剑指offer-面试题49-丑数-空间换时间

    /* 题目: 求从1开始的第n个丑数. */ /* 思路: 按顺序列出各个丑数. */ #include<iostream> #include<cstring> #includ ...

  5. html页面提交JSON,ASP页面接收并打印

    jsonSubmit.html 1)能过 javascript函数驱动请求页 <!DOCTYPE html> <html> <head> <title> ...

  6. C++文件读写demo

    一.常用文件打开方式 二.读写文件步骤(文本文件) 1.写文件步骤 1)#include <fstream> //包含头文件 2)ofstream ofs; //创建流对象 3)ofs.o ...

  7. git签名设置

    作用:只区分不同开发人员的身份 一.项目级别/仓库级别:仅在当前本地库范围内有效 签名设置用户名(UserName)和邮箱(User@email),邮箱可以是任意邮箱(无效邮箱也可以) git con ...

  8. 在IE8的基础上安装IE11

    安装包下载 链接:https://pan.baidu.com/s/1WAuyMg_SrfVa0wLjoop5Jw   提取码:nh1r 安装方式 先运行 - EIE11_ZH-CN_WOL_WIN76 ...

  9. learn to rank 模型概述

    模型总体描述: https://zhuanlan.zhihu.com/p/26539920 LambdaMART中Lambda计算以及RegressionTree训练: https://blog.cs ...

  10. Android ListView的批量处理(多选/反选/删除)

    在Android开发中经常遇到使用ListView的情况,有时候需要的不仅仅是列表显示,还有长按列表进行多选,并且批量删除的情况,在这里记录一下自己的所学. 先上效果图: 几个需要用到的核心方法: / ...