How To Remove Consecutive Duplicates In Tensorflow?
For example, input a 1-d tensor:
l_in = [1,1,2,2,3,4,5,5,1,3,5]
I want to remove the consecutive duplicates, which means the output should be:
l_out = [1,2,3,4,5,1,3,5]
tf.unique function only returns unique elements, indicating that the last three elements will also be eliminated. The output of
[1,2,3,4,5], [0,0,1,1,2,3,4,4,0,2,4] = tf.unique(l_in)
where the second item is the corresponding IDs.
Is there any way to only remove consecutive duplicates while preserving non-duplicated and non-unique elements?
For 1D tensors, using array rotation/shifting:
import tensorflow as tf l_in = tf.constant([1,1,2,2,3,4,5,5,1,3,5]) l_left_shift = tf.concat((l_in[1:], ), axis=0) mask_left_shift = tf.not_equal(l_in - l_left_shift, 0) mask = tf.concat(([True], mask_left_shift[:-1]), axis=0) l_out = tf.boolean_mask(l_in, mask) with tf.Session() as sess: print(sess.run(l_out)) # [1 2 3 4 5 1 3 5]
(i.e. the idea is to subtract each element with its right neighbor, then mask out the neighbor if the subtraction result is 0)
- → What are the pluses/minuses of different ways to configure GPIOs on the Beaglebone Black?
- → Django, code inside <script> tag doesn't work in a template
- → React - Django webpack config with dynamic 'output'
- → GAE Python app - Does URL matter for SEO?
- → Put a Rendered Django Template in Json along with some other items
- → session disappears when request is sent from fetch
- → Python Shopify API output formatted datetime string in django template
- → Shopify app: adding a new shipping address via webhook
- → Shopify + Python library: how to create new shipping address
- → shopify python api: how do add new assets to published theme?
- → Access 'HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT' with Python Shopify Module