Tensorflow 批处理:将结果保留为字符串 [英] Tensorflow batch: keep result as strings
问题描述
这个简单的程序
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 个字符串 b'string'
2串
3 ['str', 'ng']
4 ['str', 'ng']
错误:张量流:QueueRunner 中的异常:会话已关闭.
print(5, output.split('i'))
类型错误:需要一个类似字节的对象,而不是str"
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'
如果输入是字符串列表,为什么结果不是字符串列表?
Why isn't the result a list of strings, if the input is?
好的,@jdehesa 解释 为什么,但不是如何修复"它.我可以将 bytes.decode() 应用于会话的结果:
OK, @jdehesa explained WHY, but not how to 'fix' it. I can apply bytes.decode() to the results of session:
output, = map(bytes.decode, sess.run(batch))
并且存在 tf.map_fn() 应该对张量执行相同的操作.唯一的问题是我如何在我的场景中使用它?
And there exists tf.map_fn() that should do the same on tensors. The only question is how I can use this in my scenario?
PS:实际上,错误信息也令人费解.问题是我们提供了一个 bytes 对象,而不是一个字符串.但 TypeError 表明恰恰相反.
PS: actually, the error message is puzzling, too. The problem is that we provide a bytes object, not a string. But the TypeError suggests exactly the opposite.
PPS:解释了错误消息,感谢@jdehesa:它是关于 split() 的参数,而不是对象.output.split(b'i')
效果很好!
PPS: the error message explained, thanks to @jdehesa: it was about the split()'s parameter, not the object. output.split(b'i')
works well!
推荐答案
问题在于 output
是一个 bytes
对象,因为 TensorFlow tf.string
张量确实由 bytes
组成.但是随后您尝试将 split
与 str
分隔符一起使用,这就是它抱怨的原因.试试:
The problem is that output
is a bytes
object, because TensorFlow tf.string
tensors are indeed made of bytes
. But then you are trying to use split
with a str
separator, and that is why it complains. Try:
output.split(b'i')
或:
output.decode().split('i')
这篇关于Tensorflow 批处理:将结果保留为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!