TensorFlow输入数据协议缓冲区(tf.train.Example)具有字符串类型的Feature的TypeError

时间:2017-06-21 01:01:53

标签: python tensorflow

我正在尝试根据tensorflow的tf.train.Example将我的数据编码为tutorial。 我有一个字符串值,我想传递给Features类的Example属性,我使用以下代码:

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

但是,我收到的错误是bytes而不是str

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

我错过了什么?

1 个答案:

答案 0 :(得分:1)

他们似乎希望s1成为字节字符串,因此您需要在b之前添加"

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