import tensorflow as tf
import numpy as np
indices = np.array([[0, 0], [1, 1], [2, 2], [3, 4]], dtype=np.int32)
values = np.array([1, 2, 3, 4], dtype=np.int32)
shape = np.array([5, 5], dtype=np.int32)
x = tf.SparseTensor(values=values,indices=indices,dense_shape=shape)
with tf.Session() as sess:
result = sess.run(x)
print(result)
result_value = tf.sparse_tensor_to_dense(result)
print('value:\n', sess.run(result_value))
Sparsetensor(indices=[[0, 0], [1, 2]], values=[1, 2], dense_shape=[3, 4])
dense tensor:
[[1, 0, 0, 0]
[0, 0, 2, 0]
[0, 0, 0, 0]]
tensorflow 使用三个dense tensor来表达一个sparse tensor:indices、value、dense_shape
sparse_to_dense()和sparse_tensor_to_dense()的用法
tf.SparseTensor和tf.sparse_tensor_to_dense