按照文档 here ,我正在尝试从 unicode 字符串创建功能。这是特征创建方法的样子,
def _bytes_feature(value):
return tf.train.Feature(bytes_list=tf.train.BytesList(value=[value]))
这会引发异常,
File "/home/rklopfer/.virtualenvs/tf/local/lib/python2.7/site-packages/google/protobuf/internal/python_message.py", line 512, in init
copy.extend(field_value)
File "/home/rklopfer/.virtualenvs/tf/local/lib/python2.7/site-packages/google/protobuf/internal/containers.py", line 275, in extend
new_values = [self._type_checker.CheckValue(elem) for elem in elem_seq_iter]
File "/home/rklopfer/.virtualenvs/tf/local/lib/python2.7/site-packages/google/protobuf/internal/type_checkers.py", line 108, in CheckValue
raise TypeError(message)
TypeError: u'Gross' has type <type 'unicode'>, but expected one of: (<type 'str'>,)
当然,如果我将 value
包装在 str
中,它会在遇到第一个实际 unicode 字符时失败。
最佳答案
字节列表 definition在 feature.proto 中,它是 repeated bytes
类型,这意味着您需要向它传递一些可转换为字节序列列表的内容。
将 unicode
转换为字节列表的方法不止一种,因此存在歧义。您可以改为手动进行。 IE,要使用UTF-8
编码
value.encode("utf-8")
关于python - 标准 TensorFlow 格式的 Unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38961547/