我正在尝试根据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'>,),)
我错过了什么?
答案 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])