【TensorFlow】稀疏矢量
生活随笔
收集整理的這篇文章主要介紹了
【TensorFlow】稀疏矢量
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
- 官方Document: https://tensorflow.google.cn/api_guides/python/sparse_ops
- 開發測試環境:
- Win10
- Python 3.6.4
- tensorflow-gpu 1.6.0
SparseTensor與SparseTensorValue的理解
SparseTensor(indices, values, dense_shape)
稀疏矢量的表示
- indices shape為[N, ndims]的2-D int64矢量,用以指定非零元素的位置,比如indices=[[1,3], [2,4]]表示[1,3]和[2,4]位置的元素為非零元素。
- values shape為[N]的1-D矢量,對應indices所指位置的元素值
- dense_shape shape為[ndims]的1-D矢量,代表稀疏矩陣的shape
稀疏矢量的封裝并不直觀,可以通過稀疏矢量的方式構建矢量(sparse_to_dense)或者將稀疏矢量轉換成矢sparse_tensor_to_dense量的方式來感受一下:
def sparse_to_dense(sparse_indices,output_shape,sparse_values,default_value=0,validate_indices=True,name=None)- sparse_indices sparse_indices:稀疏矩陣中那些個別元素對應的索引值。
- sparse_indices是個數,那么它只能指定一維矩陣的某一個元素
- sparse_indices是個向量,那么它可以指定一維矩陣的多個元素
- sparse_indices是個矩陣,那么它可以指定二維矩陣的多個元素
- output_shape 輸出的稀疏矩陣的shape
- sparse_value 個別元素的值
- sparse_values是個數:所有索引指定的位置都用這個數
- sparse_values是個向量:輸出矩陣的某一行向量里某一行對應的數(所以這里向量的長度應該和輸出矩陣的行數對應,不然報錯)
- default_value:未指定元素的默認值,一般如果是稀疏矩陣的話就是0了
實例展示
import tensorflow as tf import numpy BATCHSIZE=6label=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([index, label], 1) # [[0, 0], [0, 2], [0, 3], [0, 6], [0, 7], [0, 9]] (6,2) onehot_labels = tf.sparse_to_dense(concated, [BATCHSIZE,10], 1.0, 0.0)# use a vector sparse_indices2=tf.constant([1,3,4]) onehot_labels2 = tf.sparse_to_dense(sparse_indices2, [10], 1.0, 0.0)#can use# use a scalar sparse_indices3=tf.constant(5) onehot_labels3 = tf.sparse_to_dense(sparse_indices3, [10], 1.0, 0.0)sparse_tensor_00 = tf.SparseTensor(indices=[[0,0,0], [1,1,2]], values=[4, 6], dense_shape=[2,2,3]) dense_tensor_00 = tf.sparse_tensor_to_dense(sparse_tensor_00)with tf.Session(config=config) as sess:result1=sess.run(onehot_labels)result2 = sess.run(onehot_labels2)result3 = sess.run(onehot_labels3)result4 = sess.run(dense_tensor_00)print ("This is result1:")print (result1)print ("This is result2:")print (result2)print ("This is result3:")print (result3)print ("This is result4:")print (result4)輸出結果如下
[[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.] This is result4: [[[4 0 0][0 0 0]][[0 0 0][0 0 6]]]區別
兩者的區別可以通過應用來說起
If you would like to define the tensor outside the graph, e.g. define the sparse tensor for later data feed, use SparseTensorValue. In contrast, if the sparse tensor is defined in graph, use SparseTensor
在graph定義sparse_placeholder,在feed中需要使用SparseTensorValue
x_sp = tf.sparse_placeholder(dtype=tf.float32) W = tf.Variable(tf.random_normal([6, 6])) y = tf.sparse_tensor_dense_matmul(sp_a=x_sp, b=W)init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) sess.run(init)stv = tf.SparseTensorValue(indices=[[0, 0], [1, 2]], values=[1.1, 1.2], dense_shape=[2,6]) result = sess.run(y,feed_dict={x_sp:stv})print(result)在graph中做定義需要使用SparseTensor
indices_i = tf.placeholder(dtype=tf.int64, shape=[2, 2]) values_i = tf.placeholder(dtype=tf.float32, shape=[2]) dense_shape_i = tf.placeholder(dtype=tf.int64, shape=[2]) st = tf.SparseTensor(indices=indices_i, values=values_i, dense_shape=dense_shape_i)W = tf.Variable(tf.random_normal([6, 6])) y = tf.sparse_tensor_dense_matmul(sp_a=st, b=W)init = tf.global_variables_initializer() gpu_options = tf.GPUOptions(per_process_gpu_memory_fraction=0.2) sess = tf.Session(config=tf.ConfigProto(gpu_options=gpu_options)) sess.run(init)result = sess.run(y,feed_dict={indices_i:[[0, 0], [1, 2]], values_i:[1.1, 1.2], dense_shape_i:[2,6]})print(result)在feed中應用SparseTensor,需要使用運算
x = tf.sparse_placeholder(tf.float32) y = tf.sparse_reduce_sum(x)config = tf.ConfigProto() config.gpu_options.allow_growth = True config.allow_soft_placement = True with tf.Session(config=config) as sess:indices = np.array([[3, 2, 0], [4, 5, 1]], dtype=np.int64)values = np.array([1.0, 2.0], dtype=np.float32)shape = np.array([7, 9, 2], dtype=np.int64)print(sess.run(y, feed_dict={x: tf.SparseTensorValue(indices, values, shape)})) # Will succeed.print(sess.run(y, feed_dict={x: (indices, values, shape)})) # Will succeed.sp = tf.SparseTensor(indices=indices, values=values, dense_shape=shape)sp_value = sp.eval(session=sess)print(sp_value)print(sess.run(y, feed_dict={x: sp_value})) # Will succeed.總結
以上是生活随笔為你收集整理的【TensorFlow】稀疏矢量的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 系统设计类问题
- 下一篇: 【使用注意】特殊中括号[]的特殊json