TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法

原文地址:http://blog.csdn.net/mao_xiao_feng/article/details/53365889

TensorFlow是一个很坑的东西,在没有session运行的时候,所有数据都看不到结果,很难去print

而且TF还没有中文的API手册,很多东西很难体会

在这里记录一下比较难理解的几个方法的用法,以便后面用到

tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value, name=None)

除去name参数用以指定该操作的name,与方法有关的一共四个参数

第一个参数sparse_indices:稀疏矩阵中那些个别元素对应的索引值。

     有三种情况:

     sparse_indices是个数,那么它只能指定一维矩阵的某一个元素

     sparse_indices是个向量,那么它可以指定一维矩阵的多个元素

     sparse_indices是个矩阵,那么它可以指定二维矩阵的多个元素

第二个参数output_shape:输出的稀疏矩阵的shape

第三个参数sparse_values:个别元素的值。

     分为两种情况:

     sparse_values是个数:所有索引指定的位置都用这个数

     sparse_values是个向量:输出矩阵的某一行向量里某一行对应的数(所以这里向量的长度应该和输出矩阵的行数对应,不然报错)

第四个参数default_value:未指定元素的默认值,一般如果是稀疏矩阵的话就是0了


举一个例子:

在mnist里面有一个把数字标签转化成onehot标签的操作,所谓onehot标签就是:

如果标签是6那么对应onehot就是[ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]

如果标签是1那么对应onehot就是[ 0.  1.  0.  0.  0.  0.  0.  0.  0.  0.]

如果标签是0那么对应onehot就是[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]

就是把标签变为适用于神经网络输出的形式。

[python]  view plain  copy
  1. BATCHSIZE=6  
  2. label=tf.expand_dims(tf.constant([0,2,3,6,7,9]),1)  
假设一个batch有6个样本,每个样本的label是0,2,3,6,7,9
[python]  view plain  copy
  1. index=tf.expand_dims(tf.range(0, BATCHSIZE),1)  
生成一个index表明一个batch里面每个样本对应的序号

(至于这里为什么要调用tf.expand_dims我在上一篇博客已经解释过了,链接:http://blog.csdn.net/mao_xiao_feng/article/details/53366163)

[python]  view plain  copy
  1. concated = tf.concat(1, [index, label])  
最后把他们两个矩阵进行连接,连接以后的矩阵是这样的
[python]  view plain  copy
  1. [[0 0]  
  2.  [1 2]  
  3.  [2 3]  
  4.  [3 6]  
  5.  [4 7]  
  6.  [5 9]]  
[python]  view plain  copy
  1. onehot_labels = tf.sparse_to_dense(concated, tf.pack([BATCHSIZE,10]), 1.00.0)  
最后一步,调用tf.sparse_to_dense输出一个onehot标签的矩阵,输出的shape就是行数为BATCHSIZE,列数为10的矩阵,指定元素值为1.0,其余元素值为0.0


程序源码:

[python]  view plain  copy
  1. import tensorflow as tf  
  2. import numpy  
  3.   
  4.   
  5. BATCHSIZE=6  
  6. label=tf.expand_dims(tf.constant([0,2,3,6,7,9]),1)  
  7. index=tf.expand_dims(tf.range(0, BATCHSIZE),1)  
  8. #use a matrix  
  9. concated = tf.concat(1, [index, label])  
  10. onehot_labels = tf.sparse_to_dense(concated, tf.pack([BATCHSIZE,10]), 1.00.0)  
  11.   
  12. #use a vector  
  13. concated2=tf.constant([1,3,4])  
  14. #onehot_labels2 = tf.sparse_to_dense(concated2, tf.pack([BATCHSIZE,10]), 1.0, 0.0)#cant use ,because output_shape is not a vector  
  15. onehot_labels2 = tf.sparse_to_dense(concated2, tf.pack([10]), 1.00.0)#can use  
  16.   
  17. #use a scalar  
  18. concated3=tf.constant(5)  
  19. onehot_labels3 = tf.sparse_to_dense(concated3, tf.pack([10]), 1.00.0)  
  20.   
  21. with tf.Session() as sess:  
  22.     result1=sess.run(onehot_labels)  
  23.     result2 = sess.run(onehot_labels2)  
  24.     result3 = sess.run(onehot_labels3)  
  25.     print ("This is result1:")  
  26.     print (result1)  
  27.     print ("This is result2:")  
  28.     print (result2)  
  29.     print ("This is result3:")  
  30.     print (result3)  

输出结果:

[python]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. This is result1:  
  2. [[ 1.  0.  0.  0.  0.  0.  0.  0.  0.  0.]  
  3.  [ 0.  0.  1.  0.  0.  0.  0.  0.  0.  0.]  
  4.  [ 0.  0.  0.  1.  0.  0.  0.  0.  0.  0.]  
  5.  [ 0.  0.  0.  0.  0.  0.  1.  0.  0.  0.]  
  6.  [ 0.  0.  0.  0.  0.  0.  0.  1.  0.  0.]  
  7.  [ 0.  0.  0.  0.  0.  0.  0.  0.  0.  1.]]  
  8. This is result2:  
  9. 0.  1.  0.  1.  1.  0.  0.  0.  0.  0.]  
  10. This is result3:  
  11. 0.  0.  0.  0.  0.  1.  0.  0.  0.  0.]  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值