Python 来自生成器的tensorflow数据集

Python 来自生成器的tensorflow数据集,python,tensorflow,tensorflow-datasets,Python,Tensorflow,Tensorflow Datasets,我有一个数据文件,格式如下: A B C x,y,z 因为BC非常非常大,所以我不想把它转换成下面的格式到磁盘上 A B C x A、B、C、y A B C z 现在我使用dataset.from_generator读取它,但在训练时速度非常慢 我怎样才能解决这个问题 罗吉斯是: def gen_sample2(self): for file_name in self.file_names: with open(file_name) as f:

我有一个数据文件,格式如下: A B C x,y,z

因为BC非常非常大,所以我不想把它转换成下面的格式到磁盘上

  • A B C x
  • A、B、C、y
  • A B C z
  • 现在我使用dataset.from_generator读取它,但在训练时速度非常慢

    我怎样才能解决这个问题

    罗吉斯是:

    def gen_sample2(self):
        for file_name in self.file_names:
    
            with open(file_name) as f:
                for line in f:
                    item = line.strip().split(',')
                    pids_str = item[4]
                    pids = [int(pid) for pid in pids_str.split('|') if pid in self.pid_set]
                    for item_id in pids:
                        yield {"gender": item[1], "topics": item[2], "weights": item[3], "item_id": item_id}
    
    def get_dataset_iterator(self):
        dataset = tf.data.Dataset.from_generator(self.gen_sample2,
                                                 output_types={"gender":tf.string,
                                                               "topics":tf.string,
                                                               "weights":tf.string,
                                                               "item_id":tf.int32
                                                               })
        dataset = dataset.batch(self.batch_size)
    
        # if self.buffer_size > 0:
        #     dataset = dataset.shuffle(self.buffer_size)
    
        dataset = dataset.repeat(self.iter)
        return  dataset.make_one_shot_iterator()
    

    您应该将数据转换为,这样TensorFlow将能够更快地读取数据。这将是巨大的,比如说A是一个很长的字符串,我的意思是只是源逗号分隔的文件。你不能把它转换成TFRecords文件吗?它应该是一个可比较的大小(然后你会做一个类似于你现在所做的转换)。你应该将你的数据转换为,这样TensorFlow将能够更快地读取它。这将是巨大的,比如说a是一个非常长的字符串,我的意思是只是源逗号分隔的文件。你不能把它转换成TFRecords文件吗?它应该是一个可比较的大小(然后您将执行与现在类似的转换)。