我创建了一个 tfRecord 文件来存储数据。我必须存储印地语文本,因此我使用 string.encode('utf-8') 将其保存在字节中。
但是,我在读取数据时陷入困境。我正在张量流数据集 API 的帮助下读取数据。我知道我可以使用 string.decode('utf-8') 对其进行解码,但这不是我想要的。我想要一些解决方案,通过它我可以将字节字符串解码回图形内的 Unicode 字符串。
我努力了as_text https://www.tensorflow.org/api_docs/python/tf/compat/as_text, 解码原始数据 https://www.tensorflow.org/api_docs/python/tf/io/decode_raw但他们给出了错误。
我的解析(映射)函数:
def _parse_function(tfrecord_serialized):
features={'float': tf.FixedLenSequenceFeature([],
tf.float32,allow_missing=True),
'byte': tf.FixedLenFeature([], tf.string),
'int': tf.FixedLenSequenceFeature([],
tf.int64,allow_missing=True)}
parsed_features = tf.parse_single_example(tfrecord_serialized,
features)
return parsed_features['float'],parsed_features['byte'], parsed_features['int']`
我正在阅读我的 tfRecord 文件,如下所示。
filenames = ["data.tfrecord"] ## List of filename,Multiple filename can be provided together.
dataset = tf.data.TFRecordDataset(filenames)
dataset = dataset.map(_parse_function)
iterator = dataset.make_initializable_iterator()`
t1,t2,t3 = iterator.get_next()
sess = tf.Session()
sess.run(iterator.initializer)
a,b,c = sess.run([t1,t2,t3])
print(a,b,c)
b.decode('utf-8')`
在 b.decode 上,我得到的输出非常好,出于明显的原因,我希望在图表中执行此操作,从 tf 返回到 python 然后再返回通常不是一个好主意。