W3Cschool
恭喜您成為首批注冊(cè)用戶
獲得88經(jīng)驗(yàn)值獎(jiǎng)勵(lì)
sparse_to_dense ( sparse_indices , output_shape , sparse_values , default_value = 0 , validate_indices = True , name = None )
定義在:tensorflow/python/ops/sparse_ops.py.
請(qǐng)參閱指南:稀疏張量>轉(zhuǎn)變
將稀疏表示形式轉(zhuǎn)換為稠密張量.
構(gòu)建一個(gè) dense 形狀 output_shape 的數(shù)組,如下述所示:
# If sparse_indices is scalar
dense[i] = (i == sparse_indices ? sparse_values : default_value)
# If sparse_indices is a vector, then for each i
dense[sparse_indices[i]] = sparse_values[i]
# If sparse_indices is an n by d matrix, then for each i in [0, n)
dense[sparse_indices[i][0], ..., sparse_indices[i][d-1]] = sparse_values[i]
所有其他值的 dense 都設(shè)置為 default_value.如果 sparse_values 是標(biāo)量,則所有稀疏索引都設(shè)置為該單個(gè)值.
索引應(yīng)按字典順序排序,索引不得有任何重復(fù).如果 validate_indices 為 True,則在執(zhí)行期間檢查這些屬性.
函數(shù)參數(shù):
函數(shù)返回值:
tf.sparse_to_dense函數(shù)返回形狀為 output_shape 的密集 Tensor,它與 sparse_values 具有相同的類型.
除去name參數(shù)用以指定該操作的 name,與方法有關(guān)的一共四個(gè)參數(shù) :
第一個(gè)參數(shù) sparse_indices:稀疏矩陣中那些個(gè)別元素對(duì)應(yīng)的索引值。
BATCHSIZE=6
?label=tf.expand_dims(tf.constant([0,2,3,6,7,9]),1)
假設(shè)一個(gè) batch 有6個(gè)樣本,每個(gè)樣本的 label 是0,2,3,6,7,9
index=tf.expand_dims(tf.range(0, BATCHSIZE),1)生成一個(gè)index表明一個(gè)batch里面每個(gè)樣本對(duì)應(yīng)的序號(hào)
concated = tf.concat(1, [index, label])
最后把他們兩個(gè)矩陣進(jìn)行連接,連接以后的矩陣是這樣的
[[0 0][1 2]
[2 3]
[3 6]
[4 7]
[5 9]]
onehot_labels = tf.sparse_to_dense(concated, tf.pack([BATCHSIZE,10]), 1.0, 0.0)
最后一步,調(diào)用 tf.sparse_to_dense 輸出一個(gè) onehot 標(biāo)簽的矩陣,輸出的 shape 就是行數(shù)為 BATCHSIZE,列數(shù)為10的矩陣,指定元素值為1.0,其余元素值為0.0
程序源碼:
import 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)
輸出結(jié)果:
This 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.]
Copyright©2021 w3cschool編程獅|閩ICP備15016281號(hào)-3|閩公網(wǎng)安備35020302033924號(hào)
違法和不良信息舉報(bào)電話:173-0602-2364|舉報(bào)郵箱:jubao@eeedong.com
掃描二維碼
下載編程獅App
編程獅公眾號(hào)
聯(lián)系方式:
更多建議: