Python 尾随'\x00和x27;从tf.py_func返回numpy字符串数组时的张量字符
当从Python 尾随'\x00和x27;从tf.py_func返回numpy字符串数组时的张量字符,python,tensorflow,Python,Tensorflow,当从tf.py_func调用的函数返回的numpy字符串数组将具有固定的字符串长度和尾随'\x00'字符,而不是没有填充的“自然”可变字符串长度时 以下是一个例子: import tensorflow as tf import numpy as np def main(): def foo(x): a = np.asarray(['abc', 'd'], dtype=np.string_) return a with tf.Session()
tf.py_func
调用的函数返回的numpy字符串数组将具有固定的字符串长度和尾随'\x00'字符,而不是没有填充的“自然”可变字符串长度时
以下是一个例子:
import tensorflow as tf
import numpy as np
def main():
def foo(x):
a = np.asarray(['abc', 'd'], dtype=np.string_)
return a
with tf.Session() as sess:
f = tf.py_func(foo, [tf.constant(1)], (tf.string))
f = tf.Print(f, [f, tf.shape(f)])
actual = sess.run(f)
print actual
打印出:
[abc d\000\000][2]
我正在使用的一个小解决方案是:
f = tf.string_split(f, delimiter='\x00', skip_empty=True).values
这是TF问题还是我做错了什么?返回一个列表而不是数据列似乎有效:
def foo(x):
a = [['abc', 'd']]
return a
返回列表而不是ndarray似乎有效:
def foo(x):
a = [['abc', 'd']]
return a
你应该改变
a = np.asarray(['abc', 'd'], dtype=np.string_)
到
你应该改变
a = np.asarray(['abc', 'd'], dtype=np.string_)
到
解释你的答案会很好。