在Tensorflow中使用字符串标签
问题内容:
我仍在尝试使用自己的图像数据运行Tensorflow。我可以通过这个示例链接中的converter_to()函数创建一个.tfrecords文件。
现在,我想用该示例链接中的代码来训练网络。
但是它在read_and_decode()函数中失败。我对该功能的更改是:
label = tf.decode_raw(features['label'], tf.string)
错误是:
TypeError: DataType string for attr 'out_type' not in list of allowed values: float32, float64, int32, uint8, int16, int8, int64
那么如何1)阅读和2)使用字符串标签进行张量流训练。
问题答案:
该convert_to_records.py
脚本创建一个.tfrecords
文件,其中每个记录都是Example
协议缓冲区。该协议缓冲区支持使用bytes_list
kind的字符串功能。
该tf.decode_raw
运算用于分析二进制串为图像数据;
它并非旨在解析字符串(文本)标签。假设这features['label']
是tf.string
张量,则可以使用tf.string_to_number
op将其转换为数字。TensorFlow程序内部对字符串处理的其他支持有限,因此,如果您需要执行一些更复杂的功能以将字符串标签转换为整数,则应在Python的修改版本中执行此转换convert_to_tensor.py
。