SparseTensor

TensorFlow使用三个dense tensor来表达一个sparse tensor:indicesvaluesdense_shape

假如我们有一个dense tensor:

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

那么用SparseTensor表达这个数据对应的三个dense tensor如下:

  • indices:[[0, 0], [1, 2]]

  • values:[1, 2]

  • dense_shape:[3, 4]

可以通过以下两种方法,将sparse tensor转化为dense tensor:

  • tf.sparse_to_dense(sparse_indices, output_shape, sparse_values, default_value=0, validate_indices=True, name=None)

  • tf.sparse_tensor_to_dense(sp_input, default_value=0, validate_indices=True, name=None)

SparseTensor(indices, values, dense_shape)

  • indices: 一个2D的 int64 Tensor,shape为(N, ndims),指定了sparse tensor中的索引, 例如: indices=[[1,3], [2,4]]说明,dense tensor中对应索引为[1,3], [2,4]位置的元素的值不为0.
  • values: 一个1D tensor,shape(N)用来指定索引处的值. For example, given indices=[[1,3], [2,4]], the parameter values=[18, 3.6] specifies that element [1,3] of the sparse tensor has a value of 18, and element [2,4] of the tensor has a value of 3.6.
  • dense_shape: 一个1D的int64 tensor,形状为ndims,指定dense tensor的形状.

相对应的有一个tf.sparse_placeholder,如果给这个sparse_placeholder喂数据呢?

sp = tf.sparse_placeholder(tf.int32)

with tf.Session() as sess:
  #就这么喂就可以了
  feed_dict = {sp:(indices, values, dense_shape)}

tensorflow中目前没有API提供denseTensor->SparseTensor转换

tf.sparse_tensor_to_dense(sp_input, default_value=0, validate_indices=True, name=None)

把一个SparseTensor转化为DenseTensor.

  • sp_input: 一个SparceTensor.
  • default_value:没有指定索引的对应的默认值.默认为0.
  • validate_indices: 布尔值.如果为True的话,将会检查sp_inputindiceslexicographic order和是否有重复.
  • name: 返回tensor的名字前缀.可选.

Reference:

https://segmentfault.com/a/1190000008793389

  • 3
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值