Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (19.9k points)

Having a Tensor of strings of numbers (like "32", "45" and so on), how could I convert it to a tensor that has a symbol repeated as much times as the number indicates.

For instance, if I have a Tensor ["2", "3", "0", "1"], I would like to obtain something like ["aa", "aaa", "", "a"].

I have obtained it using numpy, but now I'm trying to do it in TensorFlow directly because I don't have the session started, so I cannot look for the variable value.

I share here a snippet of the code

import tensorflow as tf

a = tf.Variable(["2", "3", "0", "1"], dtype=tf.dtypes.string)

res = tf.strings.regex_replace(a, "([0-9]+)", r"a" * int("\\1"))

with tf.Session() as sess:

sess.run(tf.global_variables_initializer())

    print(sess.run(res)) # It should show ["aa", "aaa", "", "a"]

But int("\1") doesn't return the number, but a ValueError:

ValueError: invalid literal for int() with base 10: '\1'

1 Answer

0 votes
by (25.1k points)
edited by

You can't do what you wish to do using regular expressions in tensorflow. You can do it like this:

import tensorflow as tf

def repeat_symbol(nums, symbol):

    nums = tf.convert_to_tensor(nums)

    symbol = tf.convert_to_tensor(symbol)

    mask = tf.sequence_mask(nums)

    symbol_rep = tf.gather(tf.stack(["", symbol]), tf.cast(mask, tf.int32))

    return tf.strings.reduce_join(symbol_rep, axis=-1)

with tf.Graph().as_default(), tf.Session() as sess:

    a = tf.constant(["2", "3", "0", "1"], dtype=tf.string)

    a_nums = tf.strings.to_number(a, out_type=tf.int32)

    result = repeat_symbol(a_nums, "a")

    print(sess.run(result))

To know more about this you can have a look at the following video:-

Related questions

0 votes
2 answers
asked Sep 12, 2019 in Python by Sammy (47.6k points)
+1 vote
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...