我正在尝试使用 tensorflow 获取词嵌入,并且我已经使用我的语料库创建了相邻的工作列表。
我的词汇表中唯一单词的数量为 8000,相邻单词列表的数量约为 160 万
由于数据非常大,我试图将单词列表分批写入 TFRecords 文件。
def save_tfrecords_wordlist(toprocess_word_lists, path ):
writer = tf.io.TFRecordWriter(path)
for word_list in toprocess_word_lists:
features=tf.train.Features(
feature={
'word_list_X': tf.train.Feature( bytes_list=tf.train.BytesList(value=[word_list[0].encode('utf-8')] )),
'word_list_Y': tf.train.Feature( bytes_list=tf.train.BytesList(value=[word_list[1].encode('utf-8') ]))
}
)
example = tf.train.Example(features = features)
writer.write(example.SerializeToString())
writer.close()
定义批次
batches = [0,250000,500000,750000,1000000,1250000,1500000,1641790]
for i in range(len(batches) - 1 ):
batches_start = batches[i]
batches_end = batches[i + 1]
print( str(batches_start) + " -- " + str(batches_end ))
toprocess_word_lists = word_lists[batches_start:batches_end]
save_tfrecords_wordlist( toprocess_word_lists, path +"/TFRecords/data_" + str(i) +".tfrecords")
##############################
def _parse_function(example_proto):
features = {"word_list_X": tf.io.FixedLenFeature((), tf.string),
"word_list_Y": tf.io.FixedLenFeature((), tf.string)}
parsed_features = tf.io.parse_single_example(example_proto, features)
沧海一幻觉
相关分类