tf.sparse_to_dense (
sparse_indices , #sparse_indices[i] 包含完整索引,这是 sparse_values[i] 将放置的位置.
output_shape ,
sparse_values , #如果是标量,则所有稀疏索引都设置为该单个值.
default_value = 0 , #所有其他值的 dense 都设置为 default_value.
validate_indices = True , #如果为 True,则检查索引以确保按照词典顺序排序并且没有重复.
name = None
)
按英文的意思:将稀疏表示形式转换为稠密张量。构建一个 dense 形状 output_shape 的数组。
# 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]
制作one-hot标签
reference:https://blog.csdn.net/qq_36411093/article/details/85336323
或者https://blog.csdn.net/mao_xiao_feng/article/details/53365889
import tensorflow as tf
import numpy
indices = tf.reshape(tf.range(0, 10 ,1), [10, 1])
labels=tf.expand_dims(tf.constant([0,2,3,6,7,9,1,3,5,4]),1)
print(indices)
print(labels)
onehot = tf.sparse_to_dense(
tf.concat(values=[indices, labels], axis=1),
[10, 10], 1.0, 0.0)
with tf.Session() as sess:
a = sess.run(onehot)
print(a)