CentAu
CentAu

Reputation: 11160

TensorFlow input data protocol buffer (tf.train.Example) TypeError for Feature with string type

I am trying to encode my data as tf.train.Example according to tensorflow's tutorial. I have a string value that I want to pass to the Features property of the Example class and I am using the following code:

import tensorflow as tf
tf_example = tf.train.Example()
s1 = "sample string 1"
tf_example.features.feature['str1'].bytes_list.value.extend([s1])

However, I get the error that it is expecting bytes not str:

TypeError: 'sample string 1' has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)

What am I missing?

Upvotes: 1

Views: 664

Answers (1)

Miriam Farber
Miriam Farber

Reputation: 19634

It seems that they expect s1 to be a byte string, so you need to add b before the ":

import tensorflow as tf
tf_example = tf.train.Example()
s1 = b"sample string 1"
tf_example.features.feature['str1'].bytes_list.value.extend([s1])

Upvotes: 2

Related Questions