tf.sparse_to_dense用途

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)

在这里插入图片描述
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值