遵循文档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字符上失败。
答案 0 :(得分:5)
BytesList definition位于feature.proto中,类型为repeated bytes
,这意味着您需要将可转换为可转换为字节序列列表的内容传递给它。
将unicode
转换为字节列表的方法不止一种,因此不明确。你可以手动完成。 IE,使用UTF-8
编码
value.encode("utf-8")
答案 1 :(得分:0)
注意Tensorflow现在具有处理unicode文本的内置方法,包括与多种常见格式之间的转换。