3

I am new to tensorflow , here is a quick question , this is my code

session=tf.Session()
x=tf.Variable(str)
valueOfX=session.run(x.assign('xyz'))
print(valueOfX)

Why the output is =>

b'xyz'

But when I use int as datatype and assign an integer , the assignment is fine .

1
  • By "the assignment is fine" what do you mean? You get the expected output? Can you show us that please. Commented Jun 26, 2017 at 17:23

1 Answer 1

2

This confusion arises because Python 3 uses a Unicode string representation for string literals. The printed representation b'xyz' means that valueOfX is a bytes object. TensorFlow uses bytes as the internal representation of all string tensors and variables, and (when using Python 3) implicitly converts str literals, such as the 'xyz' in your code, to bytes using a UTF-8 unicode encoding.

Sign up to request clarification or add additional context in comments.

3 Comments

so my output is completely fine ? or there is something which can be done to avoid this 'b' ?
Yes, it is fine. Assuming that your strings use only ASCII characters, you can disregard the b prefix. If you use non-ASCII Unicode characters in your program, you may want to print valueOfX.decode() instead (and specify the appropriate encoding if you're not using UTF-8).
thanks, i tried the decoding statement and it works !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.