tf中的tf.sparse_to_dense的用法
2018-01-09 23:46
369 查看
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] viewplain copyBATCHSIZE=6 label=tf.expand_dims(tf.constant([0,2,3,6,7,9]),1) 假设一个batch有6个样本,每个样本的label是0,2,3,6,7,9[python] viewplain copyindex=tf.expand_dims(tf.range(0, BATCHSIZE),1) 生成一个index表明一个batch里面每个样本对应的序号(至于这里为什么要调用tf.expand_dims我在上一篇博客已经解释过了,链接:http://blog.csdn.net/mao_xiao_feng/article/details/53366163)[python] viewplain copyconcated = tf.concat(1, [index, label]) 最后把他们两个矩阵进行连接,连接以后的矩阵是这样的[python] viewplain copy[[0 0] [1 2] [2 3] [3 6] [4 7] [5 9]] [python] viewplain copyonehot_labels = tf.sparse_to_dense(concated, tf.pack([BATCHSIZE,10]), 1.0, 0.0) 最后一步,调用tf.sparse_to_dense输出一个onehot标签的矩阵,输出的shape就是行数为BATCHSIZE,列数为10的矩阵,指定元素值为1.0,其余元素值为0.0程序源码:[python] viewplain copyimport tensorflow as tf import numpy BATCHSIZE=6 label=tf.expand_dims(tf.constant([0,2,3,6,7,9]),1) index=tf.expand_dims(tf.range(0, BATCHSIZE),1) #use a matrix concated = tf.concat(1, [index, label]) onehot_labels = tf.sparse_to_dense(concated, tf.pack([BATCHSIZE,10]), 1.0, 0.0) #use a vector concated2=tf.constant([1,3,4]) #onehot_labels2 = tf.sparse_to_dense(concated2, tf.pack([BATCHSIZE,10]), 1.0, 0.0)#cant use ,because output_shape is not a vector onehot_labels2 = tf.sparse_to_dense(concated2, tf.pack([10]), 1.0, 0.0)#can use #use a scalar concated3=tf.constant(5) onehot_labels3 = tf.sparse_to_dense(concated3, tf.pack([10]), 1.0, 0.0) with tf.Session() as sess: result1=sess.run(onehot_labels) result2 = sess.run(onehot_labels2) result3 = sess.run(onehot_labels3) print ("This is result1:") print (result1) print ("This is result2:") print (result2) print ("This is result3:") print (result3) 输出结果:[python] viewplain copyThis is result1: [[ 1. 0. 0. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 1. 0. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 1. 0. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.] [ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]] This is result2: [ 0. 1. 0. 1. 1. 0. 0. 0. 0. 0.] This is result3: [ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
相关文章推荐
- TensorFlow入门基础知识(四)tf.sparse_to_dense的用法
- TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法
- 【TensorFlow】tf.sparse_to_dense的用法
- tf.sparse_to_dense()
- tf.nn.sparse_softmax_cross_entropy_with_logits()函数的用法
- tensorflow——tf.one_hot以及tf.sparse_to_dense函数
- tf.convert_to_tensor的用法
- tf.nn.sparse_softmax_cross_entropy_with_logits的用法
- 学习tf.sparse_to_dense函数(代码实现)
- tf.image.crop_to_bounding_box的用法
- tf.add_to_collection,tf.get_collection和tf.add_n的用法
- Tensorflow 的tf.one_hot()功能:dense to one hot
- tf.add_to_collection、tf.get_collection、tf.add_n用法浅析
- opencv中scr.copyTo()的用法介绍
- LINQ To SQL中IN的用法
- tf.nn.rnn_cell.BasicRNNCell函数的用法
- tf.truncated_normal与tf.random_normal的详细用法
- map flatmap mappartition flatMapToPair四种用法区别
- javascript稀疏数组(sparse array)和密集数组用法分析
- SQL2005中row_number()、rank()、dense_rank()、ntile()函数的用法