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 .

Daniyal Javaid
  • 1,426
  • 2
  • 18
  • 32

1 Answers1

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.

mrry
  • 125,488
  • 26
  • 399
  • 400
  • so my output is completely fine ? or there is something which can be done to avoid this 'b' ? – Daniyal Javaid Jun 26 '17 at 17:24
  • 3
    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()`](https://docs.python.org/3.3/library/stdtypes.html#bytes.decode) instead (and specify the appropriate encoding if you're not using UTF-8). – mrry Jun 26 '17 at 17:27
  • thanks, i tried the decoding statement and it works ! – Daniyal Javaid Jun 26 '17 at 17:30