为什么TensorFlow返回的字符串在Python 3中显示为'b'前缀?

问题描述:

我刚刚在RPi 3上完成安装Tensorflow 1.3。验证安装(根据此https://www.tensorflow.org/install/install_sources)以某种方式显示小写“b”。看到这些代码:为什么TensorFlow返回的字符串在Python 3中显示为'b'前缀?

[email protected]:/home/pi# python 
Python 3.4.2 (default, Oct 19 2014, 13:31:11) 
[GCC 4.9.1] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tensorflow as tf 
>>> hello = tf.constant('Hello, TensorFlow!') 
>>> sess = tf.Session() 
>>> print(sess.run(hello)) 
b'Hello, TensorFlow!' 
>>> 

不,这不是一个错误。你的安装非常好,这是正常的行为。

b之前的字符串是由于Tensorflow内部表示形式的字符串。

Tensorflow表示字符串作为字节数组,从而当你“提取物”它们(从曲线图,从而tensorflow的内部表示,以蟒环境)使用sess.run(hello)你得到一个bytes类型,而不是一个str类型。而如果你做

import tensorflow as tf 
hello = tf.constant('Hello, TensorFlow!') 
sess = tf.Session() 
print(type(sess.run(hello))) 

结果<class 'bytes'>

您可以在此使用type功能验证

print(type('Hello, TensorFlow!')) 

结果<class 'str'>

+0

我很高兴,谢谢! – user1801605

+0

不客气!如果解决了您的问题,请记得接受我的回复。 – nessuno