tf.sparse_to_dense()
2017-10-25 15:05
555 查看
tf.sparse_to_dense
import tensorflow as tf
import numpy as np
SIZE=6
CLASS=10
label=np.random.randint(0,10,size=SIZE)
label=np.reshape(label,[SIZE,1])
index = np.reshape(np.arange(SIZE), [SIZE, 1])
#use a matrix
concated = tf.concat([index, label], 1)
onehot_labels = tf.sparse_to_dense(concated, [SIZE, CLASS], 1.0, 0.0)
#use a vector
concated2=tf.constant([1,3,4])
onehot_labels2 = tf.sparse_to_dense(concated2, [ CLASS], 1.0, 0.0)
#use a scalar
concated3=tf.constant(5)
onehot_labels3 = tf.sparse_to_dense(concated3, [ CLASS], 1.0, 0.0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
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)
import tensorflow as tf
import numpy as np
SIZE=6
CLASS=10
label=np.random.randint(0,10,size=SIZE)
label=np.reshape(label,[SIZE,1])
index = np.reshape(np.arange(SIZE), [SIZE, 1])
#use a matrix
concated = tf.concat([index, label], 1)
onehot_labels = tf.sparse_to_dense(concated, [SIZE, CLASS], 1.0, 0.0)
#use a vector
concated2=tf.constant([1,3,4])
onehot_labels2 = tf.sparse_to_dense(concated2, [ CLASS], 1.0, 0.0)
#use a scalar
concated3=tf.constant(5)
onehot_labels3 = tf.sparse_to_dense(concated3, [ CLASS], 1.0, 0.0)
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
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)
相关文章推荐
- TensorFlow入门基础知识(四)tf.sparse_to_dense的用法
- 【TensorFlow】tf.sparse_to_dense的用法
- tf中的tf.sparse_to_dense的用法
- TensorFlow 官方文档中文版解读之2——tf.sparse_to_dense的用法
- Tensorflow 的tf.one_hot()功能:dense to one hot
- tensorflow——tf.one_hot以及tf.sparse_to_dense函数
- 学习tf.sparse_to_dense函数(代码实现)
- Tensorflow 处理libsvm格式数据生成TFRecord (parse libsvm data to TFRecord)
- Unable to determine the workspace. You may be able to correct this by running 'tf workspaces /collec
- tensorflow 笔记10:tf.nn.sparse_softmax_cross_entropy_with_logits 函数
- Error Setting working folder: Could not rename properties.tf2 to properties.tf1 in recover.
- Tensorflow二分类处理dense或者sparse(文本分类)的输入数据
- tensorflow tf.layers.dense 实例
- tensorflow将训练好的模型freeze,即将权重固化到图里面,并使用该模型进行预测(tf.graph_util.convert_variables_to_constants函数)
- 如何利用tf.add_to_collection、tf.get_collection以及tf.add_n来简化正则项的计算
- the basic approach to read dataset(TFRecord) with iterator in Tensorflow
- [tensorflow] tf.nn.sparse_softmax_cross_entropy_with_logits的使用方法及常见报错
- 《Sparse and Redundant Representations:From Theory to Applications in Signal and Image Processing》序论
- Sparse Dictionaries (How to Make or Find them)
- linux tenserflow add traning picture dataset to TFRecord