【问题标题】:The print of string constant is always attached with 'b' inTensorFlow [duplicate]在TensorFlow中,字符串常量的打印总是附有“b”[重复]
【发布时间】:2017-04-15 18:10:01
【问题描述】:

在 Windows 10 上安装 TensorFlow r0.12(CPU) 的测试过程中,我发现打印的字符串 contant 总是以 'b' 结尾。 python的打印正常。我无法弄清楚原因所以来这里寻求帮助。代码如下:

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

【问题讨论】:

  • b 前缀是表示字节字符串而不是 unicode 字符串。默认值取决于您的 python 版本:python2 str 是字节,但 python3 str 是 unicode。
  • 简单地说:这是一个bytes 对象。

标签: python windows tensorflow


【解决方案1】:

使用sess.run(hello).decode(),因为它是一个字节串。 decode 方法会返回字符串。

你的打印语句必须看起来像

print(sess.run(hello).decode())

【讨论】:

  • Tensor 对象没有decode 属性,所以您的意思是print(sess.run(hello).decode())?这对我有用。
  • 为什么Validate your tensorflow installation! 上没有提到这个? (我使用 3.6.2 应该使用 unicode str
  • 是的,当你尝试新事物时总是很糟糕,而且事情不会顺其自然。即使是很小的警告也会很烦人。我希望他们能在网站上解决这个问题。
  • 2018 年仍未在网站上修复。谢谢@kermis
猜你喜欢
  • 2018-01-12
  • 2018-05-09
  • 2017-02-02
  • 2023-03-10
  • 2020-11-14
  • 2020-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多