在tensorflow2.0中,我想训练一个因时丢失的skip-gram模型。 tf.data.Dataset.from_tensor_slices()不适合,因为输入文件确实很大。所以我写了一个像这样的数据集生成器类:
class DataSet:
""""""
def __init__(self, args, vocab):
self.args = args
self.vocab = vocab
def generator(self):
"""a generator function, it will return skip-gram sample or cbow sample"""
with open(self.args.input) as f_input:
for line in tqdm.tqdm(f_input.readlines()):
tokens = line.strip().split()
tokens_indices = self.vocab.indices(tokens)
for index, target_word in enumerate(tokens_indices):
context_words = list()
begin = index - self.args.window_size if index - self.args.window_size > 0 else 0
end = index + 1 + self.args.window_size if index + self.args.window_size + 1 < len(tokens_indices) else len(
tokens_indices)
context_words.extend(tokens_indices[begin:index])
context_words.extend(tokens_indices[index + 1:end])
if self.args.cbow > 0:
yield context_words, target_word
else:
for i in range(len(context_words)):
yield target_word, context_words[i]
def dataset(self):
"""Using tf.data.Dataset.from_generator() to return sample"""
if self.args.cbow:
dataset = tf.data.Dataset.from_generator(
self.generator,
(tf.int32, tf.int32),
(tf.TensorShape([None]), tf.TensorShape([]))
)
else:
dataset = tf.data.Dataset.from_generator(
self.generator,
(tf.int32, tf.int32),
(tf.TensorShape([]), tf.TensorShape([]))
)
return dataset
然后我用以下代码测试我的代码:
dataset = DataSet(args, vocab).dataset()
iterator = dataset.make_one_shot_iterator()
for batch, (x,y) in enumerate(dataset.batch(128)):
pass
print(batch, x.shape, y.shape)
但是迭代所有行需要花费大量时间(在MacBook Pro 2012中约为10分钟/ 15000行)。有什么方法可以加快代码的速度吗?
如果使用大型数据集,则TFRecord是合适的选项。它使用二进制文件格式来存储数据,并且可能对导入管道的性能以及模型的训练时间产生重大影响。二进制数据占用磁盘上较少的空间,花费较少的时间进行复制,并且可以从磁盘更有效地进行读取。如果您的数据存储在旋转磁盘上,则尤其如此,因为与SSD相比,其读写性能要低得多。
但是,纯性能并不是TFRecord文件格式的唯一优势。它经过优化,可通过多种方式与Tensorflow一起使用。首先,它使合并多个数据集变得容易,并且与库提供的数据导入和预处理功能无缝集成。特别是对于太大而无法完全存储在内存中的数据集来说,这是一个优势,因为仅从磁盘加载然后处理时需要的数据(例如批处理)。 TFRecords的另一个主要优点是可以存储序列数据(例如,时间序列或单词编码),并且这种方式可以非常有效地(从编码的角度)导入这种类型的数据。
[建议使用glimpse on TFRecord的官方链接。您也可以通过how to build TFRecord pipeline上的此链接。
这是使用TFRecordWriter
写入序列化记录,然后将其加载到TFRecordDatset
中的简单示例
%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
def write_date_tfrecord():
#writes 10 dummy values to replicate the issue
Output = [20191221 + x for x in range(0,10)]
print("Writing Output - ", Output)
example = tf.train.Example(
features = tf.train.Features(
feature = {
'Output':tf.train.Feature(float_list=tf.train.FloatList(value=Output))
}
))
writer = tf.io.TFRecordWriter("Output.tf_record")
writer.write(example.SerializeToString())
def parse_function(serialized_example):
features = {
'Output': tf.io.FixedLenSequenceFeature([], tf.float32,allow_missing=True)
}
features = tf.io.parse_single_example(serialized=serialized_example, features=features)
Output = features['Output']
return Output
def dataset_generator():
trRecordDataset = tf.data.TFRecordDataset("Output.tf_record")
trRecordDataset = trRecordDataset.map(parse_function, num_parallel_calls = tf.data.experimental.AUTOTUNE)
return trRecordDataset
if __name__ == '__main__':
write_date_tfrecord()
generator = dataset_generator()
for Output in generator:
print(Output)
输出-
2.2.0
Writing Output - [20191221, 20191222, 20191223, 20191224, 20191225, 20191226, 20191227, 20191228, 20191229, 20191230]
tf.Tensor(
[20191220. 20191222. 20191224. 20191224. 20191224. 20191226. 20191228.
20191228. 20191228. 20191230.], shape=(10,), dtype=float32)
希望这能回答您的问题。祝您学习愉快。