标准 TensorFlow 格式的 Unicode
Unicode in the standard TensorFlow format
按照文档 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
中,它会在遇到第一个 actual unicode 字符时失败。
BytesList definition 在 feature.proto 中,它的类型是 repeated bytes
,这意味着您需要向它传递一些可以转换为字节序列列表的东西。
有不止一种方法可以将 unicode
转换为字节列表,因此会产生歧义。您可以改为手动进行。 IE,要使用UTF-8
编码
value.encode("utf-8")
注意 Tensorflow 现在具有处理 unicode 文本的内置方法,包括与许多常见格式之间的转换。
按照文档 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
中,它会在遇到第一个 actual unicode 字符时失败。
BytesList definition 在 feature.proto 中,它的类型是 repeated bytes
,这意味着您需要向它传递一些可以转换为字节序列列表的东西。
有不止一种方法可以将 unicode
转换为字节列表,因此会产生歧义。您可以改为手动进行。 IE,要使用UTF-8
编码
value.encode("utf-8")
注意 Tensorflow 现在具有处理 unicode 文本的内置方法,包括与许多常见格式之间的转换。