简体   繁体   English

如何在 Tensorflow 中切片 RaggedTensor

[英]How to slice a RaggedTensor in Tensorflow

I have a RaggedTensor that I'm trying to slice:我有一个RaggedTensor ,我正在尝试切片:

batch_size_t = 4
ragged = tf.ragged.constant([[3, 1, 4, 1], [1, 2], [5, 9, 2], [6]])
tf.slice(ragged, [0, 0], [batch_size_t, 3])

However tf.slice does not appear to like ragged tensors:然而tf.slice似乎不喜欢tf.slice张量:

---------------------------------------------------------------------------
_FallbackException                        Traceback (most recent call last)
/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/ops/gen_array_ops.py in _slice(input, begin, size, name)
   8438         _ctx._context_handle, tld.device_name, "Slice", name,
-> 8439         tld.op_callbacks, input, begin, size)
   8440       return _result

...

/anaconda3/envs/ml/lib/python3.6/site-packages/tensorflow_core/python/framework/constant_op.py in convert_to_eager_tensor(value, ctx, dtype)
     94       dtype = dtypes.as_dtype(dtype).as_datatype_enum
     95   ctx.ensure_initialized()
---> 96   return ops.EagerTensor(value, ctx.device_name, dtype)
     97 
     98 

ValueError: TypeError: object of type 'RaggedTensor' has no len()

Is there a way to slice ragged tensors so I can truncate the length of inner dimensions?有没有办法对参差不齐的张量进行切片,以便我可以截断内部尺寸的长度?

If you want a simple contiguous slice of your ragged tensor you can simply use standard indexing,如果您想要一个简单连续的不规则张量切片,您可以简单地使用标准索引,

batch_size_t = 4
ragged = tf.ragged.constant([[3, 1, 4, 1], [1, 2], [5, 9, 2], [6]])
sliced_ragged = ragged[:batch_size_t,:3]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM