学习tf.sparse_to_dense函数(代码实现)
来源:互联网 发布:吉林大学网络教育电话 编辑:程序博客网 时间:2024/06/11 09:23
#当tf.sparse_to_dense的第一个参数是2阶的tensor时
import tensorflow as tf BATCH_SIZE=5 label=tf.expand_dims(tf.constant([1,3,5,7,9]),1)#真实标签 shape==>[5,1]index=tf.expand_dims(tf.range(0, BATCH_SIZE),1)#真实标签的索引 shape==>[5,1]concated = tf.concat([index, label],1) #将标签和索引tensor在第二个维度上连接起来,新的concated的shape==>[5,2]onehot_labels = tf.sparse_to_dense(concated, [BATCH_SIZE,10], 1.0, 0.0) # onehot_labels的shape==>[5,10]with tf.Session() as sess: onehot1=sess.run(onehot_labels) print (onehot1)
[[ 0. 1. 0. 0. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 1. 0. 0. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 1. 0. 0.]
[ 0. 0. 0. 0. 0. 0. 0. 0. 0. 1.]]
#当tf.sparse_to_dense的第一个参数是1阶的tensor时
import tensorflow as tf
BATCH_SIZE=5
index=tf.constant([1,3,5,7,9])#shape==>[5]
onehot_labels = tf.sparse_to_dense(index, [10], 1.0, 0.0) # onehot_labels的shape==>[10]
with tf.Session() as sess:
onehot2=sess.run(onehot_labels)
print (onehot2)
输出结果是:
[ 0. 1. 0. 1. 0. 1. 0. 1. 0. 1.]
#当tf.sparse_to_dense的第一个参数是0阶的tensor时
import tensorflow as tf
BATCH_SIZE=5
index=tf.constant(3)# shape==>0
onehot_labels = tf.sparse_to_dense(index, [10], 1.0, 0.0)# onehot_labels的shape==>[10]
with tf.Session() as sess:
onehot3=sess.run(onehot_labels)
print (onehot3)
输出结果是:
[ 0. 0. 0. 1. 0. 0. 0. 0. 0. 0.]
注:1.当一个tensor的shape==>[5,2],说明该tensor是二阶的(因为5,2一共两个数字)
此tensor的形式是这样的[ [ * , *],[ * , *],[* , * ],[* , * ],[* , *] ]
2.当一个tensor的shape==>[10],说明该tensor是1阶的(因为只有1个数字10)
此tensor的形式是这样的[ * , * , * , * , * , * , * , * , * , * ]
1 0
- 学习tf.sparse_to_dense函数(代码实现)
- tf.sparse_to_dense()
- tensorflow——tf.one_hot以及tf.sparse_to_dense函数
- 【TensorFlow】tf.sparse_to_dense的用法
- TensorFlow- tf.argmax 函数学习
- tensorflow学习笔记(二十六):构建TF代码
- TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法
- tf-idf及核心代码c++实现
- tf函数总结(一)
- TensorFlow 学习(一)—— tf.get_variable() vs tf.Variable(),tf.name_scope() vs tf.variable_scope()
- TF-IDF(java实现)
- Spark机器学习:TF-IDF实现原理
- 机器学习- TF-IDF源代码实现
- 应用TF-Slim快速实现迁移学习
- 应用TF-Slim快速实现迁移学习
- tf函数
- tensorflow学习笔记(三十):tf.gradients 与 tf.stop_gradient()
- ROS进二阶学习笔记(1) TF 学习笔记1:TF介绍 + tf工具
- lasync命令模块实用工具cmdutils
- iOS中如何清除缓存?
- JQueryAjax操作
- PHP和Python中的引用机制
- 如何设置覆铜与不同网络的间隙
- 学习tf.sparse_to_dense函数(代码实现)
- AngularJS中filter过滤器的用法详解
- 解决 genymotion 联通本机服务器(apache为例)
- 第五讲、文本编辑器(vim/vi)
- hdu 1558 Segment set 并查集 叉积判断线段是否相交
- 使用androidstudio导入eclipse,IntelliJ等开发的项目
- 查看apk签名,查看key签名,adb常用命令 查看包名
- string类中的find函数
- 安卓Android开发:listView+checkbox的简单实现