有一个由数字串组成的张量(比如“32”、“45”等等),我怎么能把它转换成一个张量,这个张量有一个符号重复的次数和数字所表示的次数一样多。你知道吗
例如,如果我有一个张量[“2”,“3”,“0”,“1”],我想得到像[“aa”,“aaa”,“a”]。你知道吗
我已经使用numpy获得了它,但是现在我正在尝试直接在TensorFlow中完成它,因为我没有启动会话,所以我无法查找变量值。你知道吗
我在这里分享一段代码
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"]
但是int(“\1”)不返回数字,而是返回ValueError:
ValueError:基为10的int()的文本无效:'\1'
我不认为用TensorFlow中的正则表达式可以做到这一点。有一种方法可以做到:
相关问题 更多 >
编程相关推荐