gpt4 book ai didi

python - 如何表示 tensorflow 中的稀疏特征列表?

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:14 24 4
gpt4 key购买 nike

我正在尝试使用 tensorflow.SequenceExample 来存储我的功能,例如问答系统,如下所述。

给定一个问题文本,例如“家有多远”。我想将其视为一个序列,并使用稀疏表示来表示每个单词。这不是一次性编码。每个单词都有多个 bool 特征。

how -> [ 1 0 0 0 1] -> [1,5]
far -> [ 0 1 1 0 0] -> [2,3]
is -> [ 1 1 0 0 1] -> [1,2,5]
home-> [ 0 0 1 1 0] -> [3,4]

我的文本现在表示为:[[1,5],[2,3],[1,2,5],[3,4]]

同样,我有另一个文本 answer text,它具有类似列表列表的表示形式。

我如何用 tensorflow 的 TFRecord 格式来写这个?我已经在下面的代码中尝试过了。我所知道的错误是,我将一个 int64list 发送到一个只需要一个 int64 值的函数。

有人在表示此类数据方面取得了成功吗?

import tensorflow as tf


example = {

'query': [[123, 543, 234, 2322],
[133, 243, 233, 256, 4332],
[232, 356, 632],
[153, 143, 231, 456]],
'document': [[1156, 12322],
[2133, 14332],
[1143, 1343, 1232, 1356, 1632],
[1153, 1143]],
'label': 1
}

tmp_filename = 'tf.tmp'


def make_example(example):
"""
Makes a single example from Python lists that follows the
format of tf.train.SequenceExample.
"""
query_features = example['query']
keyword_features = example['document']
example_sequence = tf.train.SequenceExample()

example_sequence.context.feature["query_length"].int64_list.value.append(len(query_features))
example_sequence.context.feature["keyword_length"].int64_list.value.append(len(keyword_features))

query = example_sequence.feature_lists.feature_list["query"]
document = example_sequence.feature_lists.feature_list["document"]

for feat in query_features:
print("Appending: ", feat)
#query.feature.add().int64_list.value.append(feat)
query.feature.add().list.value.append(feat)


for feat in keyword_features:
document.feature.add().int64_list.value.append(feat)

return example_sequence


# Write all examples into a TFRecords file
def save_tf(filename):
with open(filename, 'w') as fp:
writer = tf.python_io.TFRecordWriter(fp.name)

ex = make_example(example)
writer.write(ex.SerializeToString())
writer.close()


#
def read_and_decode_single_example(filename):
filename_queue = tf.train.string_input_producer([filename],
num_epochs=None)

reader = tf.TFRecordReader()
_, serialized_example = reader.read(filename_queue)

context_features = {
"length": tf.FixedLenFeature([], dtype=tf.int64)
}

sequence_features = {

"query": tf.VarLenFeature([], dtype=tf.int64),
"document": tf.VarLenFeature([], dtype=tf.int64)
}

return serialized_example, context_features, sequence_feature

最佳答案

根据我的经验,编码数组的最佳方式是使用 numpy 的 tostring提取原始字节。在读取数据集时,您可以使用 TensorFlow 的 decode_raw .

以下应该足以满足您的编码要求:

for feat in query_features:
print("Appending: ", feat)
raw_feat = np.array(feat, dtype=np.int64).tostring()
query.feature.add().bytes_list.value.append(raw_feat)

for feat in keyword_features:
raw_feat = np.array(feat, dtype=np.int64).tostring()
document.feature.add().bytes_list.value.append(raw_feat)

请注意,您必须确保在解码时在另一端使用相同的数据类型(本例中为 tf.int64),否则最终会出现乱码。

关于python - 如何表示 tensorflow 中的稀疏特征列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51161751/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com