先给出tf.one_hot的用例:(例子转自https://www.jianshu.com/p/c5b4ec39713b)

 import tensorflow as tf
var0 = tf.one_hot(indices=[1, 2, 3], depth=3, axis=0)
var1 = tf.one_hot(indices=[1, 2, 3], depth=4, axis=0)
var2 = tf.one_hot(indices=[1, 2, 3], depth=4, axis=1)
# axis=1 按行排
var3 = tf.one_hot(indices=[1, 2, 3], depth=4, axis=-1)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
a0 = sess.run(var0)
a1 = sess.run(var1)
a2 = sess.run(var2)
a3 = sess.run(var3)
print("var0(axis=0 depth=3)\n", a0)
print("var1(axis=0 depth=4P)\n", a1)
print("var2(axis=1)\n", a2)
print("var3(axis=-1)\n", a3)

运行结果如下:

 2018-08-01 18:06:39.012597: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
var0(axis=0 depth=3)
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]]
var1(axis=0 depth=4P)
[[0. 0. 0.]
[1. 0. 0.]
[0. 1. 0.]
[0. 0. 1.]]
var2(axis=1)
[[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]]
var3(axis=-1)
[[0. 1. 0. 0.]
[0. 0. 1. 0.]
[0. 0. 0. 1.]] 进程已结束,退出代码0

这里需要一个4*3的矩阵,记为var1,则需要var1[2,1]为0的one_hot矩阵。如下代码可实现。

 import tensorflow as tf
q1 = 2
q2 = 1
depth = 4
kuan = 3
var0 = tf.one_hot(indices=[q2], depth=kuan, on_value=q1, off_value=depth, axis=1)
var1 = tf.one_hot(indices=[depth, q1, depth], depth=depth, axis=0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
a0 = sess.run(var0)
a1 = sess.run(var1)
print("var0(axis=0 depth=3)\n", a0)
print("var1(axis=0 depth=4P)\n", a1)

运行结果:

 2018-08-01 18:08:01.640973: I T:\src\github\tensorflow\tensorflow\core\platform\cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2
var0(axis=0 depth=3)
[[4 2 4]]
var1(axis=0 depth=4P)
[[0. 0. 0.]
[0. 0. 0.]
[0. 1. 0.]
[0. 0. 0.]] 进程已结束,退出代码0

最新文章

  1. Word 2010 发布博文测试
  2. php调用c/c++的一种方式
  3. HIVE几种数据导入方式
  4. HTML Meta标签知多少
  5. 利用js的for循环实现一个简单的“九九乘法表”
  6. STM32F0xx_DAC输出电压配置详细过程
  7. 《JavaScript高级程序设计》第6章 面向对象程序设计
  8. Windows 7中使用Eclipse 使用CDT and WinGW 开发C/C++(转载)
  9. 用原生JavaScript实现图片瀑布流的浏览效果
  10. mobile端
  11. Handler不同线程间的通信
  12. 大手册(书籍)排版利器-XML自动排版生成工具
  13. [52ABP实战课程系列]Docker&Ubuntu从入门到实战开课啦~
  14. ●BZOJ 2500 幸福的道路
  15. mysql史上最全的学习资料
  16. Wcf传递的参数实际不为空,但是接收时显示为空。
  17. Ubuntu下面MySQL的参数文件my.cnf浅析
  18. day18_雷神_django第一天
  19. BZOJ1041 HAOI2008圆上的整点(数论)
  20. 【JMeter】【性能测试】配置元件

热门文章

  1. Java工具类之:包装类
  2. python入门005
  3. Jmeter系列(43)- 详解 Jmeter 图形化 HTML 压测报告之 Charts 模块
  4. 数据可视化之powerBI入门(二)体验PowerBI:零基础分分钟生成一份交互报表
  5. 爬虫前篇 /https协议原理剖析
  6. kafka零拷贝
  7. 【Python学习笔记三】一个简单的python爬虫
  8. javascript : 递归遍历数组
  9. 设计模式:chain of responsibility模式
  10. Mysql5.7前后修改用户密码变化