tf.sparse_tensor_to_dense

代码:

import tensorflow as tf

def getSparseTensor():

    indices = [[0, 0], [1, 2]]
    values = [1, 2]
    shape = [3, 4]
    return indices, values, shape

if __name__ == "__main__":
    sparse_tensor = getSparseTensor()
    targets = tf.sparse_placeholder(tf.int32, name='targets')

    dense_tensor1 = tf.sparse_tensor_to_dense(targets, default_value=0)

    with tf.Session() as sess:
        print(sess.run(dense_tensor1, feed_dict={targets: sparse_tensor}))

输出:

[[1 0 0 0]
 [0 0 2 0]
 [0 0 0 0]]

发布了90 篇原创文章 · 获赞 9 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/wangwenjie1997/article/details/105617628