gpt4 book ai didi

python - Tensorflow 批处理 : keep result as strings

转载 作者:太空宇宙 更新时间:2023-11-04 02:13:12 26 4
gpt4 key购买 nike

这个简单的程序

import tensorflow as tf

input = 'string'
batch = tf.train.batch([tf.constant(input)], batch_size=1)
with tf.Session() as sess:
tf.train.start_queue_runners()
output, = sess.run(batch)

print(1, input, output)
print(2, str(output, 'utf-8'))
print(3, input.split('i'))
print(4, str(output, 'utf-8').split('i'))
print(5, output.split('i'))

打印

1 string b'string'
2 string
3 ['str', 'ng']
4 ['str', 'ng']
ERROR:tensorflow:Exception in QueueRunner: Session has been closed.
      print(5, output.split('i'))
TypeError: a bytes-like object is required, not 'str'

如果输入是,为什么结果不是字符串列表?

好的,@jdehesa explained 为什么,但不是如何“修复”它。我可以将 bytes.decode() 应用于 session 的结果:

output, = map(bytes.decode, sess.run(batch))

并且存在 tf.map_fn() 应该对张量执行相同的操作。唯一的问题是如何在我的场景中使用它?


PS:其实报错信息也很费解。问题是我们提供了一个字节对象,而不是一个字符串。但是 TypeError 表明恰恰相反。

PPS:感谢@jdehesa 解释了错误消息:这是关于 split() 的参数,而不是对象。 output.split(b'i') 运行良好!

最佳答案

问题是 output 是一个 bytes 对象,因为 TensorFlow tf.string 张量确实是由 bytes。但是随后您尝试将 splitstr 分隔符一起使用,这就是它提示的原因。尝试:

output.split(b'i')

或:

output.decode().split('i')

关于python - Tensorflow 批处理 : keep result as strings,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53278261/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com