本文介绍了TensorFlow 输入数据协议缓冲区(tf.train.Example)TypeError for Feature with string type的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的数据编码为 tf.train.Example 根据 tensorflow 的教程.我有一个字符串值要传递给 Example 类的 Features 属性,我正在使用以下代码:

I am trying to encode my data as tf.train.Example according to tensorflow's tutorial.I have a string value that I want to pass to the Features property of the Example class and I am using the following code:

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:

However, I get the error that it is expecting bytes not str:

TypeError: 'sample string 1' has type <class 'str'>, but expected one of: ((<class 'bytes'>,),)

我错过了什么?

推荐答案

好像他们期望 s1 是一个字节串,所以需要在前面加上 b":

It seems that they expect s1 to be a byte string, so you need to add b before the ":

import tensorflow as tf
tf_example = tf.train.Example()
s1 = b"sample string 1"
tf_example.features.feature['str1'].bytes_list.value.extend([s1])

这篇关于TensorFlow 输入数据协议缓冲区(tf.train.Example)TypeError for Feature with string type的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-30 03:41