在评估字符串张量时,Tensorflow输出一个额外的``b'' [英] Tensorflow outputs an additional 'b' when evaluating a string tensor

查看:144
本文介绍了在评估字符串张量时,Tensorflow输出一个额外的``b''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近在我的Conda中安装了tensorflow: 在运行以下代码时,它给了我一个额外的"b". 我不知道这个"b"的原因..? 为什么要打印它以及如何去除它?该程序在Jupyter NoteBook中实现的Tensorflow环境中运行

I've recently installed tensorflow into my Conda: while running following code it gives me an additional 'b'. I don't know the reason for that 'b'..? Why is it being printed and how to get rid of it? This program is running in Tensorflow environment implemented in Jupyter NoteBook

import tesnorflow as tf
msg_1 = tf.constant('Welcome to TensorFlow')
with tf.Session() as s:
    print(s.sub(msg_1))

结果

b'Welcome to TensorFlow'

另请参见

推荐答案

Tensorflow将字符串表示为Bytes对象. 此"b"是Python的处理方式标记Bytes对象.您可以通过应用.decode()将session.run()返回的Bytes对象转换为常规的python字符串:

Tensorflow represents strings as Bytes objects. This 'b' is Python's way of marking Bytes objects. You can convert the Bytes object returned by session.run() to a regular python string by applying .decode():

print(s.run(msg_1).decode('UTF-8'))

这篇关于在评估字符串张量时,Tensorflow输出一个额外的``b''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆