Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 尾随'\x00和x27;从tf.py_func返回numpy字符串数组时的张量字符_Python_Tensorflow - Fatal编程技术网

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_)


解释你的答案会很好。